diff --git a/src/Entity/Index.php b/src/Entity/Index.php index 614e0fe..179206b 100644 --- a/src/Entity/Index.php +++ b/src/Entity/Index.php @@ -18,6 +18,7 @@ use Drupal\search_api\Query\QueryInterface; use Drupal\search_api\Query\ResultSetInterface; use Drupal\search_api\SearchApiException; use Drupal\search_api\ServerInterface; +use Drupal\search_api\Tracker\TrackerInterface; use Drupal\search_api\Utility; use Drupal\user\TempStoreException; use Drupal\views\Views; @@ -306,11 +307,13 @@ class Index extends ConfigEntityBase implements IndexInterface { */ public function getDatasource($datasource_id) { $datasources = $this->getDatasources(); + if (empty($datasources[$datasource_id])) { $args['@datasource'] = $datasource_id; $args['%index'] = $this->label(); throw new SearchApiException(new FormattableMarkup('The datasource with ID "@datasource" could not be retrieved for index %index.', $args)); } + return $datasources[$datasource_id]; } @@ -358,12 +361,7 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function getTrackerId() { - if (is_null($this->tracker_settings)) { - return 'default'; - } - - $plugins = array_keys($this->tracker_settings); - return $this->tracker_settings[$plugins[0]]['plugin_id']; + return $this->getTrackerInstance()->getPluginId(); } /** @@ -374,12 +372,15 @@ class Index extends ConfigEntityBase implements IndexInterface { if (is_null($this->tracker_settings)) { $tracker_plugin_configuration = array('index' => $this); - } else { + $plugin_id = 'default'; + } + else { $plugins = array_keys($this->tracker_settings); $tracker_plugin_configuration = array('index' => $this) + $this->tracker_settings[$plugins[0]]['settings']; + $plugin_id = $this->tracker_settings[$plugins[0]]['plugin_id']; } - if (!($this->trackerInstance = \Drupal::service('plugin.manager.search_api.tracker')->createInstance($this->getTrackerId(), $tracker_plugin_configuration))) { + if (!($this->trackerInstance = \Drupal::service('plugin.manager.search_api.tracker')->createInstance($plugin_id, $tracker_plugin_configuration))) { $args['@tracker'] = $this->getTrackerId(); $args['%index'] = $this->label(); throw new SearchApiException(new FormattableMarkup('The tracker with ID "@tracker" could not be retrieved for index %index.', $args)); @@ -392,6 +393,15 @@ class Index extends ConfigEntityBase implements IndexInterface { /** * {@inheritdoc} */ + public function setTracker(TrackerInterface $tracker) { + $this->trackerInstance = $tracker; + + return $this; + } + + /** + * {@inheritdoc} + */ public function hasValidServer() { return $this->server !== NULL && Server::load($this->server) !== NULL; } @@ -1003,6 +1013,14 @@ class Index extends ConfigEntityBase implements IndexInterface { } } + // Add tracker settings. + $this->tracker_settings = array( + $this->getTrackerId() => array( + 'plugin_id' => $this->getTrackerId(), + 'settings' => $this->getTrackerInstance()->getConfiguration(), + ) + ); + // Call the preIndexSave() method of all applicable processors. foreach ($this->getProcessorsByStage(ProcessorInterface::STAGE_PRE_INDEX_SAVE) as $processor) { $processor->preIndexSave(); diff --git a/src/Form/IndexForm.php b/src/Form/IndexForm.php index 1db8781..a415956 100644 --- a/src/Form/IndexForm.php +++ b/src/Form/IndexForm.php @@ -554,12 +554,7 @@ class IndexForm extends EntityForm { $tracker_form_state = new SubFormState($form_state, array('tracker_config')); $tracker->submitConfigurationForm($form['tracker_config'], $tracker_form_state); - $index->set('tracker_settings', array( - $tracker_id => array( - 'plugin_id' => $tracker_id, - 'settings' => $tracker->getConfiguration(), - ) - )); + $index->setTracker($tracker); } /** diff --git a/src/IndexInterface.php b/src/IndexInterface.php index 85570ae..98633d1 100644 --- a/src/IndexInterface.php +++ b/src/IndexInterface.php @@ -8,10 +8,12 @@ namespace Drupal\search_api; use Drupal\Core\Config\Entity\ConfigEntityInterface; +use Drupal\search_api\Datasource\DatasourceInterface; use Drupal\search_api\Item\FieldInterface; use Drupal\search_api\Processor\ProcessorInterface; use Drupal\search_api\Query\QueryInterface; use Drupal\search_api\Query\ResultSetInterface; +use Drupal\search_api\Tracker\TrackerInterface; /** * Defines the interface for index entities. @@ -204,6 +206,16 @@ interface IndexInterface extends ConfigEntityInterface { public function getTrackerInstance(); /** + * Sets the tracker the index uses. + * + * @param \Drupal\search_api\Tracker\TrackerInterface $tracker + * The tracker the index uses. + * + * @return $this + */ + public function setTracker(TrackerInterface $tracker); + + /** * Determines whether this index is lying on a valid server. * * @return bool diff --git a/tests/src/Kernel/DependencyRemovalTest.php b/tests/src/Kernel/DependencyRemovalTest.php index 08a16e7..2619ae5 100644 --- a/tests/src/Kernel/DependencyRemovalTest.php +++ b/tests/src/Kernel/DependencyRemovalTest.php @@ -122,7 +122,7 @@ class DependencyRemovalTest extends KernelTestBase { // Set the server on the index and save that, too. However, we don't want // the index enabled, since that would lead to all kinds of overhead which // is completely irrelevant for this test. - $this->index->set('server', $server->id()); + $this->index->setServer($server); $this->index->disable(); $this->index->save(); @@ -348,16 +348,15 @@ class DependencyRemovalTest extends KernelTestBase { // server. $dependency_key = $this->dependency->getConfigDependencyKey(); $dependency_name = $this->dependency->getConfigDependencyName(); - $this->index->set('tracker_settings', array( - 'search_api_test_dependencies' => array( - 'plugin_id' => 'search_api_test_dependencies', - 'settings' => array( - $dependency_key => array( - $dependency_name, - ), - ) - ) - )); + + $tracker = \Drupal::getContainer() + ->get('plugin.manager.search_api.tracker') + ->createInstance('search_api_test_dependencies', array( + $dependency_key => array( + $dependency_name, + ), + )); + $this->index->setTracker($tracker); $this->index->save(); // Check the dependencies were calculated correctly. @@ -391,10 +390,9 @@ class DependencyRemovalTest extends KernelTestBase { // Depending on whether the plugin should have removed the dependency or // not, make sure the right action was taken. - $config = $this->index->get('tracker_settings'); - $plugins = array_keys($config); - $tracker = $config[$plugins[0]]['plugin_id']; - $tracker_config = $config[$plugins[0]]['settings']; + $tracker_instance = $this->index->getTrackerInstance(); + $tracker = $tracker_instance->getPluginId(); + $tracker_config = $tracker_instance->getConfiguration(); if ($remove_dependency) { $this->assertEquals('search_api_test_dependencies', $tracker, 'Tracker not reset'); $this->assertEmpty($tracker_config, 'Tracker settings adapted'); @@ -426,7 +424,10 @@ class DependencyRemovalTest extends KernelTestBase { 'settings' => array(), ), )); - $this->index->set('tracker', 'search_api_test_dependencies'); + $tracker = \Drupal::getContainer() + ->get('plugin.manager.search_api.tracker') + ->createInstance('search_api_test_dependencies'); + $this->index->setTracker($tracker); $this->index->save(); // Check the dependencies were calculated correctly.