You are here

public function Index::postSave in Search API 8

Acts on a saved entity before the insert or update hook is invoked.

Used after the entity is saved, but before invoking the insert or update hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

bool $update: TRUE if the entity has been updated, or FALSE if it has been inserted.

Overrides EntityBase::postSave

File

src/Entity/Index.php, line 1369

Class

Index
Defines the search index configuration entity.

Namespace

Drupal\search_api\Entity

Code

public function postSave(EntityStorageInterface $storage, $update = TRUE) {
  parent::postSave($storage, $update);

  // New indexes don't have any items indexed.
  if (!$update) {
    $this
      ->setHasReindexed();
  }
  try {

    // Fake an original for inserts to make code cleaner.

    /** @var \Drupal\search_api\IndexInterface $original */
    $original = $update ? $this->original : static::create([
      'status' => FALSE,
    ]);
    $index_task_manager = \Drupal::getContainer()
      ->get('search_api.index_task_manager');
    if ($this
      ->status() && $original
      ->status()) {

      // React on possible changes that would require re-indexing, etc.
      $this
        ->reactToServerSwitch($original);
      $this
        ->reactToDatasourceSwitch($original);
      $this
        ->reactToTrackerSwitch($original);
      $this
        ->reactToProcessorChanges($original);
    }
    elseif (!$this
      ->status() && $original
      ->status()) {
      if ($this
        ->hasValidTracker()) {
        $index_task_manager
          ->stopTracking($original);
      }
      if ($original
        ->isServerEnabled()) {
        $original
          ->getServerInstance()
          ->removeIndex($original);
      }
    }
    elseif ($this
      ->status() && !$original
      ->status()) {
      $this
        ->getServerInstance()
        ->addIndex($this);
      if ($this
        ->hasValidTracker()) {
        $index_task_manager
          ->startTracking($this);
      }
    }
    if (!$index_task_manager
      ->isTrackingComplete($this)) {

      // Give tests and site admins the possibility to disable the use of a
      // batch for tracking items. Also, do not use a batch if running in the
      // CLI.
      $use_batch = \Drupal::state()
        ->get('search_api_use_tracking_batch', TRUE);
      if (!$use_batch || Utility::isRunningInCli()) {
        $index_task_manager
          ->addItemsAll($this);
      }
      elseif (!defined('MAINTENANCE_MODE') || !in_array(MAINTENANCE_MODE, [
        'install',
        'update',
      ])) {
        $index_task_manager
          ->addItemsBatch($this);
      }
    }
    if (\Drupal::moduleHandler()
      ->moduleExists('views')) {
      Views::viewsData()
        ->clear();

      // Remove this line when https://www.drupal.org/node/2370365 gets fixed.
      Cache::invalidateTags([
        'extension:views',
      ]);
      \Drupal::cache('discovery')
        ->delete('views:wizard');
    }
    Cache::invalidateTags($this
      ->getCacheTags());
    $this->properties = [];
  } catch (SearchApiException $e) {
    $this
      ->logException($e);
  }
}