menu.inc 24.4 KB
Newer Older
Dries's avatar
   
Dries committed
1
<?php
Kjartan's avatar
Kjartan committed
2

3
4
5
6
7
/**
 * @file
 * API for the Drupal menu system.
 */

8
use Drupal\Component\Utility\String;
9
use Drupal\Core\Render\Element;
10
11
use Drupal\Core\Template\Attribute;

Dries's avatar
   
Dries committed
12
/**
13
 * @defgroup menu Menu and routing system
Dries's avatar
   
Dries committed
14
 * @{
Dries's avatar
   
Dries committed
15
 * Define the navigation menus, and route page requests to code based on URLs.
Dries's avatar
   
Dries committed
16
 *
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 * @section sec_overview Overview and terminology
 * The Drupal routing system defines how Drupal responds to URL requests that
 * the web server passes on to Drupal. The routing system is based on the
 * @link http://symfony.com Symfony framework. @endlink The central idea is
 * that Drupal subsystems and modules can register routes (basically, URL
 * paths and context); they can also register to respond dynamically to
 * routes, for more flexibility. When Drupal receives a URL request, it will
 * attempt to match the request to a registered route, and query dynamic
 * responders. If a match is made, Drupal will then instantiate the required
 * classes, gather the data, format it, and send it back to the web browser.
 * Otherwise, Drupal will return a 404 or 403 response.
 *
 * The menu system uses routes; it is used for navigation menus, local tasks,
 * local actions, and contextual links:
 * - Navigation menus are hierarchies of menu links; links point to routes or
 *   URLs.
 * - Menu links and their hierarchies can be defined by Drupal subsystems
 *   and modules, or created in the user interface using the Menu UI module.
 * - Local tasks are groups of related routes. Local tasks are usually rendered
 *   as a group of tabs.
 * - Local actions are used for operations such as adding a new item on a page
 *   that lists items of some type. Local actions are usually rendered as
 *   buttons.
 * - Contextual links are actions that are related to sections of rendered
 *   output, and are usually rendered as a pop-up list of links. The
 *   Contextual Links module handles the gathering and rendering of contextual
 *   links.
 *
 * The following sections of this topic provide an overview of the routing and
 * menu APIs. For more detailed information, see
 * https://www.drupal.org/developing/api/8/routing and
 * https://www.drupal.org/developing/api/8/menu
 *
 * @section sec_register Registering simple routes
 * To register a route, add lines similar to this to a module_name.routing.yml
 * file in your top-level module directory:
 * @code
 * dblog.overview:
 *   path: '/admin/reports/dblog'
 *   defaults:
 *     _content: '\Drupal\dblog\Controller\DbLogController::overview'
 *     _title: 'Recent log messages'
 *   requirements:
 *     _permission: 'access site reports'
 * @endcode
 * Some notes:
 * - The first line is the machine name of the route. Typically, it is prefixed
 *   by the machine name of the module that defines the route, or the name of
 *   a subsystem.
 * - The 'path' line gives the URL path of the route (relative to the site's
 *   base URL).
 * - The 'defaults' section tells how to build the main content of the route,
 *   and can also give other information, such as the page title and additional
 *   arguments for the route controller method. There are several possibilities
 *   for how to build the main content, including:
 *   - _content: A callable, usually a method on a page controller class
 *     (see @ref sec_controller below for details).
 *   - _controller: A callable, usually a method on a page controller class
 *     (see @ref sec_controller below for details).
 *   - _form: A form controller class. See the
 *     @link form_api Form API topic @endlink for more information about
 *     form controllers.
 *   - _entity_form: A form for editing an entity. See the
 *     @link entity_api Entity API topic @endlink for more information.
 * - The 'requirements' section is used in Drupal to give access permission
 *   instructions (it has other uses in the Symfony framework). Most
 *   routes have a simple permission-based access scheme, as shown in this
 *   example. See the @link user_api Permission system topic @endlink for
 *   more information about permissions.
 *
 * See https://www.drupal.org/node/2092643 for more details about *.routing.yml
 * files, and https://www.drupal.org/node/2122201 for information on how to
 * set up dynamic routes.
 *
 * @section sec_placeholders Defining routes with placeholders
 * Some routes have placeholders in them, and these can also be defined in a
 * module_name.routing.yml file, as in this example from the Block module:
94
 * @code
95
 * entity.block.edit_form:
96
 *   path: '/admin/structure/block/manage/{block}'
97
 *   defaults:
98
99
 *     _entity_form: 'block.default'
 *     _title: 'Configure block'
100
 *   requirements:
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
 *     _entity_access: 'block.update'
 * @endcode
 * In the path, '{block}' is a placeholder - it will be replaced by the
 * ID of the block that is being configured by the entity system. See the
 * @link entity_api Entity API topic @endlink for more information.
 *
 * @section sec_controller Route controllers for simple routes
 * For simple routes, after you have defined the route in a *.routing.yml file
 * (see @ref sec_register above), the next step is to define a page controller
 * class and method. Page controller classes do not necessarily need to
 * implement any particular interface or extend any particular base class. The
 * only requirement is that the method specified in your *.routing.yml file
 * return one of the following, depending on whether you specified _content or
 * _controller in the routing file defaults section:
 * - A render array (see the
 *   @link theme_render Theme and render topic @endlink for more information),
 *   if _content is used in the routing file.
 * - A \Drupal\Core\Page\HtmlFragmentInterface object (fragment or page), if
 *   _content is used in the routing file.
 * - A \Symfony\Component\HttpFoundation\Response object, if _controller is
 *   used in the routing file.
 * As a note, if your module registers multiple simple routes, it is usual
 * (and usually easiest) to put all of their methods on one controller class.
 *
 * Most controllers will need to display some information stored in the Drupal
 * database, which will involve using one or more Drupal services (see the
 * @link container Services and container topic @endlink). In order to properly
 * inject services, a controller should implement
 * \Drupal\Core\DependencyInjection\ContainerInjectionInterface; simple
 * controllers can do this by extending the
 * \Drupal\Core\Controller\ControllerBase class. See
 * \Drupal\dblog\Controller\DbLogController for a straightforward example of
 * a controller class.
 *
 * @section sec_links Defining menu links for the administrative menu
 * Routes for administrative tasks can be added to the main Drupal
 * administrative menu hierarchy. To do this, add lines like the following to a
138
 * module_name.links.menu.yml file (in the top-level directory for your module):
139
140
141
142
143
144
145
146
147
148
149
150
 * @code
 * dblog.overview:
 *   title: 'Recent log messages'
 *   parent: system.admin_reports
 *   description: 'View events that have recently been logged.'
 *   route_name: dblog.overview
 *   weight: -1
 * @endcode
 * Some notes:
 * - The first line is the machine name for your menu link, which usually
 *   matches the machine name of the route (given in the 'route_name' line).
 * - parent: The machine name of the menu link that is the parent in the
151
 *   administrative hierarchy. See system.links.menu.yml to find the main
152
153
154
155
 *   skeleton of the hierarchy.
 * - weight: Lower (negative) numbers come before higher (positive) numbers,
 *   for menu items with the same parent.
 *
156
157
 * Discovered menu links from other modules can be altered using
 * hook_menu_links_discovered_alter().
158
159
160
161
162
163
164
165
 *
 * @todo Derivatives will probably be defined for these; when they are, add
 *   documentation here.
 *
 * @section sec_tasks Defining groups of local tasks (tabs)
 * Local tasks appear as tabs on a page when there are at least two defined for
 * a route, including the base route as the main tab, and additional routes as
 * other tabs. Static local tasks can be defined by adding lines like the
166
 * following to a module_name.links.task.yml file (in the top-level directory
167
168
169
170
171
172
173
174
175
176
177
 * for your module):
 * @code
 * book.admin:
 *   route_name: book.admin
 *   title: 'List'
 *   base_route: book.admin
 * book.settings:
 *   route_name: book.settings
 *   title: 'Settings'
 *   base_route: book.admin
 *   weight: 100
178
 * @endcode
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
 * Some notes:
 * - The first line is the machine name for your local task, which usually
 *   matches the machine name of the route (given in the 'route_name' line).
 * - base_route: The machine name of the main task (tab) for the set of local
 *   tasks.
 * - weight: Lower (negative) numbers come before higher (positive) numbers,
 *   for tasks on the same base route. If there is a tab whose route
 *   matches the base route, that will be the default/first tab shown.
 *
 * Local tasks from other modules can be altered using
 * hook_menu_local_tasks_alter().
 *
 * @todo Derivatives are in flux for these; when they are more stable, add
 *   documentation here.
 *
 * @section sec_actions Defining local actions for routes
 * Local actions can be defined for operations related to a given route. For
 * instance, adding content is a common operation for the content management
 * page, so it should be a local action. Static local actions can be
 * defined by adding lines like the following to a
199
 * module_name.links.action.yml file (in the top-level directory for your
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
 * module):
 * @code
 * node.add_page:
 *   route_name: node.add_page
 *   title: 'Add content'
 *   appears_on:
 *     - system.admin_content
 * @endcode
 * Some notes:
 * - The first line is the machine name for your local action, which usually
 *   matches the machine name of the route (given in the 'route_name' line).
 * - appears_on: Machine names of one or more routes that this local task
 *   should appear on.
 *
 * Local actions from other modules can be altered using
 * hook_menu_local_actions_alter().
 *
 * @todo Derivatives are in flux for these; when they are more stable, add
 *   documentation here.
 *
 * @section sec_contextual Defining contextual links
 * Contextual links are displayed by the Contextual Links module for user
 * interface elements whose render arrays have a '#contextual_links' element
 * defined. For example, a block render array might look like this, in part:
 * @code
 * array(
 *   '#contextual_links' => array(
 *     'block' => array(
 *       'route_parameters' => array('block' => $entity->id()),
 *     ),
 *   ),
 * @endcode
 * In this array, the outer key 'block' defines a "group" for contextual
 * links, and the inner array provides values for the route's placeholder
 * parameters (see @ref sec_placeholders above).
 *
 * To declare that a defined route should be a contextual link for a
 * contextual links group, put lines like the following in a
238
 * module_name.links.contextual.yml file (in the top-level directory for your
239
240
241
242
 * module):
 * @code
 * block_configure:
 *   title: 'Configure block'
243
 *   route_name: 'entity.block.edit_form'
244
245
246
247
248
249
250
251
252
253
254
255
 *   group: 'block'
 * @endcode
 * Some notes:
 * - The first line is the machine name for your contextual link, which usually
 *   matches the machine name of the route (given in the 'route_name' line).
 * - group: This needs to match the link group defined in the render array.
 *
 * Contextual links from other modules can be altered using
 * hook_contextual_links_alter().
 *
 * @todo Derivatives are in flux for these; when they are more stable, add
 *   documentation here.
Dries's avatar
   
Dries committed
256
257
 */

