menu.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * @file
  4. * API for the Drupal menu system.
  5. */
  6. /**
  7. * @addtogroup menu
  8. * @{
  9. */
  10. use Drupal\Component\Render\FormattableMarkup;
  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 = new FormattableMarkup('<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 and is removed from 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. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use
  108. * \Drupal\Core\Menu\LocalTaskManagerInterface::getLocalTasks() instead.
  109. *
  110. * @see https://www.drupal.org/node/2874695
  111. */
  112. function menu_primary_local_tasks() {
  113. @trigger_error(__FUNCTION__ . '() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Menu\LocalTaskManagerInterface::getLocalTasks() instead. See https://www.drupal.org/node/2874695', E_USER_DEPRECATED);
  114. /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
  115. $manager = \Drupal::service('plugin.manager.menu.local_task');
  116. $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 0);
  117. // Do not display single tabs.
  118. return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
  119. }
  120. /**
  121. * Returns the rendered local tasks at the second level.
  122. *
  123. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use
  124. * \Drupal\Core\Menu\LocalTaskManagerInterface::getLocalTasks() instead.
  125. *
  126. * @see https://www.drupal.org/node/2874695
  127. */
  128. function menu_secondary_local_tasks() {
  129. @trigger_error(__FUNCTION__ . '() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Menu\LocalTaskManagerInterface::getLocalTasks() instead. See https://www.drupal.org/node/2874695', E_USER_DEPRECATED);
  130. /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
  131. $manager = \Drupal::service('plugin.manager.menu.local_task');
  132. $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 1);
  133. // Do not display single tabs.
  134. return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
  135. }
  136. /**
  137. * Returns a renderable element for the primary and secondary tabs.
  138. *
  139. * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
  140. * local_tasks_block block or inline theming instead.
  141. *
  142. * @see https://www.drupal.org/node/2874695
  143. */
  144. function menu_local_tabs() {
  145. @trigger_error(__FUNCTION__ . '() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use local_tasks_block block or inline theming instead. See https://www.drupal.org/node/2874695', E_USER_DEPRECATED);
  146. $build = [
  147. '#theme' => 'menu_local_tasks',
  148. '#primary' => menu_primary_local_tasks(),
  149. '#secondary' => menu_secondary_local_tasks(),
  150. ];
  151. return !empty($build['#primary']) || !empty($build['#secondary']) ? $build : [];
  152. }
  153. /**
  154. * Clears all cached menu data.
  155. *
  156. * This should be called any time broad changes
  157. * might have been made to the router items or menu links.
  158. *
  159. * @deprecated in drupal:8.6.0 and is removed from drupal:9.0.0. Use
  160. * \Drupal::cache('menu')->invalidateAll() instead.
  161. *
  162. * @see https://www.drupal.org/node/2989138
  163. */
  164. function menu_cache_clear_all() {
  165. @trigger_error("menu_cache_clear_all() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use \Drupal::cache('menu')->invalidateAll() instead. See https://www.drupal.org/node/2989138", E_USER_DEPRECATED);
  166. \Drupal::cache('menu')->invalidateAll();
  167. }
  168. /**
  169. * @} End of "addtogroup menu".
  170. */