You are here

function entity_language_fallback_entity_delete in Entity Language Fallback 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).)

Note that this function implements tracking only on behalf of the "Content Entity" datasource defined in this module, not for entity-based datasources in general. Datasources defined by other modules still have to implement their own mechanism for tracking new/updated/deleted entities.

See also

\Drupal\search_api\Plugin\search_api\datasource\ContentEntity

File

./entity_language_fallback.module, line 204
Add fallback languages to entities.

Code

function entity_language_fallback_entity_delete(EntityInterface $entity) {
  if (!\Drupal::moduleHandler()
    ->moduleExists('search_api')) {
    return;
  }

  // Check if the entity is a content entity.
  if (!$entity instanceof ContentEntityInterface || $entity->search_api_skip_tracking) {
    return;
  }
  $indexes = ContentEntityFallback::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);
  }
}