258
/**
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
 * @section Rendering menus
 * Once you have created menus (that contain menu links), you want to render
 * them. Drupal provides a block (Drupal\system\Plugin\Block\SystemMenuBlock) to
 * do so.
 *
 * However, perhaps you have more advanced needs and you're not satisfied with
 * what the menu blocks offer you. If that's the case, you'll want to:
 * - Instantiate \Drupal\Core\Menu\MenuTreeParameters, and set its values to
 *   match your needs. Alternatively, you can use
 *   MenuLinkTree::getCurrentRouteMenuTreeParameters() to get a typical
 *   default set of parameters, and then customize them to suit your needs.
 * - Call \Drupal\Core\MenuLinkTree::load() with your menu link tree parameters,
 *   this will return a menu link tree.
 * - Pass the menu tree to \Drupal\Core\Menu\MenuLinkTree::transform() to apply
 *   menu link tree manipulators that transform the tree. You will almost always
 *   want to apply access checking. The manipulators that you will typically
 *   need can be found in \Drupal\Core\Menu\DefaultMenuTreeManipulators.
 * - Potentially write a custom menu tree manipulator, see
 *   \Drupal\Core\Menu\DefaultMenuTreeManipulators for examples. This is only
 *   necessary if you want to do things like adding extra metadata to rendered
 *   links to display icons next to them.
 * - Pass the menu tree to \Drupal\Core\Menu\MenuLinkTree::build(), this will
 *   build a renderable array.
 *
 * Combined, that would look like this:
284
 * @code
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
 * $menu_tree = \Drupal::menuTree();
 * $menu_name = 'my_menu';
 *
 * // Build the typical default set of menu tree parameters.
 * $parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
 *
 * // Load the tree based on this set of parameters.
 * $tree = $menu_tree->load($menu_name, $parameters);
 *
 * // Transform the tree using the manipulators you want.
 * $manipulators = array(
 *   // Only show links that are accessible for the current user.
 *   array('callable' => 'menu.default_tree_manipulators:checkAccess'),
 *   // Use the default sorting of menu links.
 *   array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
 * );
 * $tree = $menu_tree->transform($tree, $manipulators);
 *
 * // Finally, build a renderable array from the transformed tree.
 * $menu = $menu_tree->build($tree);
305
 *
306
307
 * $menu_html = drupal_render($menu);
 * @endcode
308
309
 */

