You are here

public function FallbackLanguage::alterIndexedItems in Entity Language Fallback 8

Alter the items to be indexed.

Parameters

\Drupal\search_api\Item\ItemInterface[] $items: An array of items to be indexed, passed by reference.

Overrides ProcessorPluginBase::alterIndexedItems

File

src/Plugin/search_api/processor/FallbackLanguage.php, line 37

Class

FallbackLanguage
Excludes unpublished nodes from node indexes.

Namespace

Drupal\entity_language_fallback\Plugin\search_api\processor

Code

public function alterIndexedItems(array &$items) {

  // Annoyingly, this doc comment is needed for PHPStorm. See
  // http://youtrack.jetbrains.com/issue/WI-23586

  /** @var \Drupal\search_api\Item\ItemInterface $item */
  foreach ($items as $item_id => $item) {
    $object = $item
      ->getOriginalObject();
    $entity = $object
      ->getValue();
    if (!$entity instanceof ContentEntityInterface || !$entity
      ->isTranslatable()) {
      continue;
    }

    // Only add missing translations to the source language item.
    $entity_lang = $entity
      ->language()
      ->getId();
    if (!$entity
      ->hasField('content_translation_source') || !in_array($entity->content_translation_source->value, [
      $entity_lang,
      LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ])) {
      continue;
    }
    foreach ($this->languages as $langcode => $language) {
      if ($entity
        ->hasTranslation($langcode)) {
        continue;
      }
      $fallback_chain = $language
        ->getThirdPartySetting('entity_language_fallback', 'fallback_langcodes', []);
      $fallback_found = FALSE;
      foreach ($fallback_chain as $candidate) {
        if ($entity
          ->hasTranslation($candidate)) {
          $fallback_found = TRUE;
          break;
        }
      }
      if ($fallback_found) {
        $entity = $entity
          ->getTranslation($candidate);
        $new_key = 'entity:' . $entity
          ->getEntityType()
          ->id() . '/' . $entity
          ->id() . ':' . $candidate;
        $new_item = new Item($item
          ->getIndex(), $new_key, $item
          ->getDatasource());
        $object
          ->setValue($entity);
        $new_item
          ->setOriginalObject($object);
        $new_item
          ->setLanguage($langcode);
        $items[$new_key . $langcode] = $new_item;
      }
    }
  }
}