You are here

function search_api_et_entity_update in Search API Entity Translation 7.2

Implements hook_entity_update().

File

./search_api_et.module, line 305
Adds Entity Translation support to the Search API.

Code

function search_api_et_entity_update($entity, $entity_type) {

  // We only react on entity operations for types with property information, as
  // we don't provide search integration for the others.
  if (!entity_get_property_info($entity_type)) {
    return;
  }
  list($entity_id, $revision, $bundle) = entity_extract_ids($entity_type, $entity);
  $et_entity_type = SearchApiEtHelper::getItemType($entity_type);

  // Use the translation handler to fetch the main language of the entity.
  // entity_language() together with entity_translation returns the current
  // form language of an entity. Hence if the entity is translated the form
  // language usually differs from the main language - what would lead to the
  // removal of a valid tracking language.
  if (!isset($entity->original) || !is_object($entity->original)) {
    $entity->original = entity_load_unchanged($entity_type, $entity_id);
  }
  if (entity_translation_enabled($entity_type, $entity)) {
    $translation_handler = entity_translation_get_handler($entity_type, $entity);
    $language = $translation_handler
      ->getLanguage();
    $translation_handler_original = entity_translation_get_handler($entity_type, $entity->original);
    $old_language = $translation_handler_original
      ->getLanguage();
  }
  else {
    $language = entity_language($entity_type, $entity);
    $old_language = entity_language($entity_type, $entity->original);
  }

  // If the entity language has changed, remove the old item from the index.
  if (!empty($language) && !empty($old_language) && $language !== $old_language) {
    search_api_track_item_delete($et_entity_type, array(
      SearchApiEtHelper::buildItemId($entity_id, $old_language),
    ));
    search_api_track_item_insert($et_entity_type, array(
      SearchApiEtHelper::buildItemId($entity_id, $language),
    ));
  }
  else {
    search_api_track_item_change($et_entity_type, array(
      SearchApiEtHelper::buildItemId($entity_id, $language),
    ));
  }
}