You are here

function _menu_attributes_form_alter in Menu Attributes 8

Same name and namespace in other branches
  1. 6.2 menu_attributes.module \_menu_attributes_form_alter()
  2. 6 menu_attributes.module \_menu_attributes_form_alter()
  3. 7 menu_attributes.module \_menu_attributes_form_alter()

Add the menu attributes to a menu item edit form.

Parameters

$form: The menu item edit form passed by reference.

$item: The optional existing menu item for context.

1 call to _menu_attributes_form_alter()
menu_attributes_form_menu_link_content_form_alter in ./menu_attributes.module
Implements hook_form_BASE_FORM_ID_alter().

File

./menu_attributes.module, line 186
Alters the menu item form to allow the administrator to specify additional attributes for the menu link

Code

function _menu_attributes_form_alter(array &$form, FormStateInterface $form_state, $menu_link) {
  $form['options'][MENU_ATTRIBUTES_LINK] = [
    '#type' => 'details',
    '#title' => t('Menu link attributes'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  ];
  $form['options'][MENU_ATTRIBUTES_ITEM] = [
    '#type' => 'details',
    '#title' => t('Menu item attributes'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  ];
  $attributes = menu_attributes_get_menu_attribute_info();
  $menu_link_options = $menu_link->link
    ->first()->options ?: [];
  $menu_attributes[MENU_ATTRIBUTES_LINK] = isset($menu_link_options[MENU_ATTRIBUTES_LINK]) ? $menu_link_options[MENU_ATTRIBUTES_LINK] : [];
  $menu_attributes[MENU_ATTRIBUTES_ITEM] = isset($menu_link_options[MENU_ATTRIBUTES_ITEM]) ? $menu_link_options[MENU_ATTRIBUTES_ITEM] : [];
  foreach ($attributes as $attribute => $info) {

    // If no scope is set, this attribute should be available to both item
    // and link.
    if (!isset($info['scope'])) {
      $info['scope'] = [
        MENU_ATTRIBUTES_ITEM,
        MENU_ATTRIBUTES_LINK,
      ];
    }

    // Define fields for each scope.
    foreach ($info['scope'] as $group) {
      if (isset($menu_attributes[$group][$attribute])) {
        $info['form']['#default_value'] = $menu_attributes[$group][$attribute];
      }

      // If the item description is set, override the form description.
      if ($group == MENU_ATTRIBUTES_ITEM && isset($info['item_description'])) {
        $info['form']['#description'] = $info['item_description'];
      }
      $form['options'][$group][$attribute] = $info['form'] + [
        '#access' => $info['enabled'],
      ];
    }
  }

  // Hide the 'description' field since we will be using our own 'title' field.
  if (isset($form['description'])) {
    $form['description']['#access'] = FALSE;
  }

  // Restrict access to the new form elements.
  $has_visible_children = (bool) Element::getVisibleChildren($form['options']['attributes']);
  $user_has_access = \Drupal::currentUser()
    ->hasPermission('administer menu attributes');
  $form['options']['attributes']['#access'] = $has_visible_children && $user_has_access;
  $form['actions']['submit']['#submit'][] = '_menu_attributes_form_submit';
}