diff --git a/config/schema/search_api.index.schema.yml b/config/schema/search_api.index.schema.yml index 4391420..daa2fc1 100644 --- a/config/schema/search_api.index.schema.yml +++ b/config/schema/search_api.index.schema.yml @@ -76,17 +76,18 @@ search_api.index.*: index_directly: type: boolean label: 'Index items immediately' - datasources: + datasource_settings: type: sequence label: 'Datasource plugin IDs' sequence: - type: string - datasource_configs: - type: sequence - label: 'Datasource plugin configurations' - sequence: - type: plugin.plugin_configuration.search_api_datasource.[%key] - label: 'Datasource plugin configuration' + type: mapping + label: 'A datasource' + mapping: + plugin_id: + type: string + label: 'The plugin ID of the datasource' + settings: + type: plugin.plugin_configuration.search_api_datasource.[%parent.plugin_id] tracker_settings: type: sequence label: 'Tracker settings' 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 436e004..4587ca0 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 @@ -202,14 +202,14 @@ processors: options: index_directly: true cron_limit: 50 -datasources: - - 'entity:node' -datasource_configs: +datasource_settings: 'entity:node': - default: '1' - bundles: - article: '0' - page: '0' + plugin_id: 'entity:node' + settings: + default: '1' + bundles: + article: '0' + page: '0' tracker_settings: 'default': plugin_id: default diff --git a/src/Entity/Index.php b/src/Entity/Index.php index d031ab4..044418e 100644 --- a/src/Entity/Index.php +++ b/src/Entity/Index.php @@ -60,8 +60,7 @@ use Drupal\views\Views; * "field_settings", * "processors", * "options", - * "datasources", - * "datasource_configs", + * "datasource_settings", * "tracker_settings", * "server", * }, @@ -126,18 +125,11 @@ class Index extends ConfigEntityBase implements IndexInterface { protected $options = array(); /** - * The IDs of the datasources selected for this index. + * The settings of the datasources selected for this index. * * @var string[] */ - protected $datasources = array(); - - /** - * The configuration for the selected datasources. - * - * @var array - */ - protected $datasource_configs = array(); + protected $datasource_settings = array(); /** * The instantiated datasource plugins. @@ -293,7 +285,7 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function getDatasourceIds() { - return $this->datasources; + return array_keys($this->datasource_settings); } /** @@ -329,7 +321,7 @@ class Index extends ConfigEntityBase implements IndexInterface { foreach ($datasource_plugin_manager->getDefinitions() as $name => $datasource_definition) { if (class_exists($datasource_definition['class']) && empty($this->datasourcePlugins[$name])) { // Create our settings for this datasource. - $config = isset($this->datasource_configs[$name]) ? $this->datasource_configs[$name] : array(); + $config = isset($this->datasource_settings[$name]) ? $this->datasource_settings[$name]['settings'] : array(); $config += array('index' => $this); /** @var $datasource \Drupal\search_api\Datasource\DatasourceInterface */ @@ -346,7 +338,7 @@ class Index extends ConfigEntityBase implements IndexInterface { if (!$only_enabled) { return $this->datasourcePlugins; } - return array_intersect_key($this->datasourcePlugins, array_flip($this->datasources)); + return array_intersect_key($this->datasourcePlugins, array_flip(array_keys($this->datasource_settings))); } /** @@ -661,7 +653,7 @@ class Index extends ConfigEntityBase implements IndexInterface { public function getFieldsByDatasource($datasource_id) { $datasource_fields = $this->getCache(__FUNCTION__); if (!$datasource_fields) { - $datasource_fields = array_fill_keys($this->datasources, array()); + $datasource_fields = array_fill_keys(array_keys($this->datasource_settings), array()); $datasource_fields[NULL] = array(); foreach ($this->getFields() as $field_id => $field) { $datasource_fields[$field->getDatasourceId()][$field_id] = $field; @@ -1526,8 +1518,15 @@ class Index extends ConfigEntityBase implements IndexInterface { // according to plugin type, unfortunately. // First, remove plugins that need to be removed. $this->processors = array_intersect_key($this->processors, $all_plugins['processors']); - $this->datasources = array_keys($all_plugins['datasources']); - $this->datasource_configs = array_intersect_key($this->datasource_configs, $all_plugins['datasources']); + $new_datasources = array(); + foreach ($all_plugins['datasources'] as $plugin_id => $settings) { + $new_datasources[$plugin_id] = array( + 'plugin_id' => $plugin_id, + 'settings' => $settings, + ); + } + $this->datasource_settings = array_intersect_key($this->datasource_settings, $new_datasources); + // There always needs to be a tracker. if (empty($all_plugins['tracker'])) { $default_tracker_id = \Drupal::config('search_api.settings')->get('default_tracker'); @@ -1541,7 +1540,7 @@ class Index extends ConfigEntityBase implements IndexInterface { // 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 // FALSE to indicate this, which will cause the index to be deleted. - if (!$this->datasources) { + if (!$this->datasource_settings) { return FALSE; } @@ -1553,7 +1552,7 @@ class Index extends ConfigEntityBase implements IndexInterface { $this->processors[$plugin_id]['settings'] = $plugin_config; break; case 'datasources': - $this->datasource_configs[$plugin_id] = $plugin_config; + $this->datasource_settings[$plugin_id]['settings'] = $plugin_config; break; case 'tracker': $this->tracker_settings[$plugin_id]['settings'] = $plugin_config; diff --git a/src/Form/IndexForm.php b/src/Form/IndexForm.php index a6f2e04..93d8358 100644 --- a/src/Form/IndexForm.php +++ b/src/Form/IndexForm.php @@ -526,15 +526,17 @@ class IndexForm extends EntityForm { $datasources = $form_state->getValue('datasources', array()); /** @var \Drupal\search_api\Datasource\DatasourceInterface[] $datasource_plugins */ $datasource_plugins = $this->originalEntity->getDatasources(FALSE); - $datasource_configuration = array(); + $datasource_settings = array(); foreach ($datasources as $datasource_id) { $datasource = $datasource_plugins[$datasource_id]; $datasource_form = (!empty($form['datasource_configs'][$datasource_id])) ? $form['datasource_configs'][$datasource_id] : array(); $datasource_form_state = new SubFormState($form_state, array('datasource_configs', $datasource_id)); $datasource->submitConfigurationForm($datasource_form, $datasource_form_state); - $datasource_configuration[$datasource_id] = $datasource->getConfiguration(); + + $datasource_settings[$datasource_id]['plugin_id'] = $datasource_id; + $datasource_settings[$datasource_id]['settings'] = $datasource->getConfiguration(); } - $index->set('datasource_configs', $datasource_configuration); + $index->set('datasource_settings', $datasource_settings); // Call submitConfigurationForm() for the (possibly new) tracker. // @todo It seems if we change the tracker, we would validate/submit the old diff --git a/src/Tests/Processor/ContentAccessTest.php b/src/Tests/Processor/ContentAccessTest.php index b301cd4..89aa933 100644 --- a/src/Tests/Processor/ContentAccessTest.php +++ b/src/Tests/Processor/ContentAccessTest.php @@ -109,7 +109,23 @@ class ContentAccessTest extends ProcessorTestBase { $this->nodes[2]->save(); // Also index users, to verify that they are unaffected by the processor. - $this->index->set('datasources', array('entity:comment', 'entity:node', 'entity:user')); + $this->index->set( + 'datasource_settings', + array( + 'entity:comment' => array( + 'plugin_id' => 'entity:comment', + 'settings' => array(), + ), + 'entity:node' => array( + 'plugin_id' => 'entity:node', + 'settings' => array(), + ), + 'entity:user' => array( + 'plugin_id' => 'entity:user', + 'settings' => array(), + ), + ) + ); $this->index->save(); \Drupal::getContainer()->get('search_api.index_task_manager')->addItemsAll($this->index); diff --git a/src/Tests/Processor/ProcessorIntegrationTest.php b/src/Tests/Processor/ProcessorIntegrationTest.php index ad617c8..24ea7d4 100644 --- a/src/Tests/Processor/ProcessorIntegrationTest.php +++ b/src/Tests/Processor/ProcessorIntegrationTest.php @@ -32,7 +32,12 @@ class ProcessorIntegrationTest extends WebTestBase { 'name' => 'Test index', 'id' => $this->indexId, 'status' => 1, - 'datasources' => array('entity:node'), + 'datasource_settings' => array( + 'entity:node' => array( + 'plugin_id' => 'entity:user', + 'settings' => array(), + ), + ), ))->save(); } diff --git a/src/Tests/Processor/ProcessorTestBase.php b/src/Tests/Processor/ProcessorTestBase.php index 2809c31..a9ccdeb 100644 --- a/src/Tests/Processor/ProcessorTestBase.php +++ b/src/Tests/Processor/ProcessorTestBase.php @@ -87,7 +87,16 @@ abstract class ProcessorTestBase extends EntityUnitTestBase { 'id' => 'index', 'name' => 'Index name', 'status' => TRUE, - 'datasources' => array('entity:comment', 'entity:node'), + 'datasource_settings' => array( + 'entity:comment' => array( + 'plugin_id' => 'entity:comment', + 'settings' => array(), + ), + 'entity:node' => array( + 'plugin_id' => 'entity:node', + 'settings' => array(), + ), + ), 'server' => 'server', 'tracker' => 'default', )); diff --git a/src/Tests/WebTestBase.php b/src/Tests/WebTestBase.php index 8b85ea6..4dd9113 100644 --- a/src/Tests/WebTestBase.php +++ b/src/Tests/WebTestBase.php @@ -169,7 +169,12 @@ abstract class WebTestBase extends SimpletestWebTestBase { 'name' => $name, 'description' => $name . ' description', 'server' => $server_id, - 'datasources' => array($datasource_id), + 'datasource_settings' => array( + $datasource_id => array( + 'plugin_id' => $datasource_id, + 'settings' => array() + ) + ), )); $index->save(); $this->indexId = $index->id(); 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 ceb0f9b..04cea6b 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 @@ -54,9 +54,10 @@ processors: options: cron_limit: -1 index_directly: false -datasources: - - 'entity:entity_test' -datasource_configs: { } +datasource_settings: + 'entity:entity_test': + plugin_id: 'entity:entity_test' + settings: { } tracker_settings: 'default': plugin_id: default diff --git a/tests/src/Kernel/CliTest.php b/tests/src/Kernel/CliTest.php index bba9fed..95c3395 100644 --- a/tests/src/Kernel/CliTest.php +++ b/tests/src/Kernel/CliTest.php @@ -94,7 +94,12 @@ class CliTest extends KernelTestBase { 'name' => 'Test index', 'id' => 'index', 'status' => 1, - 'datasources' => array('entity:entity_test'), + 'datasource_settings' => array( + 'entity:entity_test' => array( + 'plugin_id' => 'entity:entity_test', + 'settings' => array(), + ), + ), 'tracker' => 'default', 'server' => $this->server->id(), 'options' => array('index_directly' => TRUE), diff --git a/tests/src/Kernel/DependencyRemovalTest.php b/tests/src/Kernel/DependencyRemovalTest.php index 18f641d..8daef81 100644 --- a/tests/src/Kernel/DependencyRemovalTest.php +++ b/tests/src/Kernel/DependencyRemovalTest.php @@ -63,8 +63,11 @@ class DependencyRemovalTest extends KernelTestBase { 'settings' => array() ) ), - 'datasources' => array( - 'entity:user', + 'datasource_settings' => array( + 'entity:user' => array( + 'plugin_id' => 'entity:user', + 'settings' => array(), + ) ), )); @@ -178,16 +181,21 @@ class DependencyRemovalTest extends KernelTestBase { // server. $dependency_key = $this->dependency->getConfigDependencyKey(); $dependency_name = $this->dependency->getConfigDependencyName(); - $this->index->set('datasources', array( - 'entity:user', - 'search_api_test_dependencies', - )); - $this->index->set('datasource_configs', array( - 'search_api_test_dependencies' => array( - $dependency_key => array( - $dependency_name, + $this->index->set( + 'datasource_settings', + array( + 'entity:user' => array( + 'plugin_id' => 'entity:user', + 'settings' => array(), + ), + 'search_api_test_dependencies' => array( + 'plugin_id' => 'search_api_test_dependencies', + 'settings' => array( + $dependency_key => array( + $dependency_name, + ), + ), ), - ), )); $this->index->save(); @@ -215,15 +223,14 @@ class DependencyRemovalTest extends KernelTestBase { // Depending on whether the plugin should have removed the dependency or // not, make sure the right action was taken. - $datasources = $this->index->get('datasources'); - $datasource_configs = $this->index->get('datasource_configs'); + $datasources = $this->index->get('datasource_settings'); if ($remove_dependency) { - $this->assertContains('search_api_test_dependencies', $datasources, 'Datasource not removed'); - $this->assertEmpty($datasource_configs['search_api_test_dependencies'], 'Datasource settings adapted'); + $this->assertContains('search_api_test_dependencies', array_keys($datasources), 'Datasource not removed'); + $this->assertEmpty($datasources['search_api_test_dependencies']['settings'], 'Datasource settings adapted'); } else { - $this->assertNotContains('search_api_test_dependencies', $datasources, 'Datasource removed'); - $this->assertArrayNotHasKey('search_api_test_dependencies', $datasource_configs, 'Datasource config removed'); + $this->assertNotContains('search_api_test_dependencies', array_keys($datasources), 'Datasource removed'); + $this->assertArrayNotHasKey('search_api_test_dependencies', $datasources, 'Datasource config removed'); } } @@ -236,15 +243,13 @@ class DependencyRemovalTest extends KernelTestBase { // server. $dependency_key = $this->dependency->getConfigDependencyKey(); $dependency_name = $this->dependency->getConfigDependencyName(); - $this->index->set('datasources', array( - 'search_api_test_dependencies', - )); - $this->index->set('datasource_configs', array( + $this->index->set('datasource_settings', array( 'search_api_test_dependencies' => array( - $dependency_key => array( - $dependency_name, + 'plugin_id' => 'search_api_test_dependencies', + 'settings' => array( + $dependency_key => array($dependency_name) ), - ), + ) )); $this->index->save(); @@ -403,9 +408,15 @@ class DependencyRemovalTest extends KernelTestBase { */ public function testModuleDependency() { // Test with all types of plugins at once. - $this->index->set('datasources', array( - 'entity:user', - 'search_api_test_dependencies', + $this->index->set('datasource_settings', array( + 'entity:user' => array( + 'plugin_id' => 'entity:user', + 'settings' => array(), + ), + 'search_api_test_dependencies' => array( + 'plugin_id' => 'search_api_test_dependencies', + 'settings' => array(), + ), )); $this->index->set('processors', array( 'search_api_test_dependencies' => array( diff --git a/tests/src/Kernel/LanguageKernelTest.php b/tests/src/Kernel/LanguageKernelTest.php index b555e77..72f4a28 100644 --- a/tests/src/Kernel/LanguageKernelTest.php +++ b/tests/src/Kernel/LanguageKernelTest.php @@ -108,7 +108,12 @@ class LanguageKernelTest extends KernelTestBase { 'name' => 'Test Index', 'id' => 'test_index', 'status' => 1, - 'datasources' => array('entity:' . $this->testEntityTypeId), + 'datasource_settings' => array( + 'entity:' . $this->testEntityTypeId => array( + 'plugin_id' => 'entity:' . $this->testEntityTypeId, + 'settings' => array(), + ), + ), 'tracker' => 'default', 'server' => $this->server->id(), 'options' => array('index_directly' => FALSE), diff --git a/tests/src/Kernel/ServerTaskTest.php b/tests/src/Kernel/ServerTaskTest.php index a31595d..e83a87f 100644 --- a/tests/src/Kernel/ServerTaskTest.php +++ b/tests/src/Kernel/ServerTaskTest.php @@ -91,7 +91,12 @@ class ServerTaskTest extends KernelTestBase { 'name' => 'Test index', 'id' => 'test_index', 'status' => 1, - 'datasources' => array('entity:user'), + 'datasource_settings' => array( + 'entity:user' => array( + 'plugin_id' => 'entity:user', + 'settings' => array(), + ), + ), 'tracker' => 'default', 'server' => $this->server->id(), 'options' => array('index_directly' => FALSE),