Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * API for the Drupal menu system.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * @addtogroup menu
|
Chris@0
|
10 * @{
|
Chris@0
|
11 */
|
Chris@0
|
12
|
Chris@17
|
13 use Drupal\Component\Render\FormattableMarkup;
|
Chris@0
|
14 use Drupal\Core\Render\Element;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Prepares variables for single local task link templates.
|
Chris@0
|
18 *
|
Chris@0
|
19 * Default template: menu-local-task.html.twig.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @param array $variables
|
Chris@0
|
22 * An associative array containing:
|
Chris@0
|
23 * - element: A render element containing:
|
Chris@0
|
24 * - #link: A menu link array with 'title', 'url', and (optionally)
|
Chris@0
|
25 * 'localized_options' keys.
|
Chris@0
|
26 * - #active: A boolean indicating whether the local task is active.
|
Chris@0
|
27 */
|
Chris@0
|
28 function template_preprocess_menu_local_task(&$variables) {
|
Chris@0
|
29 $link = $variables['element']['#link'];
|
Chris@0
|
30 $link += [
|
Chris@0
|
31 'localized_options' => [],
|
Chris@0
|
32 ];
|
Chris@0
|
33 $link_text = $link['title'];
|
Chris@0
|
34
|
Chris@0
|
35 if (!empty($variables['element']['#active'])) {
|
Chris@0
|
36 $variables['is_active'] = TRUE;
|
Chris@0
|
37
|
Chris@0
|
38 // Add text to indicate active tab for non-visual users.
|
Chris@17
|
39 $active = new FormattableMarkup('<span class="visually-hidden">@label</span>', ['@label' => t('(active tab)')]);
|
Chris@0
|
40 $link_text = t('@local-task-title@active', ['@local-task-title' => $link_text, '@active' => $active]);
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 $link['localized_options']['set_active_class'] = TRUE;
|
Chris@0
|
44
|
Chris@0
|
45 $variables['link'] = [
|
Chris@0
|
46 '#type' => 'link',
|
Chris@0
|
47 '#title' => $link_text,
|
Chris@0
|
48 '#url' => $link['url'],
|
Chris@0
|
49 '#options' => $link['localized_options'],
|
Chris@0
|
50 ];
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * Prepares variables for single local action link templates.
|
Chris@0
|
55 *
|
Chris@0
|
56 * Default template: menu-local-action.html.twig.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @param array $variables
|
Chris@0
|
59 * An associative array containing:
|
Chris@0
|
60 * - element: A render element containing:
|
Chris@0
|
61 * - #link: A menu link array with 'title', 'url', and (optionally)
|
Chris@0
|
62 * 'localized_options' keys.
|
Chris@0
|
63 */
|
Chris@0
|
64 function template_preprocess_menu_local_action(&$variables) {
|
Chris@0
|
65 $link = $variables['element']['#link'];
|
Chris@0
|
66 $link += [
|
Chris@0
|
67 'localized_options' => [],
|
Chris@0
|
68 ];
|
Chris@0
|
69 $link['localized_options']['attributes']['class'][] = 'button';
|
Chris@0
|
70 $link['localized_options']['attributes']['class'][] = 'button-action';
|
Chris@0
|
71 $link['localized_options']['set_active_class'] = TRUE;
|
Chris@0
|
72
|
Chris@0
|
73 $variables['link'] = [
|
Chris@0
|
74 '#type' => 'link',
|
Chris@0
|
75 '#title' => $link['title'],
|
Chris@0
|
76 '#options' => $link['localized_options'],
|
Chris@0
|
77 '#url' => $link['url'],
|
Chris@0
|
78 ];
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * Returns an array containing the names of system-defined (default) menus.
|
Chris@0
|
83 */
|
Chris@0
|
84 function menu_list_system_menus() {
|
Chris@0
|
85 return [
|
Chris@0
|
86 'tools' => 'Tools',
|
Chris@0
|
87 'admin' => 'Administration',
|
Chris@0
|
88 'account' => 'User account menu',
|
Chris@0
|
89 'main' => 'Main navigation',
|
Chris@0
|
90 'footer' => 'Footer menu',
|
Chris@0
|
91 ];
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * Collects the local tasks (tabs) for the current route.
|
Chris@0
|
96 *
|
Chris@0
|
97 * @param int $level
|
Chris@0
|
98 * The level of tasks you ask for. Primary tasks are 0, secondary are 1.
|
Chris@0
|
99 *
|
Chris@0
|
100 * @return array
|
Chris@0
|
101 * An array containing
|
Chris@0
|
102 * - tabs: Local tasks for the requested level.
|
Chris@0
|
103 * - route_name: The route name for the current page used to collect the local
|
Chris@0
|
104 * tasks.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @see hook_menu_local_tasks_alter()
|
Chris@0
|
107 * @see https://www.drupal.org/node/2544940
|
Chris@0
|
108 *
|
Chris@0
|
109 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
|
Chris@0
|
110 */
|
Chris@0
|
111 function menu_local_tasks($level = 0) {
|
Chris@0
|
112 /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
|
Chris@0
|
113 $manager = \Drupal::service('plugin.manager.menu.local_task');
|
Chris@0
|
114 return $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), $level);
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * Returns the rendered local tasks at the top level.
|
Chris@0
|
119 *
|
Chris@0
|
120 * @see https://www.drupal.org/node/2874695
|
Chris@0
|
121 *
|
Chris@0
|
122 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
|
Chris@0
|
123 */
|
Chris@0
|
124 function menu_primary_local_tasks() {
|
Chris@0
|
125 /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
|
Chris@0
|
126 $manager = \Drupal::service('plugin.manager.menu.local_task');
|
Chris@0
|
127 $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 0);
|
Chris@0
|
128 // Do not display single tabs.
|
Chris@0
|
129 return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * Returns the rendered local tasks at the second level.
|
Chris@0
|
134 *
|
Chris@0
|
135 * @see https://www.drupal.org/node/2874695
|
Chris@0
|
136 *
|
Chris@0
|
137 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
|
Chris@0
|
138 */
|
Chris@0
|
139 function menu_secondary_local_tasks() {
|
Chris@0
|
140 /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
|
Chris@0
|
141 $manager = \Drupal::service('plugin.manager.menu.local_task');
|
Chris@0
|
142 $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 1);
|
Chris@0
|
143 // Do not display single tabs.
|
Chris@0
|
144 return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 /**
|
Chris@0
|
148 * Returns a renderable element for the primary and secondary tabs.
|
Chris@0
|
149 */
|
Chris@0
|
150 function menu_local_tabs() {
|
Chris@0
|
151 $build = [
|
Chris@0
|
152 '#theme' => 'menu_local_tasks',
|
Chris@0
|
153 '#primary' => menu_primary_local_tasks(),
|
Chris@0
|
154 '#secondary' => menu_secondary_local_tasks(),
|
Chris@0
|
155 ];
|
Chris@0
|
156 return !empty($build['#primary']) || !empty($build['#secondary']) ? $build : [];
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 /**
|
Chris@0
|
160 * Clears all cached menu data.
|
Chris@0
|
161 *
|
Chris@0
|
162 * This should be called any time broad changes
|
Chris@0
|
163 * might have been made to the router items or menu links.
|
Chris@17
|
164 *
|
Chris@17
|
165 * @deprecated in Drupal 8.6.0, will be removed before Drupal 9.0.0. Use
|
Chris@17
|
166 * \Drupal::cache('menu')->invalidateAll() instead.
|
Chris@17
|
167 *
|
Chris@17
|
168 * @see https://www.drupal.org/node/2989138
|
Chris@0
|
169 */
|
Chris@0
|
170 function menu_cache_clear_all() {
|
Chris@17
|
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);
|
Chris@0
|
172 \Drupal::cache('menu')->invalidateAll();
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 /**
|
Chris@0
|
176 * @} End of "addtogroup menu".
|
Chris@0
|
177 */
|