Dries's avatar
   
Dries committed
310
/**
311
 * Implements template_preprocess_HOOK() for theme_menu_tree().
Dries's avatar
   
Dries committed
312
 */
313
function template_preprocess_menu_tree(&$variables) {
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
  if (isset($variables['tree']['#heading'])) {
    $variables['heading'] = $variables['tree']['#heading'];
    $heading = &$variables['heading'];
    // Convert a string heading into an array, using a H2 tag by default.
    if (is_string($heading)) {
      $heading = array('text' => $heading);
    }
    // Merge in default array properties into $heading.
    $heading += array(
      'level' => 'h2',
      'attributes' => array(),
    );
    // Convert the attributes array into an Attribute object.
    $heading['attributes'] = new Attribute($heading['attributes']);
    $heading['text'] = String::checkPlain($heading['text']);
  }
330

331
332
333
334
335
336
337
338
339
340
341
342
  if (isset($variables['tree']['#attributes'])) {
    $variables['attributes'] = new Attribute($variables['tree']['#attributes']);
  }
  else {
    $variables['attributes'] = new Attribute();
  }
  if (!isset($variables['attributes']['class'])) {
    $variables['attributes']['class'] = array();
  }
  $variables['attributes']['class'][] = 'menu';

  $variables['tree'] = $variables['tree']['#children'];
343
344
}

