function menu_link_weight_form_node_form_alter in Menu Link Weight 8
Same name and namespace in other branches
- 8.2 menu_link_weight.node.inc \menu_link_weight_form_node_form_alter()
- 7 menu_link_weight.module \menu_link_weight_form_node_form_alter()
Implements hook_form_BASE_FORM_ID_alter() for node forms.
File
- ./
menu_link_weight.node.inc, line 13
Code
function menu_link_weight_form_node_form_alter(&$form, FormStateInterface $form_state) {
$current_user = \Drupal::currentUser();
$module_handler = \Drupal::moduleHandler();
/** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info_manager */
$element_info_manager = \Drupal::service('plugin.manager.element_info');
$is_admin = $current_user
->hasPermission('administer menu') && isset($form['menu']['link']);
$is_admin_per_menu = $module_handler
->moduleExists('menu_admin_per_menu') && function_exists('menu_admin_per_menu_filter_parent_options') && isset($form['menu']['link']['menu_parent']['#options']) && !$current_user
->hasPermission('administer menu') && menu_admin_per_menu_filter_parent_options($current_user, $form['menu']['link']['menu_parent']['#options']);
// Only allow users with the "administer menu" permission or that the Menu
// Admin Per Menu has granted access to some menus.
if (!$is_admin && !$is_admin_per_menu) {
return;
}
// Prevent the "weight" widget from being displayed.
$form['menu']['link']['weight']['#access'] = FALSE;
// Add submission/validation handlers.
$form['#validate'][] = 'menu_link_weight_node_form_validate';
/** @see menu_ui_form_node_form_alter() */
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'menu_link_weight_node_form_submit';
}
}
// Add the Menu Link weight fieldset.
$defaults = $element_info_manager
->getInfo('fieldset');
$form['menu']['link']['menu_link_weight'] = array(
'#type' => 'fieldset',
'#title' => t('Menu link weight'),
'#prefix' => '<div id="menu-link-weight-wrapper">',
'#suffix' => '</div>',
'#process' => array_merge($defaults['#process'], [
'menu_link_weight_node_element_process',
]),
);
$form['menu']['link']['menu_link_weight']['table'] = array(
'#type' => 'table',
'#header' => array(
'name' => t('Name'),
'weight' => t('Weight'),
),
'#id' => 'menu-link-weight-reorder',
'#tabledrag' => array(
array(
'action' => 'order',
'relationship' => 'sibling',
'group' => 'menu-link-weight-item-weight',
),
),
// Remove the 'table' element from the form value structure.
'#parents' => [
'menu',
'menu_link_weight',
],
);
// Define the "db_weights" element, which will hold hidden fields with
// the values of the menu links in the database. Upon validation we will
// check whether the weights are still the same as when the form was
// built, to make sure users won't overwrite each other's changes.
$form['menu']['link']['db_weights'] = array(
'#tree' => TRUE,
'#prefix' => '<div id="menu-link-weight-db-weights-wrapper">',
'#suffix' => '</div>',
);
$form['#attached']['library'][] = 'menu_link_weight/menu_link_weight';
// Define the AJAX callback for changes in the Parent element.
$form['menu']['link']['menu_parent']['#ajax'] = array(
'callback' => 'menu_link_weight_node_parent_ajax_callback',
'event' => 'change',
);
// This next button will not be displayed if JS is enabled.
$form['menu']['link']['menu_link_weight_nojs'] = array(
'#type' => 'submit',
'#value' => menu_link_weight_get_button_text(),
// No need to validate when submitting this.
'#limit_validation_errors' => array(),
'#validate' => array(),
'#submit' => array(
'menu_link_weight_node_form_update_parent_submit',
),
'#attributes' => [
'class' => [
'js-hide',
],
],
);
}