You are here

function taxonomy_menu_ui_form_taxonomy_term_form_alter in Taxonomy Menu UI 8

Implements hook_form_BASE_FORM_ID_alter() for \Drupal\taxonomy\TermForm.

Adds menu item fields to the taxonomy term form.

See also

taxonomy_menu_ui_form_taxonomy_term_form_submit()

File

./taxonomy_menu_ui.module, line 136
Add ability to create menu links for taxonomy terms.

Code

function taxonomy_menu_ui_form_taxonomy_term_form_alter(&$form, FormStateInterface $form_state) {

  // Generate a list of possible parents
  // (not including this link or descendants).
  // @todo This must be handled in a #process handler.
  $term = $form_state
    ->getFormObject()
    ->getEntity();
  $defaults = taxonomy_menu_ui_get_menu_link_defaults($term);

  /** @var \Drupal\taxonomy\VocabularyInterface $vocabulary */
  $vid = $term
    ->bundle();
  $vocabulary = Vocabulary::load($vid);

  /** @var \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector */
  $menu_parent_selector = \Drupal::service('menu.parent_form_selector');
  $menu_names = menu_ui_get_menus();
  $vocabulary_menus = $vocabulary
    ->getThirdPartySetting('menu_ui', 'available_menus', [
    'main',
  ]);
  $available_menus = [];
  foreach ($vocabulary_menus as $menu) {
    $available_menus[$menu] = $menu_names[$menu];
  }
  if ($defaults['id']) {
    $default = $defaults['menu_name'] . ':' . $defaults['parent'];
  }
  else {
    $default = $vocabulary
      ->getThirdPartySetting('menu_ui', 'parent', 'main:');
  }
  $parent_element = $menu_parent_selector
    ->parentSelectElement($default, $defaults['id'], $available_menus);

  // If no possible parent menu items were found, there is nothing to display.
  if (empty($parent_element)) {
    return;
  }
  $current_user = \Drupal::currentUser();
  $access = $current_user
    ->hasPermission('administer menu');

  // Menu admin per menu integration.
  if (!$access && \Drupal::moduleHandler()
    ->moduleExists('menu_admin_per_menu')) {
    foreach (array_keys($available_menus) as $available_menu_id) {
      if ($access = $current_user
        ->hasPermission('administer ' . $available_menu_id . ' menu items')) {
        break;
      }
    }
  }
  $form['menu'] = [
    '#type' => 'details',
    '#title' => t('Menu settings'),
    '#access' => $access,
    '#open' => (bool) $defaults['id'],
    '#group' => 'advanced',
    '#attached' => [
      'library' => [
        'menu_ui/drupal.menu_ui',
      ],
    ],
    '#tree' => TRUE,
    '#weight' => 10,
    '#attributes' => [
      'class' => [
        'menu-link-form',
      ],
    ],
  ];
  $form['menu']['enabled'] = [
    '#type' => 'checkbox',
    '#title' => t('Provide a menu link'),
    '#default_value' => (int) (bool) $defaults['id'],
  ];
  $form['menu']['link'] = [
    '#type' => 'container',
    '#parents' => [
      'menu',
    ],
    '#states' => [
      'invisible' => [
        'input[name="menu[enabled]"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];

  // Populate the element with the link data.
  foreach ([
    'id',
    'entity_id',
  ] as $key) {
    $form['menu']['link'][$key] = [
      '#type' => 'value',
      '#value' => $defaults[$key],
    ];
  }
  $form['menu']['link']['title'] = [
    '#type' => 'textfield',
    '#title' => t('Menu link title'),
    '#default_value' => $defaults['title'],
    '#maxlength' => $defaults['title_max_length'],
  ];
  $form['menu']['link']['description'] = [
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => $defaults['description'],
    '#rows' => 1,
    '#description' => t('Shown when hovering over the menu link.'),
  ];
  $form['menu']['link']['menu_parent'] = $parent_element;
  $form['menu']['link']['menu_parent']['#title'] = t('Parent item');
  $form['menu']['link']['menu_parent']['#attributes']['class'][] = 'menu-parent-select';
  $form['menu']['link']['weight'] = [
    '#type' => 'number',
    '#title' => t('Weight'),
    '#default_value' => $defaults['weight'],
    '#description' => t('Menu links with lower weights are displayed before links with higher weights.'),
  ];
  foreach (array_keys($form['actions']) as $action) {
    if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
      $form['actions'][$action]['#submit'][] = 'taxonomy_menu_ui_form_taxonomy_term_form_submit';
    }
  }
  $form['#entity_builders'][] = 'taxonomy_menu_ui_taxonomy_term_builder';
}