admin_toolbar.module 3.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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
<?php

/**
 *  Implements hook_toolbar().
 */
function admin_toolbar_toolbar() {
  $items = array();
  $items['admin_toolbar'] = array(
    '#type' => 'toolbar_item',
    '#attached' => array(
      'library' => array(
        'admin_toolbar/toolbar.tree',
      ),
    ),
  );
  return $items;
}
/**
 *  Implements hook_toolbar_alter().
 */
function admin_toolbar_toolbar_alter(&$items) {
  // To conserve bandwidth, we only include the top-level links in the HTML.
  // The subtrees are fetched through a JSONP script that is generated at the
  // toolbar_subtrees route. We provide the JavaScript requesting that JSONP
  // script here with the hash parameter that is needed for that route.
  // @see toolbar_subtrees_jsonp()
  $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
  $subtrees_attached['drupalSettings']['toolbar'] = [
    'subtreesHash' => admin_toolbar_get_subtrees_hash($langcode),
    'langcode' => $langcode,
  ];
  // a toolbar tray. The tray contains the full administrative menu of the site.
  $items['administration'] = array(
    '#type' => 'toolbar_item',
    'tab' => array(
      '#type' => 'link',
      '#title' => t('Manage'),
      '#url' => \Drupal\Core\Url::fromRoute('system.admin'),
      '#attributes' => array(
        'title' => t('Admin menu'),
        'class' => array('toolbar-icon', 'toolbar-icon-menu'),
        // A data attribute that indicates to the client to defer loading of
        // the admin menu subtrees until this tab is activated. Admin menu
        // subtrees will not render to the DOM if this attribute is removed.
        // The value of the attribute is intentionally left blank. Only the
        // presence of the attribute is necessary.
        'data-drupal-subtrees' => '',
      ),
    ),
    'tray' => array(
      '#heading' => t('Administration menu'),
      '#attached' => $subtrees_attached,
      'toolbar_administration' => array(
        '#pre_render' => array(
          'toolbar_prerender_admin_toolbar_administration_tray',
        ),
        '#type' => 'container',
        '#attributes' => array(
          'class' => array('toolbar-menu-administration'),
        ),
      ),
    ),
    '#weight' => -15,
  );
  return $items;

}
/**
 * Renders the toolbar's administration tray.
 *
 * @param array $element
 *   A renderable array.
 *
 * @return array
 *   The updated renderable array.
 *
 * @see drupal_render()
 */
function toolbar_prerender_admin_toolbar_administration_tray(array $element) {
80
  $menu_tree = \Drupal::service('toolbar.menu_tree');
81 82
  // Render the top-level administration menu links.
  $parameters = new \Drupal\Core\Menu\MenuTreeParameters();
matio89's avatar
matio89 committed
83
  $parameters->setRoot('system.admin')->excludeRoot()->setMaxDepth(4);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  $tree = $menu_tree->load(NULL, $parameters);
  $manipulators = array(
    array('callable' => 'menu.default_tree_manipulators:checkAccess'),
    array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
    array('callable' => 'toolbar_menu_navigation_links'),
  );
  $tree = $menu_tree->transform($tree, $manipulators);
  $element['administration_menu'] = $menu_tree->build($tree);
  return $element;
}
/**
 * Returns the hash of the per-user rendered toolbar subtrees.
 *
 * @param string $langcode
 *   The langcode of the current request.
 *
 * @return string
 *   The hash of the admin_menu subtrees.
 */
function admin_toolbar_get_subtrees_hash($langcode) {
  $uid = \Drupal::currentUser()->id();
  $cid = _toolbar_get_user_cid($uid, $langcode);
  $subtrees = toolbar_get_rendered_subtrees();
  $hash = \Drupal\Component\Utility\Crypt::hashBase64(serialize($subtrees));
  $role_list_cache_tags = \Drupal::entityManager()->getDefinition('user_role')->getListCacheTags();
  \Drupal::cache('toolbar')->set($cid, $hash, \Drupal\Core\Cache\Cache::PERMANENT, \Drupal\Core\Cache\Cache::mergeTags(array('user:' . $uid, 'locale', 'config:system.menu.admin'), $role_list_cache_tags));
  return $hash;
}