function _menu_views_form_alter in Menu Views 7.2
Same name and namespace in other branches
- 8.3 menu_views.admin.inc \_menu_views_form_alter()
Alters existing forms in preparation for adding Menu Views to it.
Parameters
$element: The top level menu item edit form element passed by reference.
$form: The complete form array passed by reference.
$form_state: The complete form state array passed by reference.
2 calls to _menu_views_form_alter()
- menu_views_form_menu_edit_item_alter in ./
menu_views.admin.inc - Implements hook_form_FORM_ID_alter().
- menu_views_form_node_form_alter in ./
menu_views.admin.inc - Implements hook_form_FORM_ID_alter().
File
- ./
menu_views.admin.inc, line 136 - Form hooks for the menu_views module.
Code
function _menu_views_form_alter(array &$element, array &$form, array &$form_state) {
// Still need to render the form, just hide it for those who don't have access.
$access = user_access('administer menu views');
// Only need to attach or alter form if user has access.
if ($access) {
// Unset the previous values so that the new values can get saved.
if (isset($element['options']['#value']['menu_views'])) {
unset($element['options']['#value']['menu_views']);
}
$module_path = drupal_get_path('module', 'menu_views');
// Attach CSS.
$element['#attached']['css'][] = array(
'data' => $module_path . '/menu_views.admin.css',
'type' => 'file',
'group' => CSS_THEME,
'weight' => 100,
);
// Attach JavaScript.
$element['#attached']['js'][] = array(
'data' => array(
'menu_views' => array(
'admin_theme' => variable_get('admin_theme', 'bartik'),
'node_form' => isset($form['#node']),
),
),
'type' => 'setting',
);
$element['#attached']['js'][] = array(
'data' => $module_path . '/menu_views.admin.js',
'type' => 'file',
'group' => JS_THEME,
'weight' => 100,
);
// Create AJAX wrapper around the menu edit form element.
$element['#prefix'] = '<div id="menu-edit-item-wrapper">';
$element['#suffix'] = '</div>';
}
// Get values for the new menu views item, existing menu views item or submitted form values.
$item = _menu_views_get_item($form, $form_state);
// Use the loaded node if it exists.
$node = isset($form['#node']) ? $form['#node'] : FALSE;
// Get the original menu link, if it exists.
$menu_link = array();
if (isset($form['original_item']['#value'])) {
$menu_link = $form['original_item']['#value'];
}
elseif ($node && isset($node->menu)) {
$menu_link = $node->menu;
}
// Determine which menu item type is visible.
$visible = !('view' === $item['type']);
// Determine the correct link path to show or set values for.
if (isset($element['link_path'])) {
if ('link' === $item['type'] && !empty($element['link_path']['#default_value']) && '<view>' === $element['link_path']['#default_value']) {
$element['link_path']['#default_value'] = $item['original_path'];
}
elseif ('view' === $item['type']) {
if (!empty($element['link_path']['#default_value']) && '<view>' !== $element['link_path']['#default_value']) {
$item['original_path'] = $element['link_path']['#default_value'];
}
$element['link_path']['#default_value'] = '<view>';
}
}
elseif ($node) {
if ('view' === $item['type']) {
$element['link_path'] = array(
'#type' => 'value',
'#value' => '<view>',
);
}
else {
$element['link_path'] = array(
'#type' => 'value',
'#value' => $node && isset($node->nid) ? 'node/' . $node->nid : '',
);
}
}
// If the original path is empty and this is a node, use the node path.
if (empty($item['original_path']) && $node && isset($node->nid) && $node->nid) {
$item['original_path'] = 'node/' . $node->nid;
}
// If this menu item is a view and user does not have permissions to administer menu views, restrict access to the form.
// Cannot use drupal_access_denied() because this form can also be inside a node edit form.
if ($item['type'] == 'view' && !$access) {
$element['disabled'] = array(
'#prefix' => '<div class="messages error">',
'#suffix' => '</div>',
'#markup' => t('You do not have access to edit this type of menu item.'),
'#weight' => -9999,
);
$visible = FALSE;
// Hide the action buttons on menu item edit forms.
if (isset($element['actions'])) {
$element['actions']['#access'] = $access;
}
// Hide the checkbox toggle on node edit forms.
if (isset($form['menu']['enabled'])) {
$form['menu']['enabled']['#access'] = $access;
}
}
// Replace Parent Menu Options
$options = _menu_views_parent_options(menu_get_menus(), $menu_link);
if ($node && isset($node->type)) {
$options = _menu_views_parent_options(menu_get_menus(), $menu_link['mlid'] ? $menu_link : $node->type, $node->type);
}
$element['parent']['#title'] = t('Parent Item');
$element['parent']['#options'] = $options;
// Move general menu item settings into a container.
if ($access) {
$element['menu_item_settings'] = array(
'#access' => $access,
'#type' => 'fieldset',
'#title' => t('Menu item settings'),
'#parents' => isset($form['#node']) ? array(
'menu',
) : array(),
'#weight' => $item['type'] == 'view' ? 50 : 1,
'#attributes' => array(
'id' => 'menu_item_settings',
'class' => array(
'menu-item-settings',
),
),
);
if (isset($element['enabled'])) {
$element['menu_item_settings']['enabled'] = $element['enabled'];
unset($element['enabled']);
}
$element['menu_item_settings']['expanded'] = $element['expanded'];
unset($element['expanded']);
$element['menu_item_settings']['parent'] = $element['parent'];
unset($element['parent']);
$element['menu_item_settings']['weight'] = $element['weight'];
unset($element['weight']);
}
// Add support for the xmlsitemap_menu module.
if (module_exists('xmlsitemap_menu')) {
$element['xmlsitemap']['#weight'] = $item['type'] === 'view' ? 51 : 30;
}
// Create a radio toggle for menu item types: link or view.
$element['menu_item_type'] = array(
'#access' => $access,
'#type' => 'radios',
'#title' => t('Menu item type'),
'#options' => array(
'link' => t('Link'),
'view' => t('View'),
),
'#default_value' => $item['type'],
// Add container-inline style for admin themes like Rubik.
'#prefix' => '<div id="menu-item-type" class="form-item container-inline">',
'#suffix' => '</div>',
'#ajax' => array(
'callback' => '_menu_views_form_ajax',
'wrapper' => 'menu-edit-item-wrapper',
),
// Ensure this is rendered at the top of the form.
'#weight' => -1000,
);
// Determine if menu_attributes is enabled.
$menu_attributes = module_exists('menu_attributes');
// Show/Hide the core link form elements based on menu item type of: link or view.
foreach (element_children($element) as $child) {
// Skip options (handled below) and actions.
$type = isset($element[$child]['#type']) ? $element[$child]['#type'] : '';
// Ensure necessary children are always present (regardless of their type).
$ignore_children = array(
'menu_item_type',
'disabled',
'menu_item_settings',
'form_build_id',
'form_token',
'form_id',
'xmlsitemap',
);
// Allow the menu_attributes module to control the description field.
if ($menu_attributes) {
$ignore_children[] = 'description';
}
$ignore_types = array(
'actions',
'hidden',
'value',
'token',
);
if (in_array($child, $ignore_children) || !$type || in_array($type, $ignore_types)) {
continue;
}
$element[$child]['#access'] = $visible;
}
// Show/Hide the link options form elements based on menu item type of: link or view.
foreach (element_children($element['options']) as $child) {
// Skip menu_view options.
$type = isset($element['options'][$child]['#type']) ? $element['options'][$child]['#type'] : '';
// Ensure necessary children are always present (regardless of their type).
$ignore_children = array(
'menu_views',
);
$ignore_types = array(
'hidden',
'value',
'token',
);
if (in_array($child, $ignore_children) || !$type || in_array($type, $ignore_types)) {
continue;
}
$element['options'][$child]['#access'] = $visible;
}
// Place menu views in the options array.
$element['options']['menu_views'] = array(
'#type' => 'container',
'#tree' => TRUE,
'#weight' => 10,
'#access' => $access,
);
// Create the actual Menu Views form.
_menu_views_form($item, $element['options']['menu_views'], $form, $form_state);
// Add menu views form handlers.
array_unshift($form['#validate'], '_menu_views_form_validate');
array_unshift($form['#submit'], '_menu_views_form_submit');
}