You are here

toolbar_menu.module in Toolbar Menu 8

Same filename and directory in other branches
  1. 8.2 toolbar_menu.module

Add menu entries in the Drupal Toolbar.

File

toolbar_menu.module
View source
<?php

/**
 * @file
 * Add menu entries in the Drupal Toolbar.
 */
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function toolbar_menu_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.toolbar_menu':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Add your favorite menu in the Drupal Toolbar');
      $output .= '<h3>' . t('Usage') . '</h3>';
      $output .= '<ul>';
      $output .= '<li>' . t('Manage your menus (Structure > Menu)') . '</li>';
      $output .= '<li>' . t('Go to Config > Toolbar Menu > Toolbar Menu settings') . '</li>';
      $output .= '<li>' . t('Check all menus you want to appear in your toolbar') . '</li>';
      $output .= '<li>' . t('Re-order the menu entries if you want') . '</li>';
      $output .= '<li>' . t('Save the configuration') . '</li>';
      $output .= '<li>' . t('Your selected menus must appears in the toolbar') . '</li>';
      $output .= '</ul>';
      return $output;
  }
}

/**
 * Implements hook_page_top().
 *
 * Add a new cache tag for the toolbar cache.
 */
function toolbar_menu_page_top(array &$page_top) {
  $page_top['toolbar']['#cache']['tags'][] = 'toolbar_menu';
}

/**
 * Implements hook_toolbar().
 */
function toolbar_menu_toolbar() {
  $items = [];

  /** @var \Drupal\toolbar_menu\ToolbarMenuManager $toolbar_manager */
  $toolbar_manager = \Drupal::service('toolbar_menu.manager');
  foreach ($toolbar_manager
    ->getToolbarMenuElements() as $element_id => $element) {
    $items['toolbar_menu_' . $element_id] = [
      '#type' => 'toolbar_item',
      'tab' => [
        '#type' => 'link',
        '#title' => $element
          ->getDisplayLabel(),
        '#url' => Url::fromRoute('<front>'),
        '#attributes' => [
          'title' => $element
            ->getDisplayLabel(),
          'class' => [
            'toolbar-icon',
            'toolbar-icon-toolbar-menu',
            'toolbar-icon-toolbar-menu-' . $element_id,
          ],
          'data-drupal-subtrees' => '',
        ],
      ],
      'tray' => [
        '#heading' => t('@menu_label actions', [
          '@menu_label' => $element
            ->getDisplayLabel(),
        ]),
        'toolbar_menu_' . $element_id . '_links' => [
          '#type' => 'container',
          '#id' => $element
            ->loadMenu()
            ->id(),
          '#pre_render' => [
            'toolbar_menu_prerender_toolbar_tray',
          ],
          '#attributes' => [
            'class' => [
              'toolbar-menu-administration',
            ],
          ],
        ],
      ],
      '#weight' => $element
        ->weight(),
      '#attached' => [
        'library' => [
          'toolbar_menu/icons',
        ],
      ],
      '#cache' => [
        'tags' => array_merge($element
          ->getCacheTags(), $element
          ->loadMenu()
          ->getCacheTags(), [
          'toolbar_menu',
        ]),
      ],
    ];
  }
  return $items;
}

/**
 * Pre-render the toolbar_menu tray element.
 *
 * @param array $element
 *   The tray element to pre-render.
 *
 * @return array
 *   The pre-rendered tray element.
 */
function toolbar_menu_prerender_toolbar_tray(array $element) {

  /** @var \Drupal\toolbar\Menu\ToolbarMenuLinkTree $menu_tree */
  $menu_tree = \Drupal::service('toolbar.menu_tree');
  $parameters = new MenuTreeParameters();
  $parameters
    ->excludeRoot()
    ->onlyEnabledLinks();
  $tree = $menu_tree
    ->load($element['#id'], $parameters);
  $manipulators = [
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
    [
      'callable' => 'toolbar_menu.menu_link_tree_manipulators:addIcons',
    ],
  ];
  $tree = $menu_tree
    ->transform($tree, $manipulators);
  $element['toolbar_menu_' . $element['#id']] = $menu_tree
    ->build($tree);
  return $element;
}

Functions

Namesort descending Description
toolbar_menu_help Implements hook_help().
toolbar_menu_page_top Implements hook_page_top().
toolbar_menu_prerender_toolbar_tray Pre-render the toolbar_menu tray element.
toolbar_menu_toolbar Implements hook_toolbar().