You are here

function menu_form_alter in Drupal 4

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

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

File

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

Code

function menu_form_alter($form_id, &$form) {
  if (user_access('administer menu') && isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id) {
    $edit = isset($_POST['edit']) ? $_POST['edit'] : '';
    $edit['nid'] = $form['nid']['#value'];
    $item = array();
    if ($edit['nid'] > 0) {
      $item = db_fetch_array(db_query("SELECT * FROM {menu} WHERE path = 'node/%d'", $edit['nid']));
      if (is_array($edit['menu'])) {
        $item = !is_array($item) ? $edit['menu'] : ($_POST['op'] == t('Preview') ? array_merge($item, $edit['menu']) : array_merge($edit['menu'], $item));
      }
    }
    $form['menu'] = array(
      '#type' => 'fieldset',
      '#title' => t('Menu settings'),
      '#collapsible' => TRUE,
      '#collapsed' => empty($item['title']),
      '#tree' => TRUE,
      '#weight' => 30,
    );
    $form['menu']['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#default_value' => $item['title'],
      '#description' => t('The name to display for this link.'),
    );
    $form['menu']['description'] = array(
      '#type' => 'textfield',
      '#title' => t('Description'),
      '#default_value' => $item['description'],
      '#description' => t('The description displayed when hovering over a menu item.'),
    );

    // Generate a list of possible parents.
    $options = menu_parent_options($item['mid'], variable_get('menu_parent_items', 0));
    $form['menu']['pid'] = array(
      '#type' => 'select',
      '#title' => t('Parent item'),
      '#default_value' => $item['pid'],
      '#options' => $options,
    );
    $form['menu']['path'] = array(
      '#type' => 'hidden',
      '#value' => $item['path'],
    );
    $form['menu']['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight'),
      '#default_value' => $item['weight'],
      '#delta' => 10,
      '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
    );
    $form['menu']['mid'] = array(
      '#type' => 'hidden',
      '#value' => $item['mid'] ? $item['mid'] : 0,
    );
    $form['menu']['type'] = array(
      '#type' => 'hidden',
      '#value' => $item['type'] ? $item['type'] : MENU_CUSTOM_ITEM,
    );
    if ($item['mid'] > 0) {
      $form['menu']['delete'] = array(
        '#type' => 'checkbox',
        '#title' => t('Check to delete this menu item.'),
        '#default_value' => $item['delete'],
      );
      $form['menu']['advanced'] = array(
        '#type' => 'item',
        '#value' => t('You may also <a href="%edit">edit the advanced settings</a> for this menu item.', array(
          '%edit' => url("admin/menu/item/edit/{$item['mid']}"),
        )),
      );
    }
  }
}