diff --git a/src/Entity/Index.php b/src/Entity/Index.php index 179206b..188ac80 100644 --- a/src/Entity/Index.php +++ b/src/Entity/Index.php @@ -448,16 +448,22 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function getProcessors($only_enabled = TRUE) { + if (!$only_enabled) { + return $this->loadProcessors(); + } + + if (!is_null($this->processorInstances)) { + return $this->processorInstances; + } + $processors = $this->loadProcessors(); // Filter processors by status if required. Enabled processors are those // which have settings in the "processors" option. - if ($only_enabled) { - $processors_settings = $this->processor_settings; - $processors = array_intersect_key($processors, $processors_settings); - } + $processors_settings = $this->processor_settings; + $this->processorInstances = array_intersect_key($processors, $processors_settings); - return $processors; + return $this->processorInstances; } /** @@ -496,13 +502,7 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function addProcessor(ProcessorInterface $processor) { - $this->processor_settings[$processor->getPluginId()] = array( - 'plugin_id' => $processor->getPluginId(), - 'settings' => $processor->getConfiguration(), - ); - - // Reset processors canonical storage. - $this->processorInstances = NULL; + $this->processorInstances[$processor->getPluginId()] = $processor; return $this; } @@ -511,10 +511,12 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function removeProcessor($processor_id) { - unset($this->processor_settings[$processor_id]); - - // Reset processors canonical storage. - $this->processorInstances = NULL; + // Make sure the processorInstances are loaded before trying to remove a + // plugin from them. + if (is_null($this->processorInstances)) { + $this->getProcessors(); + } + unset($this->processorInstances[$processor_id]); return $this; } @@ -526,30 +528,30 @@ class Index extends ConfigEntityBase implements IndexInterface { * The loaded processors, keyed by processor ID. */ protected function loadProcessors() { - if (is_null($this->processorInstances)) { - /** @var $processor_plugin_manager \Drupal\search_api\Processor\ProcessorPluginManager */ - $processor_plugin_manager = \Drupal::service('plugin.manager.search_api.processor'); - $processor_settings = $this->processor_settings; - - foreach ($processor_plugin_manager->getDefinitions() as $name => $processor_definition) { - if (class_exists($processor_definition['class']) && empty($this->processorInstances[$name])) { - // Create our settings for this processor. - $settings = empty($processor_settings[$name]['settings']) ? array() : $processor_settings[$name]['settings']; - $settings['index'] = $this; - - /** @var $processor \Drupal\search_api\Processor\ProcessorInterface */ - $processor = $processor_plugin_manager->createInstance($name, $settings); - if ($processor->supportsIndex($this)) { - $this->processorInstances[$name] = $processor; - } - } - elseif (!class_exists($processor_definition['class'])) { - \Drupal::logger('search_api')->warning('Processor @id specifies a non-existing @class.', array('@id' => $name, '@class' => $processor_definition['class'])); + $processor_instances = array(); + + /** @var $processor_plugin_manager \Drupal\search_api\Processor\ProcessorPluginManager */ + $processor_plugin_manager = \Drupal::service('plugin.manager.search_api.processor'); + $processor_settings = $this->processor_settings; + + foreach ($processor_plugin_manager->getDefinitions() as $name => $processor_definition) { + if (class_exists($processor_definition['class']) && empty($processor_instances[$name])) { + // Create our settings for this processor. + $settings = empty($processor_settings[$name]['settings']) ? array() : $processor_settings[$name]['settings']; + $settings['index'] = $this; + + /** @var $processor \Drupal\search_api\Processor\ProcessorInterface */ + $processor = $processor_plugin_manager->createInstance($name, $settings); + if ($processor->supportsIndex($this)) { + $processor_instances[$name] = $processor; } } + elseif (!class_exists($processor_definition['class'])) { + \Drupal::logger('search_api')->warning('Processor @id specifies a non-existing @class.', array('@id' => $name, '@class' => $processor_definition['class'])); + } } - return $this->processorInstances; + return $processor_instances; } /** @@ -1004,6 +1006,7 @@ class Index extends ConfigEntityBase implements IndexInterface { // We first have to check for locked processors, otherwise their // preIndexSave() methods might not be called in the next step. + $this->processor_settings = array(); foreach ($this->getProcessors(FALSE) as $processor_id => $processor) { if ($processor->isLocked() && !isset($this->processor_settings[$processor_id])) { $this->processor_settings[$processor_id] = array( @@ -1013,6 +1016,19 @@ class Index extends ConfigEntityBase implements IndexInterface { } } + foreach ($this->getProcessors() as $processor_id => $processor) { + $this->processor_settings[$processor_id] = array( + 'plugin_id' => $processor_id, + 'settings' => $processor->getConfiguration(), + ); + } + + // Reset the canonical representation of the processors by running + // ::getProcessors() again here after resetting them back to NULL. This is + // only needed when creating a new Index. + $this->processorInstances = NULL; + $this->getProcessors(); + // Add tracker settings. $this->tracker_settings = array( $this->getTrackerId() => array( @@ -1460,6 +1476,8 @@ class Index extends ConfigEntityBase implements IndexInterface { // according to plugin type, unfortunately. // First, remove plugins that need to be removed. $this->processor_settings = array_intersect_key($this->processor_settings, $all_plugins['processors']); + $this->processorInstances = array_intersect_key($this->processorInstances, $all_plugins['processors']); + $new_datasources = array(); foreach ($all_plugins['datasources'] as $plugin_id => $settings) { $new_datasources[$plugin_id] = array( diff --git a/src/Tests/IntegrationTest.php b/src/Tests/IntegrationTest.php index 9ea4230..cd098d1 100644 --- a/src/Tests/IntegrationTest.php +++ b/src/Tests/IntegrationTest.php @@ -123,6 +123,7 @@ class IntegrationTest extends WebTestBase { 'datasources[]' => array('entity:node'), ); $this->drupalPostForm(NULL, $edit, $this->t('Save')); + $this->assertResponse(200); $this->assertText($this->t('The index was successfully saved.')); $this->assertText($this->t('Successfully tracked @count items for this index.', array('@count' => 2))); $this->assertEqual(2, $this->countTrackedItems()); diff --git a/src/Tests/Processor/ProcessorIntegrationTest.php b/src/Tests/Processor/ProcessorIntegrationTest.php index b2ab842..2610fda 100644 --- a/src/Tests/Processor/ProcessorIntegrationTest.php +++ b/src/Tests/Processor/ProcessorIntegrationTest.php @@ -34,7 +34,7 @@ class ProcessorIntegrationTest extends WebTestBase { 'status' => 1, 'datasource_settings' => array( 'entity:node' => array( - 'plugin_id' => 'entity:user', + 'plugin_id' => 'entity:node', 'settings' => array(), ), ), diff --git a/tests/src/Kernel/ServerTaskTest.php b/tests/src/Kernel/ServerTaskTest.php index a3faf29..8fe8e7a 100644 --- a/tests/src/Kernel/ServerTaskTest.php +++ b/tests/src/Kernel/ServerTaskTest.php @@ -97,7 +97,7 @@ class ServerTaskTest extends KernelTestBase { 'settings' => array(), ), ), - 'tracker' => array( + 'tracker_settings' => array( 'default' => array( 'plugin_id' => 'default', 'settings' => array(),