Skip to content
menu.inc 4.8 KiB
Newer Older
Dries Buytaert's avatar
Dries Buytaert committed
<?php
/**
 * @file
 * API for the Drupal menu system.
 */

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * @{
 */

use Drupal\Component\Utility\SafeMarkup;
 * Prepares variables for single local task link templates.
 * Default template: menu-local-task.html.twig.
 *
 * @param array $variables
 *   - element: A render element containing:
 *     - #link: A menu link array with 'title', 'url', and (optionally)
 *       'localized_options' keys.
 *     - #active: A boolean indicating whether the local task is active.
function template_preprocess_menu_local_task(&$variables) {
  $link = $variables['element']['#link'];
  $link += array(
    'localized_options' => array(),
  );

  if (!empty($variables['element']['#active'])) {
    // Add text to indicate active tab for non-visual users.
    $active = SafeMarkup::format('<span class="visually-hidden">@label</span>', array('@label' => t('(active tab)')));
    $link_text = t('@local-task-title@active', array('@local-task-title' => $link_text, '@active' => $active));
  $link['localized_options']['set_active_class'] = TRUE;

  $variables['link'] = array(
    '#type' => 'link',
    '#options' => $link['localized_options'],
  );
Dries Buytaert's avatar
Dries Buytaert committed

 * Prepares variables for single local action link templates.
 * Default template: menu-local-action.html.twig.
 *
 * @param array $variables
 *   - element: A render element containing:
 *     - #link: A menu link array with 'title', 'url', and (optionally)
 *       'localized_options' keys.
function template_preprocess_menu_local_action(&$variables) {
  $link = $variables['element']['#link'];
  $link += array(
    'localized_options' => array(),
  );
  $link['localized_options']['attributes']['class'][] = 'button';
  $link['localized_options']['attributes']['class'][] = 'button-action';
  $link['localized_options']['set_active_class'] = TRUE;
  $variables['link'] = array(
    '#type' => 'link',
    '#title' => $link['title'],
    '#options' => $link['localized_options'],
 * Returns an array containing the names of system-defined (default) menus.
 */
function menu_list_system_menus() {
    'tools' => 'Tools',
    'admin' => 'Administration',
    'account' => 'User account menu',
    'main' => 'Main navigation',
 * Collects the local tasks (tabs) for the current route.
 *   The level of tasks you ask for. Primary tasks are 0, secondary are 1.
 *   - tabs: Local tasks for the requested level.
 *   - route_name: The route name for the current page used to collect the local
 *     tasks.
 *
 * @see hook_menu_local_tasks_alter()
 *
 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
 */
function menu_local_tasks($level = 0) {
  /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
  $manager = \Drupal::service('plugin.manager.menu.local_task');
  return $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), $level);
/**
 * Returns the rendered local tasks at the top level.
 *
 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
function menu_primary_local_tasks() {
  /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
  $manager = \Drupal::service('plugin.manager.menu.local_task');
  $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 0);
  return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
/**
 * Returns the rendered local tasks at the second level.
 *
 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
  /** @var \Drupal\Core\Menu\LocalTaskManagerInterface $manager */
  $manager = \Drupal::service('plugin.manager.menu.local_task');
  $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 1);
  return count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : '';
 * Returns a renderable element for the primary and secondary tabs.
 */
function menu_local_tabs() {
    '#theme' => 'menu_local_tasks',
    '#primary' => menu_primary_local_tasks(),
    '#secondary' => menu_secondary_local_tasks(),
  );
  return !empty($build['#primary']) || !empty($build['#secondary']) ? $build : array();
 * Clears all cached menu data.
 *
 * This should be called any time broad changes
 * might have been made to the router items or menu links.
 */
function menu_cache_clear_all() {
  \Drupal::cache('menu')->invalidateAll();