comparison core/includes/menu.inc @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /**
4 * @file
5 * API for the Drupal menu system.
6 */
7
8 /**
9 * @addtogroup menu
10 * @{
11 */
12
13 use Drupal\Component\Utility\SafeMarkup;
14 use Drupal\Core\Render\Element;
15
16 /**
17 * Prepares variables for single local task link templates.
18 *
19 * Default template: menu-local-task.html.twig.
20 *
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.
27 */
28 function template_preprocess_menu_local_task(&$variables) {
29 $link = $variables['element']['#link'];
30 $link += [
31 'localized_options' => [],
32 ];
33 $link_text = $link['title'];
34
35 if (!empty($variables['element']['#active'])) {
36 $variables['is_active'] = TRUE;
37
38 // Add text to indicate active tab for non-visual users.
39 $active = SafeMarkup::format('<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]);
41 }
42
43 $link['localized_options']['set_active_class'] = TRUE;
44
45 $variables['link'] = [
46 '#type' => 'link',
47 '#title' => $link_text,
48 '#url' => $link['url'],
49 '#options' => $link['localized_options'],
50 ];
51 }
52
53 /**
54 * Prepares variables for single local action link templates.
55 *
56 * Default template: menu-local-action.html.twig.
57 *
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.
63 */
64 function template_preprocess_menu_local_action(&$variables) {
65 $link = $variables['element']['#link'];
66 $link += [
67 'localized_options' => [],
68 ];
69 $link['localized_options']['attributes']['class'][] = 'button';
70 $link['localized_options']['attributes']['class'][] = 'button-action';
71 $link['localized_options']['set_active_class'] = TRUE;
72
73 $variables['link'] = [
74 '#type' => 'link',
75 '#title' => $link['title'],
76 '#options' => $link['localized_options'],
77 '#url' => $link['url'],
78 ];
79 }
80
81 /**
82 * Returns an array containing the names of system-defined (default) menus.
83 */
84 function menu_list_system_menus() {
85 return [
86 'tools' => 'Tools',
87 'admin' => 'Administration',
88 'account' => 'User account menu',
89 'main' => 'Main navigation',
90 'footer' => 'Footer menu',
91 ];
92 }
93
94 /**
95 * Collects the local tasks (tabs) for the current route.
96 *
97 * @param int $level
98 * The level of tasks you ask for. Primary tasks are 0, secondary are 1.
99 *
100 * @return array
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
104 * tasks.
105 *
106 * @see hook_menu_local_tasks_alter()
107 * @see https://www.drupal.org/node/2544940
108 *
109 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
110 */
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);
115 }
116
117 /**
118 * Returns the rendered local tasks at the top level.
119 *
120 * @see https://www.drupal.org/node/2874695
121 *
122 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
123 */
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'] : '';
130 }
131
132 /**
133 * Returns the rendered local tasks at the second level.
134 *
135 * @see https://www.drupal.org/node/2874695
136 *
137 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
138 */
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'] : '';
145 }
146
147 /**
148 * Returns a renderable element for the primary and secondary tabs.
149 */
150 function menu_local_tabs() {
151 $build = [
152 '#theme' => 'menu_local_tasks',
153 '#primary' => menu_primary_local_tasks(),
154 '#secondary' => menu_secondary_local_tasks(),
155 ];
156 return !empty($build['#primary']) || !empty($build['#secondary']) ? $build : [];
157 }
158
159 /**
160 * Clears all cached menu data.
161 *
162 * This should be called any time broad changes
163 * might have been made to the router items or menu links.
164 */
165 function menu_cache_clear_all() {
166 \Drupal::cache('menu')->invalidateAll();
167 }
168
169 /**
170 * @} End of "addtogroup menu".
171 */