You are here

function apachesolr_multilingual_apachesolr_index_documents_alter in Apache Solr Multilingual 7

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

Implements hook_apachesolr_index_documents_alter().

File

./apachesolr_multilingual.module, line 70
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']) {
    global $language_content;
    $languages = language_list();
    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 (LANGUAGE_NONE == $document->ss_language && 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 (LANGUAGE_NONE == $document->ss_language) {

        // Do not create special fields for language neutral ("und").
        continue;
      }
      switch ($entity_type) {
        case 'node':
          if (module_exists('entity_translation') && entity_translation_enabled($entity_type) && entity_translation_enabled_bundle($entity_type, $entity->type)) {

            // Entity Translation.
            $entity_info = entity_get_info($entity_type);
            $translations_key = $entity_info['entity keys']['translations'];
            $original_langcode = $entity->language;
            $document
              ->setField('sm_translations', array_unique(array_merge(array_keys($entity->{$translations_key}->data), array(
              $original_langcode,
            ))));
            $document
              ->setField('sm_missing_translations', array_diff($active_langcodes, array_keys($entity->{$translations_key}->data)));
            foreach ($entity->{$translations_key}->data as $langcode => $entity_translation) {
              if ($langcode != $original_langcode && LANGUAGE_NONE != $langcode) {
                if ($entity_translation['status'] == 1) {

                  // Index published translations.
                  $additional_documents_langcodes[$langcode] = $langcode;
                }

                // CLIR for Entity Translation.
                if ($settings['apachesolr_multilingual_clir']['apachesolr_multilingual_index_translations'] && ($settings['apachesolr_multilingual_clir']['apachesolr_multilingual_index_unpublished_translations'] || $entity_translation['status'])) {

                  // Temporarily switch the language context for apachesolr_index_node_solr_document(),
                  // which is not aware of entity_translation.
                  $entity->language = $langcode;
                  $language_content = $languages[$langcode];
                  apachesolr_multilingual_index_node_translation($document, $entity, $env_id);
                  $entity->language = $original_langcode;
                  $language_content = $languages[$original_langcode];
                }
              }
            }
            if (module_exists('title')) {

              // Always override the title / label if title module is active.
              $document->label = apachesolr_clean_text(title_entity_label($entity, 'node', $document->ss_language));
            }
          }
          else {

            // 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,
            ))));

            // CLIR for Node Translation, aka Content Translation.
            if (!empty($translations) && $settings['apachesolr_multilingual_clir']['apachesolr_multilingual_index_translations']) {
              foreach ($translations as $node_stub) {
                if ($node_stub->nid != $document->entity_id && ($settings['apachesolr_multilingual_clir']['apachesolr_multilingual_index_unpublished_translations'] || $node_stub->status)) {
                  if ($translation_node = node_load($node_stub->nid)) {
                    apachesolr_multilingual_index_node_translation($document, $translation_node, $env_id);
                  }
                }
              }
            }
          }

          // In case of entity translation, create an index entry for each
          // language the node has been translated to.
          // In case of content translation this same 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;
              $language_content = $languages[$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;
              $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,
              );
              list(, , $bundle) = entity_extract_ids('node', $entity);

              //Get the callback array to add stuff to the document
              $document_callbacks = apachesolr_entity_get_callback($entity_type, 'document callback', $bundle);
              $tmp_documents = array();
              foreach ($document_callbacks as $document_callback) {

                // Call a type-specific callback to add stuff to the document.
                $tmp_documents = array_merge($tmp_documents, $document_callback($additional_documents[$id], $entity, $entity_type, $env_id));

                // Generic use case for future reference. Callbacks can
                // allow you to send back multiple documents.
                // @see apachesolr_index_node_solr_document()
                // $tmp_documents is not in use yet.
              }

              // 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;
              $language_content = $languages[$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 = LANGUAGE_NONE;
      }
    }
  }
}