345
/**
346
 * Returns HTML for a menu link and submenu.
347
 *
348
349
350
351
 * @param $variables
 *   An associative array containing:
 *   - element: Structured array data for a menu link.
 *
352
 * @ingroup themeable
353
 */
354
355
function theme_menu_link(array $variables) {
  $element = $variables['element'];
356
357
358
359
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
360
  }
361
362
363
364
  /** @var \Drupal\Core\Url $url */
  $url = $element['#url'];
  $url->setOption('set_active_class', TRUE);
  $output = \Drupal::linkGenerator()->generateFromUrl($element['#title'], $url);
365
  return '<li' . new Attribute($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
366
367
}

368
/**
369
 * Prepares variables for single local task link templates.
370
 *
371
372
373
 * Default template: menu-local-task.html.twig.
 *
 * @param array $variables
374
 *   An associative array containing:
375
376
377
378
 *   - element: A render element containing:
 *     - #link: A menu link array with 'title', 'href', and 'localized_options'
 *       keys.
 *     - #active: A boolean indicating whether the local task is active.
379
 */
380
function template_preprocess_menu_local_task(&$variables) {
381
  $link = $variables['element']['#link'];
382
383
384
  $link += array(
    'localized_options' => array(),
  );
385
386
387
388
  $link_text = $link['title'];

  if (!empty($variables['element']['#active'])) {
    // Add text to indicate active tab for non-visual users.
389
    $active = '<span class="visually-hidden">' . t('(active tab)') . '</span>';
390
    $variables['attributes']['class'] = array('active');
391

392
    // If the link does not contain HTML already, String::checkPlain() it now.
393
394
    // After we set 'html'=TRUE the link will not be sanitized by l().
    if (empty($link['localized_options']['html'])) {
395
      $link['title'] = String::checkPlain($link['title']);
396
397
    }
    $link['localized_options']['html'] = TRUE;
398
    $link_text = t('!local-task-title!active', array('!local-task-title' => $link['title'], '!active' => $active));
399
  }
400
401
  $link['localized_options']['set_active_class'] = TRUE;

402
403
404
405
406
407
  $variables['link'] = array(
    '#type' => 'link',
    '#title' => $link_text,
    '#options' => $link['localized_options'],
  );

408
  if (!empty($link['href'])) {
409
410
    // @todo - Remove this once all pages are converted to routes.
    $variables['link']['#href'] = $link['href'];
411
412
  }
  else {
413
414
415
416
    $variables['link'] += array(
      '#route_name' => $link['route_name'],
      '#route_parameters' => $link['route_parameters'],
    );
417
  }
418
}
Dries's avatar
   
