Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2994689 Allow disabling indexes #83

Open
wants to merge 6 commits into
base: 8.x-7.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion elasticsearch_helper.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:

plugin.manager.elasticsearch_index.processor:
class: Drupal\elasticsearch_helper\Plugin\ElasticsearchIndexManager
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler', '@entity_type.manager', '@queue', '@logger.factory']
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler', '@entity_type.manager', '@queue', '@logger.factory', '@config.factory']

elasticsearch_helper.type_event_subscriber:
class: Drupal\elasticsearch_helper\EventSubscriber\TypeEventSubscriber
Expand Down
40 changes: 39 additions & 1 deletion src/Plugin/ElasticsearchIndexManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\elasticsearch_helper\Plugin;

use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
Expand Down Expand Up @@ -34,6 +35,11 @@ class ElasticsearchIndexManager extends DefaultPluginManager {
*/
protected $logger;

/**
* @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig
*/
protected $config;

/**
* Constructor for ElasticsearchIndexManager objects.
*
Expand All @@ -50,15 +56,18 @@ class ElasticsearchIndexManager extends DefaultPluginManager {
* Queue factory.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* Logger factory.
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* Config factory service.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager, QueueFactory $queue_factory, LoggerChannelFactoryInterface $logger_factory) {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager, QueueFactory $queue_factory, LoggerChannelFactoryInterface $logger_factory, ConfigFactory $config_factory) {
parent::__construct('Plugin/ElasticsearchIndex', $namespaces, $module_handler, 'Drupal\elasticsearch_helper\Plugin\ElasticsearchIndexInterface', 'Drupal\elasticsearch_helper\Annotation\ElasticsearchIndex');

$this->alterInfo('elasticsearch_helper_elasticsearch_index_info');
$this->setCacheBackend($cache_backend, 'elasticsearch_helper_elasticsearch_index_plugins');
$this->entityTypeManager = $entity_type_manager;
$this->queue = $queue_factory->get('elasticsearch_helper_indexing');
$this->logger = $logger_factory->get('elasticsearch_helper');
$this->config = $config_factory->get('elasticsearch_helper.settings');
}

/**
Expand All @@ -70,6 +79,10 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
*/
public function indexEntity(EntityInterface $entity) {
foreach ($this->getDefinitions() as $plugin) {
if (!$this->indexIsEnabled($plugin['id'])) {
continue;
}

if (isset($plugin['entityType']) && $entity->getEntityTypeId() == $plugin['entityType']) {
if (!empty($plugin['bundle']) && $plugin['bundle'] != $entity->bundle()) {
// Do not index if defined plugin bundle differs from entity bundle.
Expand Down Expand Up @@ -100,6 +113,10 @@ public function indexEntity(EntityInterface $entity) {
*/
public function deleteEntity(EntityInterface $entity) {
foreach ($this->getDefinitions() as $plugin) {
if (!$this->indexIsEnabled($plugin['id'])) {
continue;
}

if (isset($plugin['entityType']) && $entity->getEntityTypeId() == $plugin['entityType']) {
if (!empty($plugin['bundle']) && $plugin['bundle'] != $entity->bundle()) {
// Do not delete if defined plugin bundle differs from entity bundle.
Expand Down Expand Up @@ -130,6 +147,10 @@ public function deleteEntity(EntityInterface $entity) {
*/
public function reindex($indices = [], array $context = []) {
foreach ($this->getDefinitions() as $definition) {
if (!$this->indexIsEnabled($definition['id'])) {
continue;
}

if (empty($indices) || in_array($definition['id'], $indices)) {
/** @var \Drupal\elasticsearch_helper\Plugin\ElasticsearchIndexInterface $plugin */
$plugin = $this->createInstance($definition['id']);
Expand Down Expand Up @@ -184,4 +205,21 @@ public function addToQueue($entity_type, $entity_id) {
]);
}

/**
* Check whether an index is enabled according to configuration.
*
* @param string $index
* Id of the index to check.
*
* @return bool
* Whether the index should be considered enabled.
*/
public function indexIsEnabled($index) {
$index_statuses = $this->config->get('index_statuses');

// When an index is not yet known in configuration, we default to enabling
// it.
return isset($index_statuses[$index]) ? $index_statuses[$index] : TRUE;
}

}