You are here

function entity_translation_i18n_menu_node_presave in Entity Translation 7

Implements hook_node_presave().

File

entity_translation_i18n_menu/entity_translation_i18n_menu.module, line 45
The menu specific translation functions and hook implementations.

Code

function entity_translation_i18n_menu_node_presave($node) {
  if (!entity_translation_enabled('node')) {
    return;
  }
  $handler = entity_translation_get_handler('node', $node);
  $translations = $handler
    ->getTranslations();
  $source_langcode = $handler
    ->getSourceLanguage();
  $tset = !empty($node->menu['tset']);

  // If no translation is available the menu data is always supposed to be
  // entered in the source string language. This way we avoid having unneeded
  // string translations hanging around.
  if (empty($source_langcode) && count($translations->data) < 2 && !$tset) {
    return;
  }

  // When creating a new translation, leave the source menu item intact and
  // create a new one.
  $langcode = entity_language('node', $node);
  if (!empty($node->menu) && $tset && !empty($source_langcode)) {
    $node->source_menu = menu_link_load($node->menu['mlid']);
    unset($node->menu['mlid']);
  }

  // Store the entity language for later reference when saving a translation.
  // If we are editing a translation in the string source language, we can skip
  // item processing since the proper values are already in place. Instead when
  // creating the translation we need to process the link item before saving it.
  if (!empty($node->menu) && !empty($langcode) && ($source_langcode || $langcode != i18n_string_source_language())) {
    $node->menu['entity_language'] = $langcode;
    $node->menu['entity_translation_handler'] = $handler;
  }

  // If we have a translation set here we should prepare it for storage,
  // otherwise we need to ensure the menu item has no language so it can be
  // localized.
  if ($tset) {
    entity_translation_i18n_menu_item_tset_prepare($node, $langcode);
  }
  else {
    $node->menu['language'] = LANGUAGE_NONE;
  }
}