/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); /** @var $index \Drupal\search_api\IndexInterface */ $index = $this->getEntity(); $index->setOptions($form_state->getValue('options', array()) + $this->originalEntity->getOptions()); $datasources = $form_state->getValue('datasources', array()); /** @var \Drupal\search_api\Datasource\DatasourceInterface[] $datasource_plugins */ $datasource_plugins = $this->originalEntity->getDatasources(FALSE); $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_settings[$datasource_id] = $datasource; } $index->setDatasources($datasource_settings); // Call submitConfigurationForm() for the (possibly new) tracker. // @todo It seems if we change the tracker, we would validate/submit the old // tracker's form using the new tracker. Shouldn't be done, of course. // Similar above for datasources, though there of course the values will // just always be empty (because datasources have their plugin ID in the // form structure). $tracker_id = $form_state->getValue('tracker', NULL); if ($this->originalEntity->getTrackerId() == $tracker_id) { $tracker = $this->originalEntity->getTrackerInstance(); } else { $tracker = $this->trackerPluginManager->createInstance($tracker_id, array('index' => $this->originalEntity)); } $tracker_form_state = new SubFormState($form_state, array('tracker_config')); $tracker->submitConfigurationForm($form['tracker_config'], $tracker_form_state); $index->setTracker($tracker); }
/** * Tests whether module dependencies are handled correctly. */ public function testModuleDependency() { // Test with all types of plugins at once. $datasources['search_api_test_dependencies'] = \Drupal::getContainer()->get('plugin.manager.search_api.datasource')->createInstance('search_api_test_dependencies', array('index' => $this->index)); $datasources['entity:user'] = \Drupal::getContainer()->get('plugin.manager.search_api.datasource')->createInstance('entity:user', array('index' => $this->index)); $this->index->setDatasources($datasources); $processor = \Drupal::getContainer()->get('plugin.manager.search_api.processor')->createInstance('search_api_test_dependencies'); $this->index->addProcessor($processor); $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. $dependencies = $this->index->getDependencies(); $this->assertContains('search_api_test_dependencies', $dependencies['module'], 'Module dependency correctly inserted'); // When the index resets the tracker, it needs to know the ID of the default // tracker. \Drupal::configFactory()->getEditable('search_api.settings')->set('default_tracker', 'default')->save(); // Disabling modules in Kernel tests normally doesn't trigger any kind of // reaction, just removes it from the list of modules (e.g., to avoid // calling of a hook). Therefore, we have to trigger that behavior // ourselves. \Drupal::getContainer()->get('config.manager')->uninstall('module', 'search_api_test_dependencies'); // Reload the index and check it's still there. $this->reloadIndex(); $this->assertInstanceOf('Drupal\\search_api\\IndexInterface', $this->index, 'Index not removed'); // Make sure the dependency has been removed. $dependencies = $this->index->getDependencies(); $dependencies += array('module' => array()); $this->assertNotContains('search_api_test_dependencies', $dependencies['module'], 'Module dependency removed from index'); // Make sure all the plugins have been removed. $this->assertNotContains('search_api_test_dependencies', $this->index->getDatasources(), 'Datasource was removed'); $this->assertArrayNotHasKey('search_api_test_dependencies', $this->index->getProcessors(), 'Processor was removed'); $this->assertEquals('default', $this->index->getTrackerId(), 'Tracker was reset'); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); /** @var $index \Drupal\search_api\IndexInterface */ $index = $this->getEntity(); $index->setOptions($form_state->getValue('options', array()) + $this->originalEntity->getOptions()); $datasources = $form_state->getValue('datasources', array()); /** @var \Drupal\search_api\Datasource\DatasourceInterface[] $datasource_plugins */ $datasource_plugins = $this->originalEntity->getDatasources(FALSE); $datasource_configuration = 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(); } $index->set('datasource_configs', $datasource_configuration); // Call submitConfigurationForm() for the (possibly new) tracker. // @todo It seems if we change the tracker, we would validate/submit the old // tracker's form using the new tracker. Shouldn't be done, of course. // Similar above for datasources, though there of course the values will // just always be empty (because datasources have their plugin ID in the // form structure). $tracker_id = $form_state->getValue('tracker', NULL); if ($this->originalEntity->getTrackerId() == $tracker_id) { $tracker = $this->originalEntity->getTracker(); } else { $tracker = $this->trackerPluginManager->createInstance($tracker_id, array('index' => $this->originalEntity)); } $tracker_form_state = new SubFormState($form_state, array('tracker_config')); $tracker->submitConfigurationForm($form['tracker_config'], $tracker_form_state); $index->set('tracker_config', $tracker->getConfiguration()); // Invalidate caches, so this gets picked up by the views wizard. Cache::invalidateTags(array('views_data')); // Remove this line when https://www.drupal.org/node/2370365 gets fixed. Cache::invalidateTags(array('extension:views')); }
/** * Checks whether the index switched tracker plugin and reacts accordingly. * * Used as a helper method in postSave(). Should only be called when the index * was enabled before the change and remained so. * * @param \Drupal\search_api\IndexInterface $original * The previous version of the index. */ protected function reactToTrackerSwitch(IndexInterface $original) { // Asserts that the index was enabled before saving and will still be // enabled afterwards. Otherwise, this method should not be called. assert('$this->status() && $original->status()', '::reactToTrackerSwitch should only be called when the index is enabled'); if ($this->getTrackerId() != $original->getTrackerId()) { $index_task_manager = \Drupal::getContainer()->get('search_api.index_task_manager'); if ($original->hasValidTracker()) { $index_task_manager->stopTracking($this); } if ($this->hasValidTracker()) { $index_task_manager->startTracking($this); } } }
/** * Checks whether the index switched tracker plugin and reacts accordingly. * * Used as a helper method in postSave(). Should only be called when the index * was enabled before the change and remained so. * * @param \Drupal\search_api\IndexInterface $original * The previous version of the index. */ protected function reactToTrackerSwitch(IndexInterface $original) { if ($this->tracker != $original->getTrackerId()) { $index_task_manager = Utility::getIndexTaskManager(); if ($original->hasValidTracker()) { $index_task_manager->stopTracking($this); } if ($this->hasValidTracker()) { $index_task_manager->startTracking($this); } } }
/** * Checks whether the index switched tracker plugin and reacts accordingly. * * Used as a helper method in postSave(). Should only be called when the index * was enabled before the change and remained so. * * @param \Drupal\search_api\IndexInterface $original * The previous version of the index. */ protected function reactToTrackerSwitch(IndexInterface $original) { if ($this->tracker != $original->getTrackerId()) { if ($original->hasValidTracker()) { $original->stopTracking(); } if ($this->hasValidTracker()) { $this->startTracking(); } } }
/** * {@inheritdoc} */ public function getTrackerId() { return $this->entity->getTrackerId(); }