You are here

function nodehierarchy_form_menu_edit_item_alter in Node Hierarchy 7.2

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

Implements 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 360
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['module']['#value'] == 'nodehierarchy') {

    // Add the js to hide/show the menu selector.
    drupal_add_js(drupal_get_path("module", "nodehierarchy") . '/nodehierarchy.js');
    $menu = menu_load($form['original_item']['#value']['menu_name']);
    $item = menu_link_load($form['mlid']['#value']);
    if (!$item) {
      $item = array(
        'link_title' => '',
        'mlid' => 0,
        'plid' => 0,
        'menu_name' => $menu['menu_name'],
        'weight' => 0,
        'link_path' => '',
        'options' => array(),
        'module' => 'menu',
        'expanded' => 0,
        'hidden' => 0,
        'has_children' => 0,
      );
    }
    $menus = menu_get_menus();

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

    // Load the parent menu item if any (to get the parent node id).
    $parent_menu = _nodehierarchy_load_menu_link($item['plid']);
    $default = $item['menu_name'] . ':' . (isset($item['plid']) ? $item['plid'] : 0);
    if (!isset($options[$default])) {
      $default = 'navigation:0';
    }

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

    // Replace the parent pulldown.
    unset($form['parent']);
    $form['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_name'] = array(
      '#type' => 'select',
      '#title' => 'Menu',
      '#prefix' => '<div class="nodehierarchy-menu-name">',
      '#suffix' => '</div>',
      '#options' => $menus,
      '#default_value' => $default,
      '#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_name']['#weight'] = 20;
    $form['weight']['#weight'] = 30;
    $form['#submit'] = array_merge(array(
      'nodehierarchy_form_menu_edit_item_submit',
    ), $form['#submit']);
  }
}