You are here

function apachesolr_multilingual_apachesolr_index_documents_alter in Apache Solr Multilingual 6.3

Same name and namespace in other branches
  1. 7 apachesolr_multilingual.module \apachesolr_multilingual_apachesolr_index_documents_alter()

Implements hook_apachesolr_index_documents_alter().

File

./apachesolr_multilingual.module, line 67
Multilingual search using Apache Solr.

Code

function apachesolr_multilingual_apachesolr_index_documents_alter(&$documents, $entity, $entity_type, $env_id) {
  static $reentrance = TRUE;

  // The variable $reentrance forbids recursive calls if set to FALSE.
  if (!$reentrance) {
    return;
  }
  $environment = apachesolr_multilingual_environment_load($env_id);
  $settings = $environment['conf']['apachesolr_multilingual_index_settings'];
  if ($settings['apachesolr_multilingual_index']) {
    foreach ($documents as $document) {
      if (!isset($document->ss_language)) {

        // This document could not be handled by apachesolr_multilingual.
        continue;
      }
      $additional_documents_langcodes = array();
      $reset_entity_language_none = FALSE;
      $active_languages = apachesolr_multilingual_language_list();
      $active_langcodes = array_keys($active_languages);
      if (APACHESOLR_MULTILINGUAL_LANGUAGE_UND == $document->ss_language && APACHESOLR_MULTILINGUAL_LANGUAGE_NONE == $entity->language) {

        // language-neutral
        $document->ss_language = $settings['apachesolr_multilingual_map_language_neutral'];
        if (APACHESOLR_MULTILINGUAL_MAP_ALL == $document->ss_language) {

          // The language-neutral entity should be mapped to all languages.
          // Therefor we temporarily "transform" the entity in a language
          // specific one that has translations.
          $document->ss_language = $entity->language = $active_langcodes[0];
          array_shift($active_languages);
          $additional_documents_langcodes = array_combine(array_keys($active_languages), array_keys($active_languages));
          $reset_entity_language_none = TRUE;
        }
      }
      if (APACHESOLR_MULTILINGUAL_LANGUAGE_UND == $document->ss_language) {

        // Do not create special fields for language neutral ("und").
        continue;
      }
      switch ($entity_type) {
        case 'node':

          // Node Translation, aka Content Translation.
          $translations = array();
          if (!empty($document->is_tnid)) {
            $translations = translation_node_get_translations($document->is_tnid);
          }
          $document
            ->setField('sm_translations', array_unique(array_merge(array_keys($translations), array(
            $document->ss_language,
          ))));
          $document
            ->setField('sm_missing_translations', array_diff($active_langcodes, array_merge(array_keys($translations), array(
            $document->ss_language,
          ))));
          if (!empty($translations) && $settings['apachesolr_multilingual_clir']['apachesolr_multilingual_index_translations']) {

            // CLIR for Node Translation, aka Content Translation.
            // TODO distinguish between published and unpublished translations
            foreach ($translations as $node_stub) {
              if ($node_stub->nid != $document->entity_id) {
                if ($translation_node = node_load($node_stub->nid)) {
                  $reentrance = FALSE;

                  // apachesolr_convert_entity_to_documents() calls hook_apachesolr_index_documents_alter().
                  // The variable $reentrance avoids an endless recursion.
                  $tmp_documents = apachesolr_convert_entity_to_documents($translation_node, 'node', $env_id);
                  $reentrance = TRUE;

                  // Handle multiple documents is a future use case of apachesolr we don't care about now.
                  $translation_document = reset($tmp_documents);
                  apachesolr_multilingual_fire_callbacks($translation_document, $translation_node, 'node', 'document callback', $env_id);
                  apachesolr_multilingual_copy_common_to_i18n_fields($translation_document, $document);
                }
              }
            }
          }

          // In case of content translation this code is used when language
          // undefined content needs to be mapped to different languages.
          // The document id gets a language suffix to be unique.
          foreach ($additional_documents_langcodes as $langcode) {

            // Prevent endless recursion if language specific document has been created already
            // and $reentrance is set to TRUE.
            if (!array_key_exists($document->id, $documents)) {

              // Temporarily switch the language context for apachesolr_index_node_solr_document(),
              // which is not aware of entity_translation.
              $original_langcode = $entity->language;
              $entity->language = $langcode;
              $id = $document->id . '/' . $langcode;
              $additional_documents = array();
              $additional_documents[$id] = clone $document;
              $reentrance = FALSE;

              // apachesolr_convert_entity_to_documents() calls hook_apachesolr_index_documents_alter().
              // The variable $reentrance avoids an endless recursion.
              $tmp_documents = apachesolr_convert_entity_to_documents($entity, $entity_type, $env_id);
              $reentrance = TRUE;

              // Handle multiple documents is a future use case of apachesolr we don't care about now.
              $basic_fields = reset($tmp_documents);
              foreach ($basic_fields
                ->getFieldNames() as $field) {
                $additional_documents[$id]->{$field} = $basic_fields->{$field};
              }
              $additional_documents[$id]->id = $id;

              // Add document_id of original language, so that all translations can be deleted together,
              // when original document is removed. See apachesolr_index_delete_entity_from_index().
              $additional_documents[$id]->sm_parent_document_id = array(
                $document->id,
              );
              apachesolr_multilingual_fire_callbacks($additional_documents[$id], $entity, $entity_type, 'document callback', $env_id);

              // Recursive call to deal with 'apachesolr_multilingual_index_translations' aka CLIR and title field, see above and below
              apachesolr_multilingual_apachesolr_index_documents_alter($additional_documents, $entity, $entity_type, $env_id);
              $entity->language = $original_langcode;
              foreach ($additional_documents as $key => $additional_document) {
                $documents[$key] = $additional_document;
              }
            }
          }
          break;
        default:
          watchdog('Apache Solr', t('Apache Solr Multilingual does not fully support indexing entities of type %type.', array(
            '%type' => $entity_type,
          )), NULL, WATCHDOG_NOTICE);
      }

      // Same for Entity Translation and Node Translation, aka Content Translation.
      apachesolr_multilingual_copy_common_to_i18n_fields($document, $document);

      // Hook to allow modifications of the language specific index document.
      foreach (module_implements('apachesolr_multilingual_index_document_alter') as $module) {
        $function = $module . '_apachesolr_multilingual_index_document_alter';
        $function($document, $document->ss_language, $entity, $entity_type, $env_id);
      }
      if ($reset_entity_language_none) {
        $entity->language = APACHESOLR_MULTILINGUAL_LANGUAGE_NONE;
      }
    }
  }
}