Chris@0: ' . t('About') . ''; Chris@0: $output .= '

' . t('The Toolbar module provides a toolbar for site administrators, which displays tabs and trays provided by the Toolbar module itself and other modules. For more information, see the online documentation for the Toolbar module.', [':toolbar_docs' => 'https://www.drupal.org/documentation/modules/toolbar']) . '

'; Chris@0: $output .= '

' . t('Terminology') . '

'; Chris@0: $output .= '
'; Chris@0: $output .= '
' . t('Tabs') . '
'; Chris@0: $output .= '
' . t('Tabs are buttons, displayed in a bar across the top of the screen. Some tabs execute an action (such as starting Edit mode), while other tabs toggle which tray is open.') . '
'; Chris@0: $output .= '
' . t('Trays') . '
'; Chris@0: $output .= '
' . t('Trays are usually lists of links, which can be hierarchical like a menu. If a tray has been toggled open, it is displayed either vertically or horizontally below the tab bar, depending on the browser width. Only one tray may be open at a time. If you click another tab, that tray will replace the tray being displayed. In wide browser widths, the user has the ability to toggle from vertical to horizontal, using a link at the bottom or right of the tray. Hierarchical menus only have open/close behavior in vertical mode; if you display a tray containing a hierarchical menu horizontally, only the top-level links will be available.') . '
'; Chris@0: $output .= '
'; Chris@0: return $output; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_theme(). Chris@0: */ Chris@0: function toolbar_theme($existing, $type, $theme, $path) { Chris@0: $items['toolbar'] = [ Chris@0: 'render element' => 'element', Chris@0: ]; Chris@0: $items['menu__toolbar'] = [ Chris@0: 'base hook' => 'menu', Chris@0: 'variables' => ['items' => [], 'attributes' => []], Chris@0: ]; Chris@0: Chris@0: return $items; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_page_top(). Chris@0: * Chris@0: * Add admin toolbar to the top of the page automatically. Chris@0: */ Chris@0: function toolbar_page_top(array &$page_top) { Chris@0: $page_top['toolbar'] = [ Chris@0: '#type' => 'toolbar', Chris@0: '#access' => \Drupal::currentUser()->hasPermission('access toolbar'), Chris@0: '#cache' => [ Chris@0: 'keys' => ['toolbar'], Chris@0: 'contexts' => ['user.permissions'], Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for administration toolbar templates. Chris@0: * Chris@0: * Default template: toolbar.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - element: An associative array containing the properties and children of Chris@0: * the tray. Properties used: #children, #attributes and #bar. Chris@0: */ Chris@0: function template_preprocess_toolbar(&$variables) { Chris@0: $element = $variables['element']; Chris@0: Chris@0: // Prepare the toolbar attributes. Chris@0: $variables['attributes'] = $element['#attributes']; Chris@0: $variables['toolbar_attributes'] = new Attribute($element['#bar']['#attributes']); Chris@0: $variables['toolbar_heading'] = $element['#bar']['#heading']; Chris@0: Chris@0: // Prepare the trays and tabs for each toolbar item as well as the remainder Chris@0: // variable that will hold any non-tray, non-tab elements. Chris@0: $variables['trays'] = []; Chris@0: $variables['tabs'] = []; Chris@0: $variables['remainder'] = []; Chris@0: foreach (Element::children($element) as $key) { Chris@0: // Early rendering to collect the wrapper attributes from Chris@0: // ToolbarItem elements. Chris@0: if (!empty($element[$key])) { Chris@0: Drupal::service('renderer')->render($element[$key]); Chris@0: } Chris@0: // Add the tray. Chris@0: if (isset($element[$key]['tray'])) { Chris@0: $attributes = []; Chris@0: if (!empty($element[$key]['tray']['#wrapper_attributes'])) { Chris@0: $attributes = $element[$key]['tray']['#wrapper_attributes']; Chris@0: } Chris@0: $variables['trays'][$key] = [ Chris@0: 'links' => $element[$key]['tray'], Chris@0: 'attributes' => new Attribute($attributes), Chris@0: ]; Chris@0: if (array_key_exists('#heading', $element[$key]['tray'])) { Chris@0: $variables['trays'][$key]['label'] = $element[$key]['tray']['#heading']; Chris@0: } Chris@0: } Chris@0: Chris@0: // Add the tab. Chris@0: if (isset($element[$key]['tab'])) { Chris@0: $attributes = []; Chris@0: // Pass the wrapper attributes along. Chris@0: if (!empty($element[$key]['#wrapper_attributes'])) { Chris@0: $attributes = $element[$key]['#wrapper_attributes']; Chris@0: } Chris@0: Chris@0: $variables['tabs'][$key] = [ Chris@0: 'link' => $element[$key]['tab'], Chris@0: 'attributes' => new Attribute($attributes), Chris@0: ]; Chris@0: } Chris@0: Chris@0: // Add other non-tray, non-tab child elements to the remainder variable for Chris@0: // later rendering. Chris@0: foreach (Element::children($element[$key]) as $child_key) { Chris@0: if (!in_array($child_key, ['tray', 'tab'])) { Chris@0: $variables['remainder'][$key][$child_key] = $element[$key][$child_key]; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_toolbar(). Chris@0: */ Chris@0: function toolbar_toolbar() { Chris@0: // The 'Home' tab is a simple link, with no corresponding tray. Chris@0: $items['home'] = [ Chris@0: '#type' => 'toolbar_item', Chris@0: 'tab' => [ Chris@0: '#type' => 'link', Chris@0: '#title' => t('Back to site'), Chris@0: '#url' => Url::fromRoute(''), Chris@0: '#attributes' => [ Chris@0: 'title' => t('Return to site content'), Chris@0: 'class' => ['toolbar-icon', 'toolbar-icon-escape-admin'], Chris@0: 'data-toolbar-escape-admin' => TRUE, Chris@0: ], Chris@0: ], Chris@0: '#wrapper_attributes' => [ Chris@0: 'class' => ['home-toolbar-tab'], Chris@0: ], Chris@0: '#attached' => [ Chris@0: 'library' => [ Chris@0: 'toolbar/toolbar.escapeAdmin', Chris@0: ], Chris@0: ], Chris@0: '#weight' => -20, Chris@0: ]; Chris@0: Chris@0: // To conserve bandwidth, we only include the top-level links in the HTML. Chris@0: // The subtrees are fetched through a JSONP script that is generated at the Chris@0: // toolbar_subtrees route. We provide the JavaScript requesting that JSONP Chris@0: // script here with the hash parameter that is needed for that route. Chris@0: // @see toolbar_subtrees_jsonp() Chris@0: list($hash, $hash_cacheability) = _toolbar_get_subtrees_hash(); Chris@0: $subtrees_attached['drupalSettings']['toolbar'] = [ Chris@0: 'subtreesHash' => $hash, Chris@0: ]; Chris@0: Chris@0: // The administration element has a link that is themed to correspond to Chris@0: // a toolbar tray. The tray contains the full administrative menu of the site. Chris@0: $items['administration'] = [ Chris@0: '#type' => 'toolbar_item', Chris@0: 'tab' => [ Chris@0: '#type' => 'link', Chris@0: '#title' => t('Manage'), Chris@0: '#url' => Url::fromRoute('system.admin'), Chris@0: '#attributes' => [ Chris@0: 'title' => t('Admin menu'), Chris@0: 'class' => ['toolbar-icon', 'toolbar-icon-menu'], Chris@0: // A data attribute that indicates to the client to defer loading of Chris@0: // the admin menu subtrees until this tab is activated. Admin menu Chris@0: // subtrees will not render to the DOM if this attribute is removed. Chris@0: // The value of the attribute is intentionally left blank. Only the Chris@0: // presence of the attribute is necessary. Chris@0: 'data-drupal-subtrees' => '', Chris@0: ], Chris@0: ], Chris@0: 'tray' => [ Chris@0: '#heading' => t('Administration menu'), Chris@0: '#attached' => $subtrees_attached, Chris@0: 'toolbar_administration' => [ Chris@0: '#pre_render' => [ Chris@0: 'toolbar_prerender_toolbar_administration_tray', Chris@0: ], Chris@0: '#type' => 'container', Chris@0: '#attributes' => [ Chris@0: 'class' => ['toolbar-menu-administration'], Chris@0: ], Chris@0: ], Chris@0: ], Chris@0: '#weight' => -15, Chris@0: ]; Chris@0: $hash_cacheability->applyTo($items['administration']); Chris@0: Chris@0: return $items; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Renders the toolbar's administration tray. Chris@0: * Chris@0: * @param array $element Chris@0: * A renderable array. Chris@0: * Chris@0: * @return array Chris@0: * The updated renderable array. Chris@0: * Chris@16: * @see \Drupal\Core\Render\RendererInterface::render() Chris@0: */ Chris@0: function toolbar_prerender_toolbar_administration_tray(array $element) { Chris@0: $menu_tree = \Drupal::service('toolbar.menu_tree'); Chris@0: // Load the administrative menu. The first level is the "Administration" link. Chris@0: // In order to load the children of that link, start and end on the second Chris@0: // level. Chris@0: $parameters = new MenuTreeParameters(); Chris@0: $parameters->setMinDepth(2)->setMaxDepth(2)->onlyEnabledLinks(); Chris@0: // @todo Make the menu configurable in https://www.drupal.org/node/1869638. Chris@0: $tree = $menu_tree->load('admin', $parameters); Chris@0: $manipulators = [ Chris@0: ['callable' => 'menu.default_tree_manipulators:checkAccess'], Chris@0: ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], Chris@0: ['callable' => 'toolbar_menu_navigation_links'], Chris@0: ]; Chris@0: $tree = $menu_tree->transform($tree, $manipulators); Chris@0: $element['administration_menu'] = $menu_tree->build($tree); Chris@0: return $element; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds toolbar-specific attributes to the menu link tree. Chris@0: * Chris@0: * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree Chris@0: * The menu link tree to manipulate. Chris@0: * Chris@0: * @return \Drupal\Core\Menu\MenuLinkTreeElement[] Chris@0: * The manipulated menu link tree. Chris@0: */ Chris@0: function toolbar_menu_navigation_links(array $tree) { Chris@0: foreach ($tree as $element) { Chris@0: if ($element->subtree) { Chris@0: toolbar_menu_navigation_links($element->subtree); Chris@0: } Chris@0: Chris@0: // Make sure we have a path specific ID in place, so we can attach icons Chris@0: // and behaviors to the menu links. Chris@0: $link = $element->link; Chris@0: $url = $link->getUrlObject(); Chris@0: if (!$url->isRouted()) { Chris@0: // This is an unusual case, so just get a distinct, safe string. Chris@0: $id = substr(Crypt::hashBase64($url->getUri()), 0, 16); Chris@0: } Chris@0: else { Chris@0: $id = str_replace(['.', '<', '>'], ['-', '', ''], $url->getRouteName()); Chris@0: } Chris@0: Chris@0: // Get the non-localized title to make the icon class. Chris@0: $definition = $link->getPluginDefinition(); Chris@0: Chris@0: $element->options['attributes']['id'] = 'toolbar-link-' . $id; Chris@0: $element->options['attributes']['class'][] = 'toolbar-icon'; Chris@0: $element->options['attributes']['class'][] = 'toolbar-icon-' . strtolower(str_replace(['.', ' ', '_'], ['-', '-', '-'], $definition['id'])); Chris@0: $element->options['attributes']['title'] = $link->getDescription(); Chris@0: } Chris@0: return $tree; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_preprocess_HOOK() for HTML document templates. Chris@0: */ Chris@0: function toolbar_preprocess_html(&$variables) { Chris@0: if (!\Drupal::currentUser()->hasPermission('access toolbar')) { Chris@0: return; Chris@0: } Chris@18: $variables['attributes']['class'][] = 'toolbar-tray-open'; Chris@18: $variables['attributes']['class'][] = 'toolbar-horizontal'; Chris@18: $variables['attributes']['class'][] = 'toolbar-fixed'; Chris@18: $variables['attributes']['class'][] = 'toolbar-loading'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the rendered subtree of each top-level toolbar link. Chris@0: * Chris@0: * @return array Chris@0: * An array with the following key-value pairs: Chris@0: * - 'subtrees': the rendered subtrees Chris@0: * - 'cacheability: the associated cacheability. Chris@0: */ Chris@0: function toolbar_get_rendered_subtrees() { Chris@0: $data = [ Chris@0: '#pre_render' => ['_toolbar_do_get_rendered_subtrees'], Chris@0: '#cache' => [ Chris@0: 'keys' => [ Chris@0: 'toolbar_rendered_subtrees', Chris@0: ], Chris@0: ], Chris@0: '#cache_properties' => ['#subtrees'], Chris@0: ]; Chris@0: \Drupal::service('renderer')->renderPlain($data); Chris@0: return [$data['#subtrees'], CacheableMetadata::createFromRenderArray($data)]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * #pre_render callback for toolbar_get_rendered_subtrees(). Chris@0: */ Chris@0: function _toolbar_do_get_rendered_subtrees(array $data) { Chris@0: $menu_tree = \Drupal::service('toolbar.menu_tree'); Chris@0: // Load the administration menu. The first level is the "Administration" link. Chris@0: // In order to load the children of that link and the subsequent two levels, Chris@0: // start at the second level and end at the fourth. Chris@0: $parameters = new MenuTreeParameters(); Chris@0: $parameters->setMinDepth(2)->setMaxDepth(4)->onlyEnabledLinks(); Chris@0: // @todo Make the menu configurable in https://www.drupal.org/node/1869638. Chris@0: $tree = $menu_tree->load('admin', $parameters); Chris@0: $manipulators = [ Chris@0: ['callable' => 'menu.default_tree_manipulators:checkAccess'], Chris@0: ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], Chris@0: ['callable' => 'toolbar_menu_navigation_links'], Chris@0: ]; Chris@0: $tree = $menu_tree->transform($tree, $manipulators); Chris@0: $subtrees = []; Chris@0: // Calculated the combined cacheability of all subtrees. Chris@0: $cacheability = new CacheableMetadata(); Chris@0: foreach ($tree as $element) { Chris@0: /** @var \Drupal\Core\Menu\MenuLinkInterface $link */ Chris@0: $link = $element->link; Chris@0: if ($element->subtree) { Chris@0: $subtree = $menu_tree->build($element->subtree); Chris@0: $output = \Drupal::service('renderer')->renderPlain($subtree); Chris@0: $cacheability = $cacheability->merge(CacheableMetadata::createFromRenderArray($subtree)); Chris@0: } Chris@0: else { Chris@0: $output = ''; Chris@0: } Chris@0: // Many routes have dots as route name, while some special ones like Chris@0: // have <> characters in them. Chris@0: $url = $link->getUrlObject(); Chris@0: $id = str_replace(['.', '<', '>'], ['-', '', ''], $url->isRouted() ? $url->getRouteName() : $url->getUri()); Chris@0: Chris@0: $subtrees[$id] = $output; Chris@0: } Chris@0: Chris@0: // Store the subtrees, along with the cacheability metadata. Chris@0: $cacheability->applyTo($data); Chris@0: $data['#subtrees'] = $subtrees; Chris@0: Chris@0: return $data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the hash of the per-user rendered toolbar subtrees. Chris@0: * Chris@0: * @return string Chris@0: * The hash of the admin_menu subtrees. Chris@0: */ Chris@0: function _toolbar_get_subtrees_hash() { Chris@0: list($subtrees, $cacheability) = toolbar_get_rendered_subtrees(); Chris@0: $hash = Crypt::hashBase64(serialize($subtrees)); Chris@0: return [$hash, $cacheability]; Chris@0: }