You are here

function _menu_link_weight_menu_link_form_alter in Menu Link Weight 8

Same name and namespace in other branches
  1. 8.2 menu_link_weight.menu_ui.inc \_menu_link_weight_menu_link_form_alter()

Alter and attach the menu link weight widgets for menu link edit form.

2 calls to _menu_link_weight_menu_link_form_alter()
menu_link_weight_form_menu_link_content_form_alter in ./menu_link_weight.menu_ui.inc
Implements hook_form_BASE_FORM_ID_alter() for menu_link_content edit forms.
menu_link_weight_form_menu_link_edit_alter in ./menu_link_weight.menu_ui.inc
Implements hook_form_BASE_FORM_ID_alter() for menu link edit forms.

File

./menu_link_weight.menu_ui.inc, line 50

Code

function _menu_link_weight_menu_link_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);
  $is_admin_per_menu = $module_handler
    ->moduleExists('menu_admin_per_menu') && function_exists('menu_admin_per_menu_filter_parent_options') && isset($form['menu_parent']['#options']) && !$current_user
    ->hasPermission('administer menu') && menu_admin_per_menu_filter_parent_options($current_user, $form['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['weight']['#access'] = FALSE;

  // Add submission/validation handlers.
  $form['#validate'][] = 'menu_link_weight_menu_link_content_form_validate';

  // Add the Menu Link weight fieldset.
  $defaults = $element_info_manager
    ->getInfo('fieldset');
  $form['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_menu_link_content_element_process',
    ]),
    '#weight' => isset($form['weight']['#weight']) ? $form['weight']['#weight'] : NULL,
  );
  $form['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['db_weights'] = array(
    '#tree' => TRUE,
  );
  $form['#attached']['library'][] = 'menu_link_weight/menu_link_weight';

  // Define the AJAX callback for changes in the Parent element.
  $form['menu_parent']['#ajax'] = array(
    'callback' => 'menu_link_weight_menu_link_content_parent_ajax_callback',
    'wrapper' => 'menu-link-weight-wrapper',
  );

  // This next button will not be displayed if JS is enabled.
  $form['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_menu_link_content_form_update_parent_submit',
    ),
    '#attributes' => [
      'class' => [
        'js-hide',
      ],
    ],
  );
}