You are here

function nodehierarchy_form_menu_edit_item_alter in Node Hierarchy 6.2

Same name and namespace in other branches
  1. 6.3 nodehierarchy.module \nodehierarchy_form_menu_edit_item_alter()
  2. 7.2 nodehierarchy.module \nodehierarchy_form_menu_edit_item_alter()

Implementation of hook_form_menu_edit_item_alter().

Alter the menu edit screen to limit the available parents according to the rules of node hierachy.

File

./nodehierarchy.module, line 365
A module to make nodes hierarchical.

Code

function nodehierarchy_form_menu_edit_item_alter(&$form, &$form_state) {

  // Replace the parent pulldown with the node hierarchy parent selector.
  if ($form['menu']['#item']['module'] == 'nodehierarchy') {

    // Add the js to hide/show the menu selector.
    drupal_add_js(drupal_get_path("module", "nodehierarchy") . '/nodehierarchy.js');

    // Get the node for this menu item.
    list(, $nid) = explode('/', $form['menu']['#item']['link_path']);
    $node = node_load($nid);

    // Load the parent menu item if any (to get the parent node id).
    $parent_menu = _nodehierarchy_load_menu_link(@$form['menu']['#item']['plid']);

    // Add a class to the menu fieldset to allow the js to work.
    $form['menu']['#attributes']['class'] .= ' nodehierarchy-menu-link';

    // Replace the parent pulldown.
    $form['menu']['pnid'] = _nodehierarchy_get_parent_selector($node->type, $parent_menu['nid'], $nid);

    // Add the menu selector in case the user picks none for the parent.
    $form['menu']['menu_name'] = array(
      '#type' => 'select',
      '#title' => 'Menu',
      '#prefix' => '<div class="nodehierarchy-menu-name">',
      '#suffix' => '</div>',
      '#options' => menu_get_menus(),
      '#default_value' => @$form['menu']['#item']['menu_name'],
      '#description' => t('If you do not pick a parent for this node its menu item will appear at the top level of this menu.'),
    );

    // Set some weights so that the weight pulldown still appears at the bottom.
    $form['menu']['parent']['#weight'] = 10;
    $form['menu']['menu_name']['#weight'] = 20;
    $form['menu']['weight']['#weight'] = 30;
    $form['#submit'] = array_merge(array(
      'nodehierarchy_form_menu_edit_item_submit',
    ), $form['#submit']);
  }
}