5 * API for the Drupal menu system.
13 use Drupal\Component\Utility\SafeMarkup;
14 use Drupal\Core\Render\Element;
18 * Prepares variables for single local task link templates.
20 * Default template: menu-local-task.html.twig.
22 * @param array $variables
23 * An associative array containing:
24 * - element: A render element containing:
25 * - #link: A menu link array with 'title', 'url', and (optionally)
26 * 'localized_options' keys.
27 * - #active: A boolean indicating whether the local task is active.
29 function template_preprocess_menu_local_task(&$variables) {
30 $link = $variables['element']['#link'];
32 'localized_options' => [],
34 $link_text = $link['title'];
36 if (!empty($variables['element']['#active'])) {
37 $variables['is_active'] = TRUE;
39 // Add text to indicate active tab for non-visual users.
40 $active = SafeMarkup::format('<span class="visually-hidden">@label</span>', ['@label' => t('(active tab)')]);
41 $link_text = t('@local-task-title@active', ['@local-task-title' => $link_text, '@active' => $active]);
44 $link['localized_options']['set_active_class'] = TRUE;
46 $variables['link'] = [
48 '#title' => $link_text,
49 '#url' => $link['url'],
50 '#options' => $link['localized_options'],
55 * Prepares variables for single local action link templates.
57 * Default template: menu-local-action.html.twig.
59 * @param array $variables
60 * An associative array containing:
61 * - element: A render element containing:
62 * - #link: A menu link array with 'title', 'url', and (optionally)
63 * 'localized_options' keys.
65 function template_preprocess_menu_local_action(&$variables) {
66 $link = $variables['element']['#link'];
68 'localized_options' => [],
70 $link['localized_options']['attributes']['class'][] = 'button';
71 $link['localized_options']['attributes']['class'][] = 'button-action';
72 $link['localized_options']['set_active_class'] = TRUE;
74 $variables['link'] = [
76 '#title' => $link['title'],
77 '#options' => $link['localized_options'],
78 '#url' => $link['url'],
83 * Returns an array containing the names of system-defined (default) menus.
85 function menu_list_system_menus() {
88 'admin' => 'Administration',
89 'account' => 'User account menu',
90 'main' => 'Main navigation',
91 'footer' => 'Footer menu',
96 * Collects the local tasks (tabs) for the current route.
99 * The level of tasks you ask for. Primary tasks are 0, secondary are 1.
102 * An array containing
103 * - tabs: Local tasks for the requested level.
104 * - route_name: The route name for the current page used to collect the local
107 * @see hook_menu_local_tasks_alter()
109 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
111 function menu_local_tasks($level = 0) {
112 /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
113 $manager = \Drupal::service('plugin.manager.menu.local_task');
114 return $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), $level);
118 * Returns the rendered local tasks at the top level.
120 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
122 function menu_primary_local_tasks() {
123 /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
124 $manager = \Drupal::service('plugin.manager.menu.local_task');
125 $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 0);
126 // Do not display single tabs.
127 return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
131 * Returns the rendered local tasks at the second level.
133 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
135 function menu_secondary_local_tasks() {
136 /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
137 $manager = \Drupal::service('plugin.manager.menu.local_task');
138 $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 1);
139 // Do not display single tabs.
140 return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
144 * Returns a renderable element for the primary and secondary tabs.
146 function menu_local_tabs() {
148 '#theme' => 'menu_local_tasks',
149 '#primary' => menu_primary_local_tasks(),
150 '#secondary' => menu_secondary_local_tasks(),
152 return !empty($build['#primary']) || !empty($build['#secondary']) ? $build : [];
156 * Clears all cached menu data.
158 * This should be called any time broad changes
159 * might have been made to the router items or menu links.
161 function menu_cache_clear_all() {
162 \Drupal::cache('menu')->invalidateAll();
166 * @} End of "addtogroup menu".