diff --git a/config/schema/search_api.index.schema.yml b/config/schema/search_api.index.schema.yml index e942124..4391420 100644 --- a/config/schema/search_api.index.schema.yml +++ b/config/schema/search_api.index.schema.yml @@ -87,12 +87,18 @@ search_api.index.*: sequence: type: plugin.plugin_configuration.search_api_datasource.[%key] label: 'Datasource plugin configuration' - tracker: - type: string - label: 'Tracker plugin ID' - tracker_config: - label: 'Tracker config plugin' - type: plugin.plugin_configuration.search_api_tracker.[%parent.tracker] + tracker_settings: + type: sequence + label: 'Tracker settings' + sequence: + type: mapping + label: 'A tracker' + mapping: + plugin_id: + type: string + label: 'The plugin ID of the tracker config' + settings: + type: plugin.plugin_configuration.search_api_tracker.[%parent.plugin_id] server: type: string label: 'Server ID' diff --git a/config/schema/search_api.tracker.schema.yml b/config/schema/search_api.tracker.schema.yml index 297badc..1cb3518 100644 --- a/config/schema/search_api.tracker.schema.yml +++ b/config/schema/search_api.tracker.schema.yml @@ -1,4 +1,3 @@ -# @todo Update the key when https://www.drupal.org/node/2291073 is fixed. plugin.plugin_configuration.search_api_tracker.default: type: sequence label: 'Entity tracker configuration' diff --git a/search_api_db/search_api_db_defaults/config/optional/search_api.index.default_index.yml b/search_api_db/search_api_db_defaults/config/optional/search_api.index.default_index.yml index 06b8a38..365ab47 100644 --- a/search_api_db/search_api_db_defaults/config/optional/search_api.index.default_index.yml +++ b/search_api_db/search_api_db_defaults/config/optional/search_api.index.default_index.yml @@ -209,8 +209,10 @@ datasource_configs: bundles: article: '0' page: '0' -tracker: default -tracker_config: { } +tracker_settings: + 'default': + plugin_id: default + settings: { } server: default_server status: true langcode: en diff --git a/src/Entity/Index.php b/src/Entity/Index.php index 7011ab8..ee3dbba 100644 --- a/src/Entity/Index.php +++ b/src/Entity/Index.php @@ -62,8 +62,7 @@ use Drupal\views\Views; * "options", * "datasources", * "datasource_configs", - * "tracker", - * "tracker_config", + * "tracker_settings", * "server", * }, * links = { @@ -150,18 +149,11 @@ class Index extends ConfigEntityBase implements IndexInterface { protected $datasourcePlugins; /** - * The tracker plugin ID. - * - * @var string - */ - protected $tracker = 'default'; - - /** - * The tracker plugin configuration. + * The tracker settings * * @var array */ - protected $tracker_config = array(); + protected $tracker_settings = array('default' => array('plugin_id' => 'default', 'settings' => array())); /** * The tracker plugin instance. @@ -368,7 +360,12 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function getTrackerId() { - return $this->tracker; + if (is_null($this->tracker_settings)) { + return 'default'; + } + + $plugins = array_keys($this->tracker_settings); + return $this->tracker_settings[$plugins[0]]['plugin_id']; } /** @@ -376,9 +373,16 @@ class Index extends ConfigEntityBase implements IndexInterface { */ public function getTracker() { if (!$this->trackerPlugin) { - $tracker_plugin_configuration = array('index' => $this) + $this->tracker_config; + + if (is_null($this->tracker_settings)) { + $tracker_plugin_configuration = array('index' => $this); + } else { + $plugins = array_keys($this->tracker_settings); + $tracker_plugin_configuration = array('index' => $this) + $this->tracker_settings[$plugins[0]]['settings']; + } + if (!($this->trackerPlugin = \Drupal::service('plugin.manager.search_api.tracker')->createInstance($this->getTrackerId(), $tracker_plugin_configuration))) { - $args['@tracker'] = $this->tracker; + $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)); } @@ -1215,7 +1219,7 @@ class Index extends ConfigEntityBase implements IndexInterface { * The previous version of the index. */ protected function reactToTrackerSwitch(IndexInterface $original) { - if ($this->tracker != $original->getTrackerId()) { + if ($this->getTrackerId() != $original->getTrackerId()) { $index_task_manager = \Drupal::getContainer()->get('search_api.index_task_manager'); if ($original->hasValidTracker()) { $index_task_manager->stopTracking($this); @@ -1520,8 +1524,13 @@ class Index extends ConfigEntityBase implements IndexInterface { $this->datasource_configs = array_intersect_key($this->datasource_configs, $all_plugins['datasources']); // There always needs to be a tracker. if (empty($all_plugins['tracker'])) { - $this->tracker = \Drupal::config('search_api.settings')->get('default_tracker'); - $this->tracker_config = array(); + $default_tracker_id = \Drupal::config('search_api.settings')->get('default_tracker'); + $this->tracker_settings = array( + $default_tracker_id = array( + 'plugin_id' => $default_tracker_id, + 'settings' => array(), + ) + ); } // There also always needs to be a datasource, but here we have no easy way // out – if we had to remove all datasources, the operation fails. Return @@ -1541,7 +1550,7 @@ class Index extends ConfigEntityBase implements IndexInterface { $this->datasource_configs[$plugin_id] = $plugin_config; break; case 'tracker': - $this->tracker_config = $plugin_config; + $this->tracker_settings[$plugin_id]['settings'] = $plugin_config; break; } } diff --git a/src/Form/IndexForm.php b/src/Form/IndexForm.php index 2bfca64..069b01e 100644 --- a/src/Form/IndexForm.php +++ b/src/Form/IndexForm.php @@ -552,7 +552,12 @@ 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_config', $tracker->getConfiguration()); + $index->set('tracker_settings', array( + $tracker_id => array( + 'plugin_id' => $tracker_id, + 'settings' => $tracker->getConfiguration(), + ) + )); } /** diff --git a/tests/search_api_test_db/config/install/search_api.index.database_search_index.yml b/tests/search_api_test_db/config/install/search_api.index.database_search_index.yml index 8dc3b58..ceb0f9b 100644 --- a/tests/search_api_test_db/config/install/search_api.index.database_search_index.yml +++ b/tests/search_api_test_db/config/install/search_api.index.database_search_index.yml @@ -57,8 +57,10 @@ options: datasources: - 'entity:entity_test' datasource_configs: { } -tracker: default -tracker_config: { } +tracker_settings: + 'default': + plugin_id: default + settings: { } server: database_search_server status: true langcode: en diff --git a/tests/src/Kernel/DependencyRemovalTest.php b/tests/src/Kernel/DependencyRemovalTest.php index 61a7da3..dffe686 100644 --- a/tests/src/Kernel/DependencyRemovalTest.php +++ b/tests/src/Kernel/DependencyRemovalTest.php @@ -57,7 +57,12 @@ class DependencyRemovalTest extends KernelTestBase { $this->index = Index::create(array( 'id' => 'test_index', 'name' => 'Test index', - 'tracker' => 'default', + 'tracker_settings' => array( + 'default' => array( + 'plugin_id' => 'default', + 'settings' => array() + ) + ), 'datasources' => array( 'entity:user', ), @@ -336,11 +341,15 @@ class DependencyRemovalTest extends KernelTestBase { // server. $dependency_key = $this->dependency->getConfigDependencyKey(); $dependency_name = $this->dependency->getConfigDependencyName(); - $this->index->set('tracker', 'search_api_test_dependencies'); - $this->index->set('tracker_config', array( - $dependency_key => array( - $dependency_name, - ), + $this->index->set('tracker_settings', array( + 'search_api_test_dependencies' => array( + 'plugin_id' => 'search_api_test_dependencies', + 'settings' => array( + $dependency_key => array( + $dependency_name, + ), + ) + ) )); $this->index->save(); @@ -375,8 +384,10 @@ class DependencyRemovalTest extends KernelTestBase { // Depending on whether the plugin should have removed the dependency or // not, make sure the right action was taken. - $tracker = $this->index->get('tracker'); - $tracker_config = $this->index->get('tracker_config'); + $config = $this->index->get('tracker_settings'); + $plugins = array_keys($config); + $tracker = $config[$plugins[0]]['plugin_id']; + $tracker_config = $config[$plugins[0]]['settings']; if ($remove_dependency) { $this->assertEquals('search_api_test_dependencies', $tracker, 'Tracker not reset'); $this->assertEmpty($tracker_config, 'Tracker settings adapted');