You are here

function entity_language_fallback_entity_insert in Entity Language Fallback 8

Implements hook_entity_insert().

Modified version of search_api_entity_insert().

See also

search_api_entity_insert()

File

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

Code

function entity_language_fallback_entity_insert(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;
  }

  // Compute the item IDs for all languages set up on the language fallback chain.
  $item_ids = [];
  $entity_id = $entity
    ->id();
  $fallback_controller = \Drupal::service('language_fallback.controller');
  $fallback_languages = array_keys($fallback_controller
    ->getTranslations($entity));
  foreach ($fallback_languages as $langcode) {
    $item_ids[] = $entity_id . ':' . $langcode;
  }
  $datasource_id = 'entity_language_fallback:' . $entity
    ->getEntityTypeId();
  foreach ($indexes as $index) {
    $filtered_item_ids = ContentEntityFallback::filterValidItemIds($index, $datasource_id, $item_ids);
    $index
      ->trackItemsInserted($datasource_id, $filtered_item_ids);
  }
}