Version 1
[yaffs-website] / web / modules / contrib / admin_toolbar / admin_toolbar.module
1 <?php
2
3 /**
4  * @file
5  * This is the module to create a drop-down menu for the core toolbar.
6  */
7
8 use Drupal\Core\Menu\MenuTreeParameters;
9 use Drupal\Core\Routing\RouteMatchInterface;
10 use Drupal\Core\Url;
11
12 /**
13  * Implements hook_toolbar_alter().
14  */
15 function admin_toolbar_toolbar_alter(&$items) {
16   $items['administration']['tray']['toolbar_administration']['#pre_render'] = array('admin_toolbar_prerender_toolbar_administration_tray');
17   $items['administration']['#attached']['library'][] = 'admin_toolbar/toolbar.tree';
18 }
19
20 /**
21  * Implements hook_help().
22  */
23 function admin_toolbar_help($route_name, RouteMatchInterface $route_match) {
24   switch ($route_name) {
25     case 'help.page.admin_toolbar':
26       $output = '';
27       $output .= '<h3>' . t('About') . '</h3>';
28       $output .= '<p>' . t('The Admin Toolbar module enhances the <a href=":toolbar">Toolbar</a> module by providing fast access to all the administrative links at the top of your site. Admin Toolbar remains a very "lightweight" module by closely integrating with all Toolbar functionality. It can be used in conjunction with all the sub or complimentary modules, listed on <a href="https://www.drupal.org/project/admin_toolbar">Admin Toolbar</a>, for quick access to system commands such as Flush all caches, <a href=":automated_cron">Run cron</a>, Run Updates, etc... For more information, see <a href=":admin_toolbar_documentation">the online documentation for the Admin Toolbar module</a>.', array(':toolbar' => Url::fromRoute('help.page', array('name' => 'toolbar'))->toString(), ':automated_cron' => (\Drupal::moduleHandler()->moduleExists('automated_cron')) ? Url::fromRoute('help.page', array('name' => 'automated_cron'))->toString() : '#', ':admin_toolbar_documentation' => 'https://www.drupal.org/node/2713693')) . '</p>';
29       $output .= '<h3>' . t('Uses') . '</h3>';
30       $output .= '<p>' . t('The Admin Toolbar greatly improves the user experience for those who regularly interact with the Drupal Toolbar by providing fast, full access to all links in the Drupal Toolbar without having to click to get there.') . '</p>';
31
32       return $output;
33   }
34 }
35
36 /**
37  * Renders the toolbar's administration tray.
38  *
39  * This is a clone of core's toolbar_prerender_toolbar_administration_tray()
40  * function, which uses setMaxDepth(4) instead of setTopLevelOnly().
41  *
42  * @param array $element
43  *   A renderable array.
44  *
45  * @return array
46  *   The updated renderable array.
47  *
48  * @see toolbar_prerender_toolbar_administration_tray()
49  */
50 function admin_toolbar_prerender_toolbar_administration_tray(array $element) {
51   $menu_tree = \Drupal::service('toolbar.menu_tree');
52   $parameters = new MenuTreeParameters();
53   $parameters->setRoot('system.admin')->excludeRoot()->setMaxDepth(4)->onlyEnabledLinks();
54   $tree = $menu_tree->load(NULL, $parameters);
55   $manipulators = array(
56     array('callable' => 'menu.default_tree_manipulators:checkAccess'),
57     array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
58     array('callable' => 'toolbar_tools_menu_navigation_links'),
59   );
60   $tree = $menu_tree->transform($tree, $manipulators);
61   $element['administration_menu'] = $menu_tree->build($tree);
62
63   return $element;
64 }
65
66 /**
67  * Adds toolbar-specific attributes to the menu link tree.
68  *
69  * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree
70  *   The menu link tree to manipulate.
71  *
72  * @return \Drupal\Core\Menu\MenuLinkTreeElement[]
73  *   The manipulated menu link tree.
74  */
75 function toolbar_tools_menu_navigation_links(array $tree) {
76   foreach ($tree as $element) {
77     if ($element->subtree) {
78       toolbar_tools_menu_navigation_links($element->subtree);
79     }
80     $link = $element->link;
81
82     // Get the non-localized title to make the icon class.
83     $definition = $link->getPluginDefinition();
84
85     $element->options['attributes']['class'][] = 'toolbar-icon';
86     $element->options['attributes']['class'][] = 'toolbar-icon-' . strtolower(str_replace(array('.', ' ', '_'), array('-', '-', '-'), $definition['id']));
87     $element->options['attributes']['title'] = $link->getDescription();
88   }
89   return $tree;
90 }