You are here

function i18nmenu_form_alter in Internationalization 6

Implementation of hook_form_alter().

Add language to menu settings of the node form, as well as setting defaults to match the translated item's menu settings.

File

i18nmenu/i18nmenu.module, line 376
Internationalization (i18n) submodule: Menu translation.

Code

function i18nmenu_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) {
    $node = $form['#node'];
    if (!empty($form['menu'])) {

      // Customized must be set to 1 to save language.
      $form['menu']['customized'] = array(
        '#type' => 'value',
        '#value' => 1,
      );
    }

    // Do nothing if the node already has a menu.
    if (!empty($node->menu['mlid'])) {
      return;
    }

    // Find the translation source node.  If creating a new node,
    // translation_source is set.  Otherwise, node_load the tnid.
    // New translation.
    if (!empty($node->translation_source)) {
      $tnode = $node->translation_source;
    }
    elseif (!empty($node->nid) && !empty($node->tnid) && $node->nid != $node->tnid) {
      $tnode = node_load($node->tnid);
    }
    else {
      return;
    }

    // Prepare the tnode so the menu item will be available.
    _i18nmenu_node_prepare($tnode);
    if ($tnode->menu) {

      // Set default values based on translation source's menu.
      $form['menu']['link_title']['#default_value'] = $tnode->menu['link_title'];
      $form['menu']['weight']['#default_value'] = $tnode->menu['weight'];
      $form['menu']['parent']['#default_value'] = $tnode->menu['menu_name'] . ':' . $tnode->menu['plid'];
    }
  }
}