diff --git a/src/Entity/Index.php b/src/Entity/Index.php index 6fc0752..fb6bb03 100644 --- a/src/Entity/Index.php +++ b/src/Entity/Index.php @@ -7,6 +7,7 @@ namespace Drupal\search_api\Entity; +use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Cache\Cache; use Drupal\Core\Config\Entity\ConfigEntityBase; @@ -155,7 +156,7 @@ class Index extends ConfigEntityBase implements IndexInterface { * * @see getTrackerInstance() */ - protected $trackerInstance = FALSE; + protected $trackerInstance = NULL; /** * The ID of the server on which data should be indexed. @@ -352,8 +353,8 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function hasValidTracker() { - $tracker = $this->getTrackerInstance(); - return ($tracker instanceof TrackerInterface); + return (bool) \Drupal::service('plugin.manager.search_api.tracker') + ->getDefinition($this->getTrackerId(), FALSE); } /** @@ -361,10 +362,10 @@ class Index extends ConfigEntityBase implements IndexInterface { */ public function getTrackerId() { $settings = $this->getTrackerSettings(); - if ($settings === array()) { - return 'default'; + if (!$settings) { + return \Drupal::config('search_api.settings')->get('default_tracker'); } - $tracker_settings = array_values($settings)[0]; + $tracker_settings = reset($settings); return $tracker_settings['plugin_id']; } @@ -372,10 +373,6 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function getTrackerInstance() { - if ($this->getTrackerSettings() === NULL) { - return NULL; - } - if (!$this->trackerInstance) { if ($this->getTrackerSettings() === array()) { $tracker_settings = array( @@ -507,15 +504,19 @@ class Index extends ConfigEntityBase implements IndexInterface { $processor_settings = $this->getProcessorSettings(); foreach ($processor_plugin_manager->getDefinitions() as $name => $processor_definition) { - if (class_exists($processor_definition['class']) && empty($this->processorInstances[$name])) { + if (empty($this->processorInstances[$name])) { // Create our settings for this processor. $settings = empty($processor_settings[$name]['settings']) ? array() : $processor_settings[$name]['settings']; $settings['index'] = $this; /** @var $processor \Drupal\search_api\Processor\ProcessorInterface */ - $processor = $processor_plugin_manager->createInstance($name, $settings); - if ($processor->supportsIndex($this)) { - $this->processorInstances[$name] = $processor; + try { + $processor = $processor_plugin_manager->createInstance($name, $settings); + if ($processor->supportsIndex($this)) { + $this->processorInstances[$name] = $processor; + } + } catch (PluginException $e) { + \Drupal::logger('search_api')->warning('Processor @id specifies a non-existing @class.', array('@id' => $name, '@class' => $processor_definition['class'])); } } elseif (!class_exists($processor_definition['class'])) { @@ -802,16 +803,13 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function indexSpecificItems(array $search_objects) { - if (!$search_objects) { - return array(); - } - if ($this->isReadOnly()) { + if (!$search_objects || $this->isReadOnly()) { return array(); } if (!$this->status()) { throw new SearchApiException(new FormattableMarkup("Couldn't index values on index %index (index is disabled)", array('%index' => $this->label()))); } - if (empty($this->getFields())) { + if (!$this->getFields()) { throw new SearchApiException(new FormattableMarkup("Couldn't index values on index %index (no fields selected)", array('%index' => $this->label()))); } diff --git a/src/Form/IndexForm.php b/src/Form/IndexForm.php index 3885dfd..accb9b0 100644 --- a/src/Form/IndexForm.php +++ b/src/Form/IndexForm.php @@ -262,7 +262,7 @@ class IndexForm extends EntityForm { '#title' => $this->t('Tracker'), '#description' => $this->t('Select the type of tracker which should be used for keeping track of item changes.'), '#options' => $this->getTrackerPluginManager()->getOptionsList(), - '#default_value' => !$index->isNew() && $index->hasValidTracker() ? $index->getTrackerInstance()->getPluginId() : key($tracker_options), + '#default_value' => $index->hasValidTracker() ? $index->getTrackerId() : key($tracker_options), '#required' => TRUE, '#disabled' => !$index->isNew(), '#ajax' => array( diff --git a/tests/src/Kernel/LanguageKernelTest.php b/tests/src/Kernel/LanguageKernelTest.php index b555e77..dacdc3e 100644 --- a/tests/src/Kernel/LanguageKernelTest.php +++ b/tests/src/Kernel/LanguageKernelTest.php @@ -104,12 +104,23 @@ class LanguageKernelTest extends KernelTestBase { $this->server->save(); // Create a test index. + $datasource_id = 'entity:' . $this->testEntityTypeId; $this->index = Index::create(array( 'name' => 'Test Index', 'id' => 'test_index', 'status' => 1, - 'datasources' => array('entity:' . $this->testEntityTypeId), - 'tracker' => 'default', + 'datasource_settings' => array( + $datasource_id => array( + 'plugin_id' => $datasource_id, + 'settings' => array(), + ), + ), + 'tracker_settings' => array( + 'default'=> array( + 'plugin_id' => 'default', + 'settings' => array(), + ), + ), 'server' => $this->server->id(), 'options' => array('index_directly' => FALSE), ));