Dries committed
419

420
/**
421
 * Prepares variables for single local action link templates.
422
 *
423
424
425
 * Default template: menu-local-action.html.twig.
 *
 * @param array $variables
426
 *   An associative array containing:
427
428
429
 *   - element: A render element containing:
 *     - #link: A menu link array with 'title', 'href', and 'localized_options'
 *       keys.
430
 */
431
function template_preprocess_menu_local_action(&$variables) {
432
  $link = $variables['element']['#link'];
433
434
435
  $link += array(
    'href' => '',
    'localized_options' => array(),
436
    'route_parameters' => array(),
437
438
  );
  $link['localized_options']['attributes']['class'][] = 'button';
439
  $link['localized_options']['attributes']['class'][] = 'button-action';
440
  $link['localized_options']['set_active_class'] = TRUE;
441

442
443
444
445
446
447
  $variables['link'] = array(
    '#type' => 'link',
    '#title' => $link['title'],
    '#options' => $link['localized_options'],
  );

448
449
  // @todo Figure out how to support local actions without a href properly.
  if ($link['href'] === '' && !empty($link['route_name'])) {
450
451
452
453
    $variables['link'] += array(
      '#route_name' => $link['route_name'],
      '#route_parameters' => $link['route_parameters'],
    );
454
455
  }
  else {
456
457
    // @todo - Remove this once all pages are converted to routes.
    $variables['link']['#href'] = $link['href'];
458
  }
459
460
}

461
/**
462
 * Returns an array containing the names of system-defined (default) menus.
463
464
 */
function menu_list_system_menus() {
465
  return array(
466
467
468
469
    'tools' => 'Tools',
    'admin' => 'Administration',
    'account' => 'User account menu',
    'main' => 'Main navigation',
470
    'footer' => 'Footer menu',
471
  );
472
473
}

474
/**
475
 * Returns an array of links to be rendered as the Main menu.
476
 */
477
function menu_main_menu() {
478
  $main_links_source = _menu_get_links_source('main_links', 'main');
479
  return menu_navigation_links($main_links_source);
Dries's avatar
   
Dries committed
480
}
481

482
/**
483
 * Returns an array of links to be rendered as the Secondary links.
484
 */
485
function menu_secondary_menu() {
486
487
  $main_links_source = _menu_get_links_source('main_links', 'main');
  $secondary_links_source = _menu_get_links_source('secondary_links', 'account');
488

489
  // If the secondary menu source is set as the primary menu, we display the
490
  // second level of the primary menu.
491
492
  if ($secondary_links_source == $main_links_source) {
    return menu_navigation_links($main_links_source, 1);
493
494
  }
  else {
495
    return menu_navigation_links($secondary_links_source, 0);
496
497
498
  }
}

499
500
501
502
503
504
505
506
507
508
509
510
/**
 * Returns the source of links of a menu.
 *
 * @param string $name
 *   A string configuration key of menu link source.
 * @param string $default
 *   Default menu name.
 *
 * @return string
 *   Returns menu name, if exist
 */
function _menu_get_links_source($name, $default) {
511
512
  $config = \Drupal::config('menu_ui.settings');
  return \Drupal::moduleHandler()->moduleExists('menu_ui') ? $config->get($name) : $default;
513
514
}

515
/**
516
 * Builds a renderable array for a navigation menu.
517
 *
518
 * @param string $menu_name
519
 *   The name of the menu.
520
 * @param int $level
521
 *   Optional, the depth of the menu to be returned.
522
 *
523
524
 * @return array
 *   A renderable array.
525
526
 */
function menu_navigation_links($menu_name, $level = 0) {
527
528
529
530
531
532
533
534
535
536
537
  $menu_tree = \Drupal::menuTree();
  $parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
  $parameters->setMaxDepth($level + 1);
  $tree = $menu_tree->load($menu_name, $parameters);
  $manipulators = array(
    array('callable' => 'menu.default_tree_manipulators:checkAccess'),
    array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
    array('callable' => 'menu.default_tree_manipulators:extractSubtreeOfActiveTrail', 'args' => array($level)),
  );
  $tree = $menu_tree->transform($tree, $manipulators);
  return $menu_tree->build($tree);
Dries's avatar
   
Dries committed
538
539
}

