annotate core/modules/menu_ui/menu_ui.module @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Allows administrators to customize the site's navigation menus.
Chris@0 6 *
Chris@0 7 * A menu (in this context) is a hierarchical collection of links, generally
Chris@0 8 * used for navigation.
Chris@0 9 */
Chris@0 10
Chris@18 11 use Drupal\Core\Url;
Chris@0 12 use Drupal\Core\Breadcrumb\Breadcrumb;
Chris@0 13 use Drupal\Core\Cache\CacheableMetadata;
Chris@0 14 use Drupal\Core\Block\BlockPluginInterface;
Chris@0 15 use Drupal\Core\Link;
Chris@0 16 use Drupal\Core\Menu\MenuLinkInterface;
Chris@0 17 use Drupal\Core\Form\FormStateInterface;
Chris@0 18 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 19 use Drupal\menu_link_content\Entity\MenuLinkContent;
Chris@0 20 use Drupal\node\NodeTypeInterface;
Chris@0 21 use Drupal\system\Entity\Menu;
Chris@0 22 use Drupal\node\NodeInterface;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Maximum length of menu name as entered by the user.
Chris@0 26 *
Chris@0 27 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. Use
Chris@0 28 * \Drupal\Core\Config\Entity\ConfigEntityStorage::MAX_ID_LENGTH because the
Chris@0 29 * menu name is a configuration entity ID.
Chris@0 30 *
Chris@0 31 * @see https://www.drupal.org/node/2831620
Chris@0 32 */
Chris@0 33 const MENU_MAX_MENU_NAME_LENGTH_UI = 27;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Implements hook_help().
Chris@0 37 */
Chris@0 38 function menu_ui_help($route_name, RouteMatchInterface $route_match) {
Chris@0 39 switch ($route_name) {
Chris@0 40 case 'help.page.menu_ui':
Chris@0 41 $output = '';
Chris@0 42 $output .= '<h3>' . t('About') . '</h3>';
Chris@0 43 $output .= '<p>' . t('The Menu UI module provides an interface for managing menus. A menu is a hierarchical collection of links, which can be within or external to the site, generally used for navigation. For more information, see the <a href=":menu">online documentation for the Menu UI module</a>.', [':menu' => 'https://www.drupal.org/documentation/modules/menu/']) . '</p>';
Chris@0 44 $output .= '<h3>' . t('Uses') . '</h3>';
Chris@0 45 $output .= '<dl>';
Chris@0 46 $output .= '<dt>' . t('Managing menus') . '</dt>';
Chris@18 47 $output .= '<dd>' . t('Users with the <em>Administer menus and menu items</em> permission can add, edit, and delete custom menus on the <a href=":menu">Menus page</a>. Custom menus can be special site menus, menus of external links, or any combination of internal and external links. You may create an unlimited number of additional menus, each of which will automatically have an associated block (if you have the <a href=":block_help">Block module</a> installed). By selecting <em>Edit menu</em>, you can add, edit, or delete links for a given menu. The links listing page provides a drag-and-drop interface for controlling the order of links, and creating a hierarchy within the menu.', [':block_help' => (\Drupal::moduleHandler()->moduleExists('block')) ? Url::fromRoute('help.page', ['name' => 'block'])->toString() : '#', ':menu' => Url::fromRoute('entity.menu.collection')->toString()]) . '</dd>';
Chris@0 48 $output .= '<dt>' . t('Displaying menus') . '</dt>';
Chris@18 49 $output .= '<dd>' . t('If you have the Block module enabled, then each menu that you create is rendered in a block that you enable and position on the <a href=":blocks">Block layout page</a>. In some <a href=":themes">themes</a>, the main menu and possibly the secondary menu will be output automatically; you may be able to disable this behavior on the <a href=":themes">theme\'s settings page</a>.', [':blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? Url::fromRoute('block.admin_display')->toString() : '#', ':themes' => Url::fromRoute('system.themes_page')->toString(), ':theme_settings' => Url::fromRoute('system.theme_settings')->toString()]) . '</dd>';
Chris@0 50 $output .= '</dl>';
Chris@0 51 return $output;
Chris@0 52 }
Chris@0 53 if ($route_name == 'entity.menu.add_form' && \Drupal::moduleHandler()->moduleExists('block') && \Drupal::currentUser()->hasPermission('administer blocks')) {
Chris@18 54 return '<p>' . t('You can enable the newly-created block for this menu on the <a href=":blocks">Block layout page</a>.', [':blocks' => Url::fromRoute('block.admin_display')->toString()]) . '</p>';
Chris@0 55 }
Chris@0 56 elseif ($route_name == 'entity.menu.collection' && \Drupal::moduleHandler()->moduleExists('block') && \Drupal::currentUser()->hasPermission('administer blocks')) {
Chris@18 57 return '<p>' . t('Each menu has a corresponding block that is managed on the <a href=":blocks">Block layout page</a>.', [':blocks' => Url::fromRoute('block.admin_display')->toString()]) . '</p>';
Chris@0 58 }
Chris@0 59 }
Chris@0 60
Chris@0 61 /**
Chris@0 62 * Implements hook_entity_type_build().
Chris@0 63 */
Chris@0 64 function menu_ui_entity_type_build(array &$entity_types) {
Chris@0 65 /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
Chris@0 66 $entity_types['menu']
Chris@0 67 ->setFormClass('add', 'Drupal\menu_ui\MenuForm')
Chris@0 68 ->setFormClass('edit', 'Drupal\menu_ui\MenuForm')
Chris@0 69 ->setFormClass('delete', 'Drupal\menu_ui\Form\MenuDeleteForm')
Chris@0 70 ->setListBuilderClass('Drupal\menu_ui\MenuListBuilder')
Chris@0 71 ->setLinkTemplate('add-form', '/admin/structure/menu/add')
Chris@0 72 ->setLinkTemplate('delete-form', '/admin/structure/menu/manage/{menu}/delete')
Chris@0 73 ->setLinkTemplate('edit-form', '/admin/structure/menu/manage/{menu}')
Chris@0 74 ->setLinkTemplate('add-link-form', '/admin/structure/menu/manage/{menu}/add')
Chris@0 75 ->setLinkTemplate('collection', '/admin/structure/menu');
Chris@0 76
Chris@0 77 if (isset($entity_types['node'])) {
Chris@0 78 $entity_types['node']->addConstraint('MenuSettings', []);
Chris@0 79 }
Chris@0 80 }
Chris@0 81
Chris@0 82 /**
Chris@0 83 * Implements hook_block_view_BASE_BLOCK_ID_alter() for 'system_menu_block'.
Chris@0 84 */
Chris@0 85 function menu_ui_block_view_system_menu_block_alter(array &$build, BlockPluginInterface $block) {
Chris@0 86 if ($block->getBaseId() == 'system_menu_block') {
Chris@0 87 $menu_name = $block->getDerivativeId();
Chris@0 88 $build['#contextual_links']['menu'] = [
Chris@0 89 'route_parameters' => ['menu' => $menu_name],
Chris@0 90 ];
Chris@0 91 }
Chris@0 92 }
Chris@0 93
Chris@0 94 /**
Chris@0 95 * Helper function to create or update a menu link for a node.
Chris@0 96 *
Chris@0 97 * @param \Drupal\node\NodeInterface $node
Chris@0 98 * Node entity.
Chris@0 99 * @param array $values
Chris@0 100 * Values for the menu link.
Chris@0 101 */
Chris@0 102 function _menu_ui_node_save(NodeInterface $node, array $values) {
Chris@0 103 /** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
Chris@0 104 if (!empty($values['entity_id'])) {
Chris@0 105 $entity = MenuLinkContent::load($values['entity_id']);
Chris@0 106 if ($entity->isTranslatable()) {
Chris@0 107 if (!$entity->hasTranslation($node->language()->getId())) {
Chris@0 108 $entity = $entity->addTranslation($node->language()->getId(), $entity->toArray());
Chris@0 109 }
Chris@0 110 else {
Chris@0 111 $entity = $entity->getTranslation($node->language()->getId());
Chris@0 112 }
Chris@0 113 }
Chris@0 114 }
Chris@0 115 else {
Chris@0 116 // Create a new menu_link_content entity.
Chris@0 117 $entity = MenuLinkContent::create([
Chris@0 118 'link' => ['uri' => 'entity:node/' . $node->id()],
Chris@0 119 'langcode' => $node->language()->getId(),
Chris@0 120 ]);
Chris@0 121 $entity->enabled->value = 1;
Chris@0 122 }
Chris@0 123 $entity->title->value = trim($values['title']);
Chris@0 124 $entity->description->value = trim($values['description']);
Chris@0 125 $entity->menu_name->value = $values['menu_name'];
Chris@0 126 $entity->parent->value = $values['parent'];
Chris@0 127 $entity->weight->value = isset($values['weight']) ? $values['weight'] : 0;
Chris@0 128 $entity->save();
Chris@0 129 }
Chris@0 130
Chris@0 131 /**
Chris@0 132 * Returns the definition for a menu link for the given node.
Chris@0 133 *
Chris@0 134 * @param \Drupal\node\NodeInterface $node
Chris@0 135 * The node entity.
Chris@0 136 *
Chris@0 137 * @return array
Chris@0 138 * An array that contains default values for the menu link form.
Chris@0 139 */
Chris@0 140 function menu_ui_get_menu_link_defaults(NodeInterface $node) {
Chris@0 141 // Prepare the definition for the edit form.
Chris@0 142 /** @var \Drupal\node\NodeTypeInterface $node_type */
Chris@0 143 $node_type = $node->type->entity;
Chris@0 144 $menu_name = strtok($node_type->getThirdPartySetting('menu_ui', 'parent', 'main:'), ':');
Chris@0 145 $defaults = FALSE;
Chris@0 146 if ($node->id()) {
Chris@0 147 $id = FALSE;
Chris@0 148 // Give priority to the default menu
Chris@0 149 $type_menus = $node_type->getThirdPartySetting('menu_ui', 'available_menus', ['main']);
Chris@0 150 if (in_array($menu_name, $type_menus)) {
Chris@0 151 $query = \Drupal::entityQuery('menu_link_content')
Chris@0 152 ->condition('link.uri', 'node/' . $node->id())
Chris@0 153 ->condition('menu_name', $menu_name)
Chris@0 154 ->sort('id', 'ASC')
Chris@0 155 ->range(0, 1);
Chris@0 156 $result = $query->execute();
Chris@0 157
Chris@0 158 $id = (!empty($result)) ? reset($result) : FALSE;
Chris@0 159 }
Chris@0 160 // Check all allowed menus if a link does not exist in the default menu.
Chris@0 161 if (!$id && !empty($type_menus)) {
Chris@0 162 $query = \Drupal::entityQuery('menu_link_content')
Chris@0 163 ->condition('link.uri', 'entity:node/' . $node->id())
Chris@0 164 ->condition('menu_name', array_values($type_menus), 'IN')
Chris@0 165 ->sort('id', 'ASC')
Chris@0 166 ->range(0, 1);
Chris@0 167 $result = $query->execute();
Chris@0 168
Chris@0 169 $id = (!empty($result)) ? reset($result) : FALSE;
Chris@0 170 }
Chris@0 171 if ($id) {
Chris@0 172 $menu_link = MenuLinkContent::load($id);
Chris@0 173 $menu_link = \Drupal::service('entity.repository')->getTranslationFromContext($menu_link);
Chris@0 174 $defaults = [
Chris@0 175 'entity_id' => $menu_link->id(),
Chris@0 176 'id' => $menu_link->getPluginId(),
Chris@0 177 'title' => $menu_link->getTitle(),
Chris@0 178 'title_max_length' => $menu_link->getFieldDefinitions()['title']->getSetting('max_length'),
Chris@0 179 'description' => $menu_link->getDescription(),
Chris@17 180 'description_max_length' => $menu_link->getFieldDefinitions()['description']->getSetting('max_length'),
Chris@0 181 'menu_name' => $menu_link->getMenuName(),
Chris@0 182 'parent' => $menu_link->getParentId(),
Chris@0 183 'weight' => $menu_link->getWeight(),
Chris@0 184 ];
Chris@0 185 }
Chris@0 186 }
Chris@0 187
Chris@0 188 if (!$defaults) {
Chris@0 189 // Get the default max_length of a menu link title from the base field
Chris@0 190 // definition.
Chris@0 191 $field_definitions = \Drupal::entityManager()->getBaseFieldDefinitions('menu_link_content');
Chris@0 192 $max_length = $field_definitions['title']->getSetting('max_length');
Chris@17 193 $description_max_length = $field_definitions['description']->getSetting('max_length');
Chris@0 194 $defaults = [
Chris@0 195 'entity_id' => 0,
Chris@0 196 'id' => '',
Chris@0 197 'title' => '',
Chris@0 198 'title_max_length' => $max_length,
Chris@0 199 'description' => '',
Chris@17 200 'description_max_length' => $description_max_length,
Chris@0 201 'menu_name' => $menu_name,
Chris@0 202 'parent' => '',
Chris@0 203 'weight' => 0,
Chris@0 204 ];
Chris@0 205 }
Chris@0 206 return $defaults;
Chris@0 207 }
Chris@0 208
Chris@0 209 /**
Chris@0 210 * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
Chris@0 211 *
Chris@0 212 * Adds menu item fields to the node form.
Chris@0 213 *
Chris@0 214 * @see menu_ui_form_node_form_submit()
Chris@0 215 */
Chris@0 216 function menu_ui_form_node_form_alter(&$form, FormStateInterface $form_state) {
Chris@0 217 // Generate a list of possible parents (not including this link or descendants).
Chris@0 218 // @todo This must be handled in a #process handler.
Chris@0 219 $node = $form_state->getFormObject()->getEntity();
Chris@0 220 $defaults = menu_ui_get_menu_link_defaults($node);
Chris@0 221 /** @var \Drupal\node\NodeTypeInterface $node_type */
Chris@0 222 $node_type = $node->type->entity;
Chris@0 223 /** @var \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector */
Chris@0 224 $menu_parent_selector = \Drupal::service('menu.parent_form_selector');
Chris@17 225 $type_menus_ids = $node_type->getThirdPartySetting('menu_ui', 'available_menus', ['main']);
Chris@17 226 if (empty($type_menus_ids)) {
Chris@17 227 return;
Chris@17 228 }
Chris@17 229 /** @var \Drupal\system\MenuInterface[] $type_menus */
Chris@17 230 $type_menus = Menu::loadMultiple($type_menus_ids);
Chris@0 231 $available_menus = [];
Chris@0 232 foreach ($type_menus as $menu) {
Chris@17 233 $available_menus[$menu->id()] = $menu->label();
Chris@0 234 }
Chris@0 235 if ($defaults['id']) {
Chris@0 236 $default = $defaults['menu_name'] . ':' . $defaults['parent'];
Chris@0 237 }
Chris@0 238 else {
Chris@0 239 $default = $node_type->getThirdPartySetting('menu_ui', 'parent', 'main:');
Chris@0 240 }
Chris@0 241 $parent_element = $menu_parent_selector->parentSelectElement($default, $defaults['id'], $available_menus);
Chris@0 242 // If no possible parent menu items were found, there is nothing to display.
Chris@0 243 if (empty($parent_element)) {
Chris@0 244 return;
Chris@0 245 }
Chris@0 246
Chris@0 247 $form['menu'] = [
Chris@0 248 '#type' => 'details',
Chris@0 249 '#title' => t('Menu settings'),
Chris@0 250 '#access' => \Drupal::currentUser()->hasPermission('administer menu'),
Chris@0 251 '#open' => (bool) $defaults['id'],
Chris@0 252 '#group' => 'advanced',
Chris@0 253 '#attached' => [
Chris@0 254 'library' => ['menu_ui/drupal.menu_ui'],
Chris@0 255 ],
Chris@0 256 '#tree' => TRUE,
Chris@0 257 '#weight' => -2,
Chris@0 258 '#attributes' => ['class' => ['menu-link-form']],
Chris@0 259 ];
Chris@0 260 $form['menu']['enabled'] = [
Chris@0 261 '#type' => 'checkbox',
Chris@0 262 '#title' => t('Provide a menu link'),
Chris@0 263 '#default_value' => (int) (bool) $defaults['id'],
Chris@0 264 ];
Chris@0 265 $form['menu']['link'] = [
Chris@0 266 '#type' => 'container',
Chris@0 267 '#parents' => ['menu'],
Chris@0 268 '#states' => [
Chris@0 269 'invisible' => [
Chris@0 270 'input[name="menu[enabled]"]' => ['checked' => FALSE],
Chris@0 271 ],
Chris@0 272 ],
Chris@0 273 ];
Chris@0 274
Chris@0 275 // Populate the element with the link data.
Chris@0 276 foreach (['id', 'entity_id'] as $key) {
Chris@0 277 $form['menu']['link'][$key] = ['#type' => 'value', '#value' => $defaults[$key]];
Chris@0 278 }
Chris@0 279
Chris@0 280 $form['menu']['link']['title'] = [
Chris@0 281 '#type' => 'textfield',
Chris@0 282 '#title' => t('Menu link title'),
Chris@0 283 '#default_value' => $defaults['title'],
Chris@0 284 '#maxlength' => $defaults['title_max_length'],
Chris@0 285 ];
Chris@0 286
Chris@0 287 $form['menu']['link']['description'] = [
Chris@17 288 '#type' => 'textfield',
Chris@0 289 '#title' => t('Description'),
Chris@0 290 '#default_value' => $defaults['description'],
Chris@0 291 '#description' => t('Shown when hovering over the menu link.'),
Chris@17 292 '#maxlength' => $defaults['description_max_length'],
Chris@0 293 ];
Chris@0 294
Chris@0 295 $form['menu']['link']['menu_parent'] = $parent_element;
Chris@0 296 $form['menu']['link']['menu_parent']['#title'] = t('Parent item');
Chris@0 297 $form['menu']['link']['menu_parent']['#attributes']['class'][] = 'menu-parent-select';
Chris@0 298
Chris@0 299 $form['menu']['link']['weight'] = [
Chris@0 300 '#type' => 'number',
Chris@0 301 '#title' => t('Weight'),
Chris@0 302 '#default_value' => $defaults['weight'],
Chris@0 303 '#description' => t('Menu links with lower weights are displayed before links with higher weights.'),
Chris@0 304 ];
Chris@0 305
Chris@0 306 foreach (array_keys($form['actions']) as $action) {
Chris@0 307 if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
Chris@0 308 $form['actions'][$action]['#submit'][] = 'menu_ui_form_node_form_submit';
Chris@0 309 }
Chris@0 310 }
Chris@0 311
Chris@0 312 $form['#entity_builders'][] = 'menu_ui_node_builder';
Chris@0 313 }
Chris@0 314
Chris@0 315 /**
Chris@0 316 * Entity form builder to add the menu information to the node.
Chris@0 317 */
Chris@0 318 function menu_ui_node_builder($entity_type, NodeInterface $entity, &$form, FormStateInterface $form_state) {
Chris@0 319 $entity->menu = $form_state->getValue('menu');
Chris@0 320 }
Chris@0 321
Chris@0 322 /**
Chris@0 323 * Form submission handler for menu item field on the node form.
Chris@0 324 *
Chris@0 325 * @see menu_ui_form_node_form_alter()
Chris@0 326 */
Chris@0 327 function menu_ui_form_node_form_submit($form, FormStateInterface $form_state) {
Chris@0 328 $node = $form_state->getFormObject()->getEntity();
Chris@0 329 if (!$form_state->isValueEmpty('menu')) {
Chris@0 330 $values = $form_state->getValue('menu');
Chris@0 331 if (empty($values['enabled'])) {
Chris@0 332 if ($values['entity_id']) {
Chris@0 333 $entity = MenuLinkContent::load($values['entity_id']);
Chris@0 334 $entity->delete();
Chris@0 335 }
Chris@0 336 }
Chris@0 337 elseif (trim($values['title'])) {
Chris@0 338 // Decompose the selected menu parent option into 'menu_name' and 'parent',
Chris@0 339 // if the form used the default parent selection widget.
Chris@0 340 if (!empty($values['menu_parent'])) {
Chris@0 341 list($menu_name, $parent) = explode(':', $values['menu_parent'], 2);
Chris@0 342 $values['menu_name'] = $menu_name;
Chris@0 343 $values['parent'] = $parent;
Chris@0 344 }
Chris@0 345 _menu_ui_node_save($node, $values);
Chris@0 346 }
Chris@0 347 }
Chris@0 348 }
Chris@0 349
Chris@0 350 /**
Chris@0 351 * Implements hook_form_FORM_ID_alter() for \Drupal\node\NodeTypeForm.
Chris@0 352 *
Chris@0 353 * Adds menu options to the node type form.
Chris@0 354 *
Chris@0 355 * @see NodeTypeForm::form()
Chris@0 356 * @see menu_ui_form_node_type_form_submit()
Chris@0 357 */
Chris@0 358 function menu_ui_form_node_type_form_alter(&$form, FormStateInterface $form_state) {
Chris@0 359 /** @var \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector */
Chris@0 360 $menu_parent_selector = \Drupal::service('menu.parent_form_selector');
Chris@0 361 $menu_options = menu_ui_get_menus();
Chris@0 362 /** @var \Drupal\node\NodeTypeInterface $type */
Chris@0 363 $type = $form_state->getFormObject()->getEntity();
Chris@0 364 $form['menu'] = [
Chris@0 365 '#type' => 'details',
Chris@0 366 '#title' => t('Menu settings'),
Chris@0 367 '#attached' => [
Chris@0 368 'library' => ['menu_ui/drupal.menu_ui.admin'],
Chris@0 369 ],
Chris@0 370 '#group' => 'additional_settings',
Chris@0 371 ];
Chris@0 372 $form['menu']['menu_options'] = [
Chris@0 373 '#type' => 'checkboxes',
Chris@0 374 '#title' => t('Available menus'),
Chris@0 375 '#default_value' => $type->getThirdPartySetting('menu_ui', 'available_menus', ['main']),
Chris@0 376 '#options' => $menu_options,
Chris@0 377 '#description' => t('The menus available to place links in for this content type.'),
Chris@0 378 ];
Chris@0 379 // @todo See if we can avoid pre-loading all options by changing the form or
Chris@0 380 // using a #process callback. https://www.drupal.org/node/2310319
Chris@0 381 // To avoid an 'illegal option' error after saving the form we have to load
Chris@0 382 // all available menu parents. Otherwise, it is not possible to dynamically
Chris@0 383 // add options to the list using ajax.
Chris@0 384 $options_cacheability = new CacheableMetadata();
Chris@0 385 $options = $menu_parent_selector->getParentSelectOptions('', NULL, $options_cacheability);
Chris@0 386 $form['menu']['menu_parent'] = [
Chris@0 387 '#type' => 'select',
Chris@0 388 '#title' => t('Default parent item'),
Chris@0 389 '#default_value' => $type->getThirdPartySetting('menu_ui', 'parent', 'main:'),
Chris@0 390 '#options' => $options,
Chris@0 391 '#description' => t('Choose the menu item to be the default parent for a new link in the content authoring form.'),
Chris@0 392 '#attributes' => ['class' => ['menu-title-select']],
Chris@0 393 ];
Chris@0 394 $options_cacheability->applyTo($form['menu']['menu_parent']);
Chris@0 395
Chris@0 396 $form['#validate'][] = 'menu_ui_form_node_type_form_validate';
Chris@0 397 $form['#entity_builders'][] = 'menu_ui_form_node_type_form_builder';
Chris@0 398 }
Chris@0 399
Chris@0 400 /**
Chris@0 401 * Validate handler for forms with menu options.
Chris@0 402 *
Chris@0 403 * @see menu_ui_form_node_type_form_alter()
Chris@0 404 */
Chris@0 405 function menu_ui_form_node_type_form_validate(&$form, FormStateInterface $form_state) {
Chris@0 406 $available_menus = array_filter($form_state->getValue('menu_options'));
Chris@0 407 // If there is at least one menu allowed, the selected item should be in
Chris@0 408 // one of them.
Chris@0 409 if (count($available_menus)) {
Chris@0 410 $menu_item_id_parts = explode(':', $form_state->getValue('menu_parent'));
Chris@0 411 if (!in_array($menu_item_id_parts[0], $available_menus)) {
Chris@0 412 $form_state->setErrorByName('menu_parent', t('The selected menu item is not under one of the selected menus.'));
Chris@0 413 }
Chris@0 414 }
Chris@0 415 else {
Chris@0 416 $form_state->setValue('menu_parent', '');
Chris@0 417 }
Chris@0 418 }
Chris@0 419
Chris@0 420 /**
Chris@0 421 * Entity builder for the node type form with menu options.
Chris@0 422 *
Chris@0 423 * @see menu_ui_form_node_type_form_alter()
Chris@0 424 */
Chris@0 425 function menu_ui_form_node_type_form_builder($entity_type, NodeTypeInterface $type, &$form, FormStateInterface $form_state) {
Chris@0 426 $type->setThirdPartySetting('menu_ui', 'available_menus', array_values(array_filter($form_state->getValue('menu_options'))));
Chris@0 427 $type->setThirdPartySetting('menu_ui', 'parent', $form_state->getValue('menu_parent'));
Chris@0 428 }
Chris@0 429
Chris@0 430 /**
Chris@0 431 * Return an associative array of the custom menus names.
Chris@0 432 *
Chris@0 433 * @param bool $all
Chris@0 434 * (optional) If FALSE return only user-added menus, or if TRUE also include
Chris@0 435 * the menus defined by the system. Defaults to TRUE.
Chris@0 436 *
Chris@0 437 * @return array
Chris@0 438 * An array with the machine-readable names as the keys, and human-readable
Chris@0 439 * titles as the values.
Chris@0 440 */
Chris@0 441 function menu_ui_get_menus($all = TRUE) {
Chris@0 442 if ($custom_menus = Menu::loadMultiple()) {
Chris@0 443 if (!$all) {
Chris@0 444 $custom_menus = array_diff_key($custom_menus, menu_list_system_menus());
Chris@0 445 }
Chris@0 446 foreach ($custom_menus as $menu_name => $menu) {
Chris@0 447 $custom_menus[$menu_name] = $menu->label();
Chris@0 448 }
Chris@0 449 asort($custom_menus);
Chris@0 450 }
Chris@0 451 return $custom_menus;
Chris@0 452 }
Chris@0 453
Chris@0 454 /**
Chris@0 455 * Implements hook_preprocess_HOOK() for block templates.
Chris@0 456 */
Chris@0 457 function menu_ui_preprocess_block(&$variables) {
Chris@0 458 if ($variables['configuration']['provider'] == 'menu_ui') {
Chris@0 459 $variables['attributes']['role'] = 'navigation';
Chris@0 460 }
Chris@0 461 }
Chris@0 462
Chris@0 463 /**
Chris@0 464 * Implements hook_system_breadcrumb_alter().
Chris@0 465 */
Chris@0 466 function menu_ui_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInterface $route_match, array $context) {
Chris@0 467 // Custom breadcrumb behavior for editing menu links, we append a link to
Chris@0 468 // the menu in which the link is found.
Chris@0 469 if (($route_match->getRouteName() == 'menu_ui.link_edit') && $menu_link = $route_match->getParameter('menu_link_plugin')) {
Chris@0 470 if (($menu_link instanceof MenuLinkInterface)) {
Chris@0 471 // Add a link to the menu admin screen.
Chris@0 472 $menu = Menu::load($menu_link->getMenuName());
Chris@0 473 $breadcrumb->addLink(Link::createFromRoute($menu->label(), 'entity.menu.edit_form', ['menu' => $menu->id()]));
Chris@0 474 }
Chris@0 475 }
Chris@0 476 }