You are here

public function SavedSearch::postSave in Search API Saved Searches 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 ContentEntityBase::postSave

File

src/Entity/SavedSearch.php, line 396

Class

SavedSearch
Provides an entity type for saved searches.

Namespace

Drupal\search_api_saved_searches\Entity

Code

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

  // For newly inserted saved searches with "Determine by result ID" detection
  // mode, prime the list of known results.
  if (!$update) {
    try {
      $type = $this
        ->getType();
    } catch (SavedSearchesException $e) {
      return;
    }
    $query = $this
      ->getQuery();
    if (!$query) {
      return;
    }
    $index_id = $query
      ->getIndex()
      ->id();
    $date_field = $type
      ->getOption("date_field.{$index_id}");
    if ($date_field) {
      return;
    }

    // Prime the "search_api_saved_searches_old_results" table with entries
    // for all current results. If we already have the executed version of
    // the query, we use that so we don't need to execute it again.
    $new_results_check = \Drupal::getContainer()
      ->get('search_api_saved_searches.new_results_check');
    if (!empty($this->cachedProperties['executed query'])) {

      /** @var \Drupal\search_api\Query\QueryInterface $query */
      $query = $this->cachedProperties['executed query'];
      $items = $query
        ->getResults()
        ->getResultItems();
      $new_results_check
        ->saveKnownResults($this, $items);
    }
    else {

      // Otherwise, just use the "new results check" code, which will
      // automatically save all results it encounters.
      $new_results_check
        ->getNewResults($this);
    }
  }
}