You are here

public function ContentEntityTrackingManager::entityDelete in Search API 8

Implements hook_entity_delete().

Deletes all entries for this entity from the tracking table for each index that tracks this entity type.

By setting the $entity->search_api_skip_tracking property to a true-like value before this hook is invoked, you can prevent this behavior and make the Search API ignore this deletion. (Note that this might lead to stale data in the tracking table or on the server, since the item will not removed from there (if it has been added before).)

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The deleted entity.

See also

search_api_entity_delete()

File

src/Plugin/search_api/datasource/ContentEntityTrackingManager.php, line 189

Class

ContentEntityTrackingManager
Provides hook implementations on behalf of the Content Entity datasource.

Namespace

Drupal\search_api\Plugin\search_api\datasource

Code

public function entityDelete(EntityInterface $entity) {

  // Check if the entity is a content entity.
  if (!$entity instanceof ContentEntityInterface || !empty($entity->search_api_skip_tracking)) {
    return;
  }
  $indexes = $this
    ->getIndexesForEntity($entity);
  if (!$indexes) {
    return;
  }

  // Remove the search items for all the entity's translations.
  $item_ids = [];
  $entity_id = $entity
    ->id();
  foreach (array_keys($entity
    ->getTranslationLanguages()) as $langcode) {
    $item_ids[] = $entity_id . ':' . $langcode;
  }
  $datasource_id = 'entity:' . $entity
    ->getEntityTypeId();
  foreach ($indexes as $index) {
    $index
      ->trackItemsDeleted($datasource_id, $item_ids);
  }
}