5 * API for the Drupal menu system.
13 use Drupal\Component\Render\FormattableMarkup;
14 use Drupal\Core\Render\Element;
17 * Prepares variables for single local task link templates.
19 * Default template: menu-local-task.html.twig.
21 * @param array $variables
22 * An associative array containing:
23 * - element: A render element containing:
24 * - #link: A menu link array with 'title', 'url', and (optionally)
25 * 'localized_options' keys.
26 * - #active: A boolean indicating whether the local task is active.
28 function template_preprocess_menu_local_task(&$variables) {
29 $link = $variables['element']['#link'];
31 'localized_options' => [],
33 $link_text = $link['title'];
35 if (!empty($variables['element']['#active'])) {
36 $variables['is_active'] = TRUE;
38 // Add text to indicate active tab for non-visual users.
39 $active = new FormattableMarkup('<span class="visually-hidden">@label</span>', ['@label' => t('(active tab)')]);
40 $link_text = t('@local-task-title@active', ['@local-task-title' => $link_text, '@active' => $active]);
43 $link['localized_options']['set_active_class'] = TRUE;
45 $variables['link'] = [
47 '#title' => $link_text,
48 '#url' => $link['url'],
49 '#options' => $link['localized_options'],
54 * Prepares variables for single local action link templates.
56 * Default template: menu-local-action.html.twig.
58 * @param array $variables
59 * An associative array containing:
60 * - element: A render element containing:
61 * - #link: A menu link array with 'title', 'url', and (optionally)
62 * 'localized_options' keys.
64 function template_preprocess_menu_local_action(&$variables) {
65 $link = $variables['element']['#link'];
67 'localized_options' => [],
69 $link['localized_options']['attributes']['class'][] = 'button';
70 $link['localized_options']['attributes']['class'][] = 'button-action';
71 $link['localized_options']['set_active_class'] = TRUE;
73 $variables['link'] = [
75 '#title' => $link['title'],
76 '#options' => $link['localized_options'],
77 '#url' => $link['url'],
82 * Returns an array containing the names of system-defined (default) menus.
84 function menu_list_system_menus() {
87 'admin' => 'Administration',
88 'account' => 'User account menu',
89 'main' => 'Main navigation',
90 'footer' => 'Footer menu',
95 * Collects the local tasks (tabs) for the current route.
98 * The level of tasks you ask for. Primary tasks are 0, secondary are 1.
101 * An array containing
102 * - tabs: Local tasks for the requested level.
103 * - route_name: The route name for the current page used to collect the local
106 * @see hook_menu_local_tasks_alter()
107 * @see https://www.drupal.org/node/2544940
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 * @see https://www.drupal.org/node/2874695
122 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
124 function menu_primary_local_tasks() {
125 /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
126 $manager = \Drupal::service('plugin.manager.menu.local_task');
127 $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 0);
128 // Do not display single tabs.
129 return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
133 * Returns the rendered local tasks at the second level.
135 * @see https://www.drupal.org/node/2874695
137 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
139 function menu_secondary_local_tasks() {
140 /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
141 $manager = \Drupal::service('plugin.manager.menu.local_task');
142 $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 1);
143 // Do not display single tabs.
144 return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
148 * Returns a renderable element for the primary and secondary tabs.
150 function menu_local_tabs() {
152 '#theme' => 'menu_local_tasks',
153 '#primary' => menu_primary_local_tasks(),
154 '#secondary' => menu_secondary_local_tasks(),
156 return !empty($build['#primary']) || !empty($build['#secondary']) ? $build : [];
160 * Clears all cached menu data.
162 * This should be called any time broad changes
163 * might have been made to the router items or menu links.
165 * @deprecated in Drupal 8.6.0, will be removed before Drupal 9.0.0. Use
166 * \Drupal::cache('menu')->invalidateAll() instead.
168 * @see https://www.drupal.org/node/2989138
170 function menu_cache_clear_all() {
171 @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);
172 \Drupal::cache('menu')->invalidateAll();
176 * @} End of "addtogroup menu".