540
/**
541
 * Collects the local tasks (tabs), action links, and the root path.
542
 *
543
 * @param int $level
544
 *   The level of tasks you ask for. Primary tasks are 0, secondary are 1.
545
 *
546
 * @return array
547
 *   An array containing
548
549
 *   - tabs: Local tasks for the requested level.
 *   - actions: Action links for the requested level.
550
551
 *   - root_path: The router path for the current page. If the current page is
 *     a default local task, then this corresponds to the parent tab.
552
553
554
 *
 * @see hook_menu_local_tasks()
 * @see hook_menu_local_tasks_alter()
555
556
557
558
559
 */
function menu_local_tasks($level = 0) {
  $data = &drupal_static(__FUNCTION__);
  $root_path = &drupal_static(__FUNCTION__ . ':root_path', '');
  $empty = array(
560
561
    'tabs' => array(),
    'actions' => array(),
562
563
564
565
    'root_path' => &$root_path,
  );

  if (!isset($data)) {
566
567
568
569
    // Look for route-based tabs.
    $data['tabs'] = array();
    $data['actions'] = array();

570
    $route_name = \Drupal::routeMatch()->getRouteName();
571
    if (!\Drupal::request()->attributes->has('exception') && !empty($route_name)) {
572
      $manager = \Drupal::service('plugin.manager.menu.local_task');
573
574
575
576
577
578
      $local_tasks = $manager->getTasksBuild($route_name);
      foreach ($local_tasks as $level => $items) {
        $data['tabs'][$level] = empty($data['tabs'][$level]) ? $items : array_merge($data['tabs'][$level], $items);
      }
    }

579
    // Allow modules to dynamically add further tasks.
580
    $module_handler = \Drupal::moduleHandler();
581
582
    foreach ($module_handler->getImplementations('menu_local_tasks') as $module) {
      $function = $module . '_menu_local_tasks';
583
      $function($data, $route_name);
584
585
    }
    // Allow modules to alter local tasks.
586
    $module_handler->alter('menu_local_tasks', $data, $route_name);
587
  }
588

589
590
591
592
593
594
  if (isset($data['tabs'][$level])) {
    return array(
      'tabs' => $data['tabs'][$level],
      'actions' => $data['actions'],
      'root_path' => $root_path,
    );
595
  }
596
  elseif (!empty($data['actions'])) {
597
598
    return array('actions' => $data['actions']) + $empty;
  }
599
  return $empty;
600
601
}

602
603
604
/**
 * Returns the rendered local tasks at the top level.
 */
605
function menu_primary_local_tasks() {
606
607
  $links = menu_local_tasks(0);
  // Do not display single tabs.
608
  return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
609
}
610

611
612
613
/**
 * Returns the rendered local tasks at the second level.
 */
614
function menu_secondary_local_tasks() {
615
616
  $links = menu_local_tasks(1);
  // Do not display single tabs.
617
  return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
618
619
620
621
622
}

/**
 * Returns the rendered local actions at the current level.
 */
623
function menu_get_local_actions() {
624
  $links = menu_local_tasks();
625
  $route_name = Drupal::routeMatch()->getRouteName();
626
  $manager = \Drupal::service('plugin.manager.menu.local_action');
627
  return $manager->getActionsForRoute($route_name) + $links['actions'];
Dries's avatar
   
Dries committed
628
629
}

630
/**
631
 * Returns the router path, or the path for a default local task's parent.
632
633
 */
function menu_tab_root_path() {
634
635
  $links = menu_local_tasks();
  return $links['root_path'];
636
637
638
}

/**
639
640
641
 * Returns a renderable element for the primary and secondary tabs.
 */
function menu_local_tabs() {
642
  $build = array(
643
644
645
646
    '#theme' => 'menu_local_tasks',
    '#primary' => menu_primary_local_tasks(),
    '#secondary' => menu_secondary_local_tasks(),
  );
647
  return !empty($build['#primary']) || !empty($build['#secondary']) ? $build : array();
648
649
}

650
/**
651
652
653
 * Clears all cached menu data.
 *
 * This should be called any time broad changes
654
 * might have been made to the router items or menu links.
655
656
 */
function menu_cache_clear_all() {
657
  \Drupal::cache('menu')->invalidateAll();
658
659
}

660
661
662
/**
 * @} End of "defgroup menu".
 */