You are here

protected function Index::reactToDatasourceSwitch in Search API 8

Checks whether the index's datasources changed 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.

Parameters

\Drupal\search_api\IndexInterface $original: The previous version of the index.

1 call to Index::reactToDatasourceSwitch()
Index::postSave in src/Entity/Index.php
Acts on a saved entity before the insert or update hook is invoked.

File

src/Entity/Index.php, line 1475

Class

Index
Defines the search index configuration entity.

Namespace

Drupal\search_api\Entity

Code

protected function reactToDatasourceSwitch(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(), '::reactToDatasourceSwitch should only be called when the index is enabled');
  $new_datasource_ids = $this
    ->getDatasourceIds();
  $original_datasource_ids = $original
    ->getDatasourceIds();
  if ($new_datasource_ids != $original_datasource_ids) {
    $added = array_diff($new_datasource_ids, $original_datasource_ids);
    $removed = array_diff($original_datasource_ids, $new_datasource_ids);
    $index_task_manager = \Drupal::getContainer()
      ->get('search_api.index_task_manager');
    $index_task_manager
      ->stopTracking($this, $removed);
    if ($this
      ->hasValidServer()) {

      /** @var \Drupal\search_api\ServerInterface $server */
      $server = $this
        ->getServerInstance();
      foreach ($removed as $datasource_id) {
        $server
          ->deleteAllIndexItems($this, $datasource_id);
      }
    }
    $index_task_manager
      ->startTracking($this, $added);
  }
}