You are here

function title_entity_presave in Title 7

Implements hook_entity_presave().

File

./title.module, line 142

Code

function title_entity_presave($entity, $entity_type) {
  $entity_langcode = title_entity_language($entity_type, $entity);
  $langcode = $entity_langcode;

  // If Entity Translation is enabled and the entity type is transltable,we need
  // to check if we have a translation for the current active language. If so we
  // need to synchronize the legacy field values into the replacing field
  // translations in the active language.
  if (module_invoke('entity_translation', 'enabled', $entity_type)) {
    $langcode = title_active_language();
    $translations = entity_translation_get_handler($entity_type, $entity)
      ->getTranslations();

    // If we are removing a translation for the active language we need to skip
    // reverse synchronization, as we would store empty values in the original
    // replacing fields immediately afterwards.
    if (!isset($translations->data[$langcode])) {
      $langcode = isset($translations->hook[$langcode]['hook']) && $translations->hook[$langcode]['hook'] == 'delete' ? FALSE : $entity_langcode;
    }
  }

  // Perform reverse synchronization to retain any change in the legacy field
  // values. We must avoid doing this twice as we might overwrite the already
  // synchronized values, if we are updating an existing entity.
  if ($langcode) {
    title_entity_sync($entity_type, $entity, $langcode, TRUE);
  }

  // If we are not dealing with the entity language, we need to synchronize the
  // original values into the legacy fields to ensure they are always stored in
  // the entity table.
  if ($entity_langcode != $langcode) {
    list($id, , ) = entity_extract_ids($entity_type, $entity);
    $sync =& drupal_static('title_entity_sync', array());
    unset($sync[$entity_type][$id]);
    title_entity_sync($entity_type, $entity, $entity_langcode);
  }
}