menu_form_alter
- Versions
- 4.7 – 5
menu_form_alter($form_id, &$form)- 6 – 7
menu_form_alter(&$form, $form_state, $form_id)
Implement hook_form_alter(). Adds menu item fields to the node form.
Code
modules/menu/menu.module, line 562
<?php
function menu_form_alter(&$form, $form_state, $form_id) {
if (!empty($form['#node_edit_form'])) {
// Generate a list of possible parents.
// @todo This must be handled in a #process handler.
$type = $form['#node']->type;
$options = menu_parent_options(menu_get_menus(), $type);
// If no possible parent menu items were found, there is nothing to display.
if (empty($options)) {
return;
}
$link = $form['#node']->menu;
$form['#submit'][] = 'menu_node_form_submit';
$form['menu'] = array(
'#type' => 'fieldset',
'#title' => t('Menu settings'),
'#access' => user_access('administer menu'),
'#collapsible' => TRUE,
'#collapsed' => !$link['link_title'],
'#group' => 'additional_settings',
'#attached' => array(
'js' => array(drupal_get_path('module', 'menu') . '/menu.js'),
),
'#tree' => TRUE,
'#weight' => -2,
'#attributes' => array('class' => array('menu-link-form')),
);
$form['menu']['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Provide a menu link'),
'#default_value' => (int) (bool) $link['mlid'],
);
$form['menu']['link'] = array(
'#type' => 'container',
'#parents' => array('menu'),
'#states' => array(
'invisible' => array(
'input[name="menu[enabled]"]' => array('checked' => FALSE),
),
),
);
// Populate the element with the link data.
foreach (array('mlid', 'module', 'hidden', 'has_children', 'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as $key) {
$form['menu']['link'][$key] = array('#type' => 'value', '#value' => $link[$key]);
}
$form['menu']['link']['link_title'] = array(
'#type' => 'textfield',
'#title' => t('Menu link title'),
'#default_value' => $link['link_title'],
);
$default = ($link['mlid'] ? $link['menu_name'] . ':' . $link['plid'] : variable_get('menu_parent_' . $type, 'main-menu:0'));
// @todo This will fail with the new selective menus per content type.
if (!isset($options[$default])) {
$default = 'navigation:0';
}
$form['menu']['link']['parent'] = array(
'#type' => 'select',
'#title' => t('Parent item'),
'#default_value' => $default,
'#options' => $options,
'#attributes' => array('class' => array('menu-parent-select')),
);
$form['menu']['link']['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight'),
'#delta' => 50,
'#default_value' => $link['weight'],
'#description' => t('Menu links with smaller weights are displayed before links with larger weights.'),
);
}
}
?>Login or register to post comments 