Drupal investigation

menu.inc 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. *
  96. * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
  97. */
  98. function menu_local_tasks($level = 0) {
  99. /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
  100. $manager = \Drupal::service('plugin.manager.menu.local_task');
  101. return $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), $level);
  102. }
  103. /**
  104. * Returns the rendered local tasks at the top level.
  105. *
  106. * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
  107. */
  108. function menu_primary_local_tasks() {
  109. /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
  110. $manager = \Drupal::service('plugin.manager.menu.local_task');
  111. $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 0);
  112. // Do not display single tabs.
  113. return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
  114. }
  115. /**
  116. * Returns the rendered local tasks at the second level.
  117. *
  118. * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
  119. */
  120. function menu_secondary_local_tasks() {
  121. /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
  122. $manager = \Drupal::service('plugin.manager.menu.local_task');
  123. $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 1);
  124. // Do not display single tabs.
  125. return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
  126. }
  127. /**
  128. * Returns a renderable element for the primary and secondary tabs.
  129. */
  130. function menu_local_tabs() {
  131. $build = [
  132. '#theme' => 'menu_local_tasks',
  133. '#primary' => menu_primary_local_tasks(),
  134. '#secondary' => menu_secondary_local_tasks(),
  135. ];
  136. return !empty($build['#primary']) || !empty($build['#secondary']) ? $build : [];
  137. }
  138. /**
  139. * Clears all cached menu data.
  140. *
  141. * This should be called any time broad changes
  142. * might have been made to the router items or menu links.
  143. */
  144. function menu_cache_clear_all() {
  145. \Drupal::cache('menu')->invalidateAll();
  146. }
  147. /**
  148. * @} End of "addtogroup menu".
  149. */