diff --git a/src/Entity/Index.php b/src/Entity/Index.php index 88148a5..672bb37 100644 --- a/src/Entity/Index.php +++ b/src/Entity/Index.php @@ -1489,6 +1489,8 @@ class Index extends ConfigEntityBase implements IndexInterface { } foreach ($this->getProcessors() as $processor) { $this->calculatePluginDependencies($processor); + $processor_dependencies = $processor->calculateDependencies(); + $this->dependencies += $processor_dependencies; } foreach ($this->getDatasources() as $datasource) { $this->calculatePluginDependencies($datasource); @@ -1525,6 +1527,15 @@ class Index extends ConfigEntityBase implements IndexInterface { } $this->setOption('processors', $processors); + // Make sure that all enabled processors are notified about the dependencies + // that will suddenly be removed and give it time to react to that. + foreach ($this->getProcessors() as $processor) { + $is_processor_changed = $processor->onDependencyRemoval($this, $dependencies); + if ($is_processor_changed) { + $changed = TRUE; + } + } + return $changed; } diff --git a/src/Processor/ProcessorInterface.php b/src/Processor/ProcessorInterface.php index 707232d..90d4d49 100644 --- a/src/Processor/ProcessorInterface.php +++ b/src/Processor/ProcessorInterface.php @@ -174,4 +174,17 @@ interface ProcessorInterface extends IndexPluginInterface { */ public function requiresReindexing(array $old_settings = NULL, array $new_settings = NULL); + /** + * Informs the plugin that entities it depends on will be deleted. + * + * @param \Drupal\search_api\IndexInterface $index + * The index we're being removed from. + * @param array $dependencies + * An array of dependencies + * + * @return bool + * Has something changed? + */ + public function onDependencyRemoval(IndexInterface $index, array $dependencies); + } diff --git a/src/Processor/ProcessorPluginBase.php b/src/Processor/ProcessorPluginBase.php index f873d62..fbb309b 100644 --- a/src/Processor/ProcessorPluginBase.php +++ b/src/Processor/ProcessorPluginBase.php @@ -111,4 +111,13 @@ abstract class ProcessorPluginBase extends IndexPluginBase implements ProcessorI // indexing process. return $this->supportsStage(ProcessorInterface::STAGE_PREPROCESS_INDEX); } + + /** + * {@inheritdoc} + */ + public function onDependencyRemoval(IndexInterface $index, array $dependencies) { + // By default, we're not reacting to anything and so we should leave + // everything as it was. + return FALSE; + } } diff --git a/src/Tests/DependencyRemovalTest.php b/src/Tests/DependencyRemovalTest.php index f0f082d..b68fb11 100644 --- a/src/Tests/DependencyRemovalTest.php +++ b/src/Tests/DependencyRemovalTest.php @@ -7,6 +7,8 @@ namespace Drupal\search_api\Tests; +use Drupal\Core\Entity\Entity\EntityViewMode; + /** * Tests what happens when an index's dependencies are removed. * @@ -62,6 +64,28 @@ class DependencyRemovalTest extends WebTestBase { } /** + * Tests what happens when a processors dependencies are removed. + * + * This is in a seperate test, because we need the search_api_test_processor enabled. + */ + public function testProcessorDependencyRemoval() { + EntityViewMode::create(array('id' => 'node.teaser_test', 'targetEntityType' => 'node'))->save(); + + $this->drupalGet($this->getIndexPath('processors')); + $this->assertText($this->t('Dependency test processor')); + $this->drupalPostForm(NULL, array('status[dependency_test_processor]' => 1), $this->t('Save')); + $this->assertResponse(200); + $this->assertText($this->t('The indexing workflow was successfully edited.')); + + $evm = EntityViewMode::load('node.teaser_test'); + $evm->delete(); + + // The index was removed, because the entity view mode was removed. + $this->drupalGet($this->getIndexPath()); + $this->assertResponse(404); + } + + /** * Tests the reaction of an index when a processor's module is removed. * * When a module that provides a processor is uninstalled, it should not diff --git a/tests/search_api_test_processor/config/schema/search_api_test_processor.schema.yml b/tests/search_api_test_processor/config/schema/search_api_test_processor.schema.yml index a9c5c2c..edeee3e 100644 --- a/tests/search_api_test_processor/config/schema/search_api_test_processor.schema.yml +++ b/tests/search_api_test_processor/config/schema/search_api_test_processor.schema.yml @@ -8,3 +8,14 @@ plugin.plugin_configuration.search_api_processor.dummy_processor: sequence: type: string label: 'Selected field' + +plugin.plugin_configuration.search_api_processor.dependency_test_processor: + type: mapping + label: 'Dummy processor configuration' + mapping: + fields: + type: sequence + label: 'The selected fields' + sequence: + type: string + label: 'Selected field' diff --git a/tests/search_api_test_processor/src/Plugin/search_api/processor/DependencyTestProcessor.php b/tests/search_api_test_processor/src/Plugin/search_api/processor/DependencyTestProcessor.php new file mode 100644 index 0000000..6e827ab --- /dev/null +++ b/tests/search_api_test_processor/src/Plugin/search_api/processor/DependencyTestProcessor.php @@ -0,0 +1,71 @@ +addDependency('config', $evm->getEntityType()->getConfigPrefix() . '.' . $evm->id()); + } + + return $form; + } + + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + return $this->dependencies; + } + + /** + * {@inheritdoc} + */ + public function onDependencyRemoval(IndexInterface $index, array $dependencies) { + return FALSE; + } + +}