Same name and namespace in other branches
  1. 4.7.x modules/menu.module \menu_form_alter()
  2. 5.x modules/menu/menu.module \menu_form_alter()

Implementation of hook_form_alter(). Adds menu item fields to the node form.

File

modules/menu/menu.module, line 358
Allows administrators to customize the site navigation menu.

Code

function menu_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['#node']) && $form['#node']->type . '_node_form' == $form_id) {

    // Note - doing this to make sure the delete checkbox stays in the form.
    $form['#cache'] = TRUE;
    $form['menu'] = array(
      '#type' => 'fieldset',
      '#title' => t('Menu settings'),
      '#access' => user_access('administer menu'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#tree' => TRUE,
      '#weight' => -2,
      '#attributes' => array(
        'class' => 'menu-item-form',
      ),
    );
    $item = $form['#node']->menu;
    if ($item['mlid']) {

      // There is an existing link.
      $form['menu']['delete'] = array(
        '#type' => 'checkbox',
        '#title' => t('Delete this menu item.'),
      );
    }
    if (!$item['link_title']) {
      $form['menu']['#collapsed'] = TRUE;
    }
    foreach (array(
      'mlid',
      'module',
      'hidden',
      'has_children',
      'customized',
      'options',
      'expanded',
      'hidden',
      'parent_depth_limit',
    ) as $key) {
      $form['menu'][$key] = array(
        '#type' => 'value',
        '#value' => $item[$key],
      );
    }
    $form['menu']['#item'] = $item;
    $form['menu']['link_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Menu link title'),
      '#default_value' => $item['link_title'],
      '#description' => t('The link text corresponding to this item that should appear in the menu. Leave blank if you do not wish to add this post to the menu.'),
      '#required' => FALSE,
    );

    // Generate a list of possible parents (not including this item or descendants).
    $options = menu_parent_options(menu_get_menus(), $item);
    $default = $item['menu_name'] . ':' . $item['plid'];
    if (!isset($options[$default])) {
      $default = 'primary-links:0';
    }
    $form['menu']['parent'] = array(
      '#type' => 'select',
      '#title' => t('Parent item'),
      '#default_value' => $default,
      '#options' => $options,
      '#description' => t('The maximum depth for an item and all its children is fixed at !maxdepth. Some menu items may not be available as parents if selecting them would exceed this limit.', array(
        '!maxdepth' => MENU_MAX_DEPTH,
      )),
      '#attributes' => array(
        'class' => 'menu-title-select',
      ),
    );
    $form['#submit'][] = 'menu_node_form_submit';
    $form['menu']['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight'),
      '#delta' => 50,
      '#default_value' => $item['weight'],
      '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
    );
  }
}