menu.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @file
  4. * API for the Drupal menu system.
  5. */
  6. /**
  7. * @addtogroup menu
  8. * @{
  9. */
  10. use Drupal\Component\Utility\SafeMarkup;
  11. use Drupal\Core\Render\Element;
  12. /**
  13. * Prepares variables for single local task link templates.
  14. *
  15. * Default template: menu-local-task.html.twig.
  16. *
  17. * @param array $variables
  18. * An associative array containing:
  19. * - element: A render element containing:
  20. * - #link: A menu link array with 'title', 'url', and (optionally)
  21. * 'localized_options' keys.
  22. * - #active: A boolean indicating whether the local task is active.
  23. */
  24. function template_preprocess_menu_local_task(&$variables) {
  25. $link = $variables['element']['#link'];
  26. $link += [
  27. 'localized_options' => [],
  28. ];
  29. $link_text = $link['title'];
  30. if (!empty($variables['element']['#active'])) {
  31. $variables['is_active'] = TRUE;
  32. // Add text to indicate active tab for non-visual users.
  33. $active = SafeMarkup::format('<span class="visually-hidden">@label</span>', ['@label' => t('(active tab)')]);
  34. $link_text = t('@local-task-title@active', ['@local-task-title' => $link_text, '@active' => $active]);
  35. }
  36. $link['localized_options']['set_active_class'] = TRUE;
  37. $variables['link'] = [
  38. '#type' => 'link',
  39. '#title' => $link_text,
  40. '#url' => $link['url'],
  41. '#options' => $link['localized_options'],
  42. ];
  43. }
  44. /**
  45. * Prepares variables for single local action link templates.
  46. *
  47. * Default template: menu-local-action.html.twig.
  48. *
  49. * @param array $variables
  50. * An associative array containing:
  51. * - element: A render element containing:
  52. * - #link: A menu link array with 'title', 'url', and (optionally)
  53. * 'localized_options' keys.
  54. */
  55. function template_preprocess_menu_local_action(&$variables) {
  56. $link = $variables['element']['#link'];
  57. $link += [
  58. 'localized_options' => [],
  59. ];
  60. $link['localized_options']['attributes']['class'][] = 'button';
  61. $link['localized_options']['attributes']['class'][] = 'button-action';
  62. $link['localized_options']['set_active_class'] = TRUE;
  63. $variables['link'] = [
  64. '#type' => 'link',
  65. '#title' => $link['title'],
  66. '#options' => $link['localized_options'],
  67. '#url' => $link['url'],
  68. ];
  69. }
  70. /**
  71. * Returns an array containing the names of system-defined (default) menus.
  72. */
  73. function menu_list_system_menus() {
  74. return [
  75. 'tools' => 'Tools',
  76. 'admin' => 'Administration',
  77. 'account' => 'User account menu',
  78. 'main' => 'Main navigation',
  79. 'footer' => 'Footer menu',
  80. ];
  81. }
  82. /**
  83. * Collects the local tasks (tabs) for the current route.
  84. *
  85. * @param int $level
  86. * The level of tasks you ask for. Primary tasks are 0, secondary are 1.
  87. *
  88. * @return array
  89. * An array containing
  90. * - tabs: Local tasks for the requested level.
  91. * - route_name: The route name for the current page used to collect the local
  92. * tasks.
  93. *
  94. * @see hook_menu_local_tasks_alter()
  95. * @see https://www.drupal.org/node/2544940
  96. *
  97. * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
  98. */
  99. function menu_local_tasks($level = 0) {
  100. /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
  101. $manager = \Drupal::service('plugin.manager.menu.local_task');
  102. return $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), $level);
  103. }
  104. /**
  105. * Returns the rendered local tasks at the top level.
  106. *
  107. * @see https://www.drupal.org/node/2874695
  108. *
  109. * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
  110. */
  111. function menu_primary_local_tasks() {
  112. /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
  113. $manager = \Drupal::service('plugin.manager.menu.local_task');
  114. $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 0);
  115. // Do not display single tabs.
  116. return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
  117. }
  118. /**
  119. * Returns the rendered local tasks at the second level.
  120. *
  121. * @see https://www.drupal.org/node/2874695
  122. *
  123. * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
  124. */
  125. function menu_secondary_local_tasks() {
  126. /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
  127. $manager = \Drupal::service('plugin.manager.menu.local_task');
  128. $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 1);
  129. // Do not display single tabs.
  130. return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
  131. }
  132. /**
  133. * Returns a renderable element for the primary and secondary tabs.
  134. */
  135. function menu_local_tabs() {
  136. $build = [
  137. '#theme' => 'menu_local_tasks',
  138. '#primary' => menu_primary_local_tasks(),
  139. '#secondary' => menu_secondary_local_tasks(),
  140. ];
  141. return !empty($build['#primary']) || !empty($build['#secondary']) ? $build : [];
  142. }
  143. /**
  144. * Clears all cached menu data.
  145. *
  146. * This should be called any time broad changes
  147. * might have been made to the router items or menu links.
  148. */
  149. function menu_cache_clear_all() {
  150. \Drupal::cache('menu')->invalidateAll();
  151. }
  152. /**
  153. * @} End of "addtogroup menu".
  154. */