5 * Provides extra menu links for the core drupal toolbar.
8 use Drupal\Core\Routing\RouteMatchInterface;
11 * Implements hook_toolbar().
13 function admin_toolbar_tools_toolbar() {
15 $items['admin_toolbar_tools'] = array(
16 '#type' => 'toolbar_item',
19 '#attributes' => array(
20 'class' => array('toolbar-icon', 'toolbar-icon-admin-toolbar-tools-help'),
23 '#attached' => array('library' => array('admin_toolbar_tools/toolbar.icon'),
31 * Implements hook_help().
33 function admin_toolbar_tools_help($route_name, RouteMatchInterface $route_match) {
34 switch ($route_name) {
35 case 'help.page.admin_toolbar_tools':
37 $output .= '<p>' . t('The Admin Toolbar Extra Tools module comes packaged with the <a href="https://www.drupal.org/project/admin_toolbar"> Admin Toolbar</a> module and adds functionality to it. The additional functionality is accessed thru extra links on the main administration <a href="https://www.drupal.org/project/toolbar">Toolbar</a>. Some links to Admin Toolbar Extra Tools administration pages are located at the bottom of this page. For more information, see the <a href="https://www.drupal.org/node/2713693">online documentation for Admin Toolbar</a>') . '</p>';
38 $output .= '<h3>' . t('Uses') . '</h3>';
39 $output .= '<p>' . t('To use Admin Toolbar Extra Tools just install it like any other module. There is no other configuration required. The Admin Toolbar functionality can be further extended by installing complimentary modules. See <a href="https://www.drupal.org/project/admin_toolbar">Admin Toolbar</a> for a complete listing of these complimentary modules.') . '</p>';
46 * Implements hook_menu_links_discovered_alter().
48 function admin_toolbar_tools_menu_links_discovered_alter(&$links) {
49 $moduleHandler = \Drupal::moduleHandler();
50 $entityTypeManager = \Drupal::entityTypeManager();
51 $routeProvider = \Drupal::service('router.route_provider');
53 foreach ($routeProvider->getAllRoutes() as $route_name => $route) {
54 $routes[] = $route_name;
57 $entityTypes = $entityTypeManager->getDefinitions();
58 $content_entities = array();
59 foreach ($entityTypes as $key => $entityType) {
60 if ($entityType->getBundleEntityType() && ($entityType->get('field_ui_base_route') != '')) {
61 $content_entities[$key] = array(
62 'content_entity' => $key,
63 'content_entity_bundle' => $entityType->getBundleEntityType(),
68 // Adds common links to entities.
69 foreach ($content_entities as $module_name => $entities) {
70 $content_entity_bundle = $entities['content_entity_bundle'];
71 $content_entity = $entities['content_entity'];
72 foreach ($entityTypeManager->getStorage($content_entity_bundle)->loadMultiple() as $machine_name => $bundle) {
73 // Normally, the edit form for the bundle would be its root link.
74 $content_entity_bundle_root = NULL;
75 if (in_array('entity.' . $content_entity_bundle . '.overview_form', $routes)) {
76 // Some bundles have an overview/list form that make a better root link.
77 $content_entity_bundle_root = 'entity.' . $content_entity_bundle . '.overview_form.' . $machine_name;
78 $links[$content_entity_bundle_root] = array(
79 'title' => $bundle->label(),
80 'route_name' => 'entity.' . $content_entity_bundle . '.overview_form',
81 'menu_name' => 'admin',
82 'parent' => 'entity.' . $content_entity_bundle . '.collection',
83 'route_parameters' => array($content_entity_bundle => $machine_name),
86 if (in_array('entity.' . $content_entity_bundle . '.edit_form', $routes)) {
87 $key = 'entity.' . $content_entity_bundle . '.edit_form.' . $machine_name;
89 'title' => $bundle->label(),
90 'route_name' => 'entity.' . $content_entity_bundle . '.edit_form',
91 'menu_name' => 'admin',
92 'parent' => 'entity.' . $content_entity_bundle . '.collection',
93 'route_parameters' => array($content_entity_bundle => $machine_name),
95 if (empty($content_entity_bundle_root)) {
96 $content_entity_bundle_root = $key;
99 $links[$key]['parent'] = $content_entity_bundle_root;
100 $links[$key]['title'] = t('Edit');
103 if ($moduleHandler->moduleExists('field_ui')) {
104 if (in_array('entity.' . $content_entity . '.field_ui_fields', $routes)) {
105 $links['entity.' . $content_entity . '.field_ui_fields' . $machine_name] = array(
106 'title' => t('Manage fields'),
107 'route_name' => 'entity.' . $content_entity . '.field_ui_fields',
108 'menu_name' => 'admin',
109 'parent' => $content_entity_bundle_root,
110 'route_parameters' => array($content_entity_bundle => $machine_name),
114 if (in_array('entity.entity_form_display.' . $content_entity . '.default', $routes)) {
115 $links['entity.entity_form_display.' . $content_entity . '.default' . $machine_name] = array(
116 'title' => t('Manage form display'),
117 'route_name' => 'entity.entity_form_display.' . $content_entity . '.default',
118 'menu_name' => 'admin',
119 'parent' => $content_entity_bundle_root,
120 'route_parameters' => array($content_entity_bundle => $machine_name),
124 if (in_array('entity.entity_view_display.' . $content_entity . '.default', $routes)) {
125 $links['entity.entity_view_display.' . $content_entity . '.default.' . $machine_name] = array(
126 'title' => t('Manage display'),
127 'route_name' => 'entity.entity_view_display.' . $content_entity . '.default',
128 'menu_name' => 'admin',
129 'parent' => $content_entity_bundle_root,
130 'route_parameters' => array($content_entity_bundle => $machine_name),
135 if ($moduleHandler->moduleExists('devel') && in_array('entity.' . $content_entity_bundle . '.devel_load', $routes)) {
136 $links['entity.' . $content_entity_bundle . '.devel_load.' . $machine_name] = array(
137 'title' => t('Devel'),
138 'route_name' => 'entity.' . $content_entity_bundle . '.devel_load',
139 'menu_name' => 'admin',
140 'parent' => $content_entity_bundle_root,
141 'route_parameters' => array($content_entity_bundle => $machine_name),
145 if (in_array('entity.' . $content_entity_bundle . '.delete_form', $routes)) {
146 $links['entity.' . $content_entity_bundle . '.delete_form.' . $machine_name] = array(
147 'title' => t('Delete'),
148 'route_name' => 'entity.' . $content_entity_bundle . '.delete_form',
149 'menu_name' => 'admin',
150 'parent' => $content_entity_bundle_root,
151 'route_parameters' => array($content_entity_bundle => $machine_name),
159 $links['user.admin_create'] = array(
160 'title' => t('Add a new user'),
161 'route_name' => 'user.admin_create',
162 'menu_name' => 'admin',
163 'parent' => 'entity.user.collection',
165 $links['user.admin_permissions'] = array(
166 'title' => t('Permissions'),
167 'route_name' => 'user.admin_permissions',
168 'menu_name' => 'admin',
169 'parent' => 'entity.user.collection',
171 $links['entity.user_role.collection'] = array(
172 'title' => t('Roles'),
173 'route_name' => 'entity.user_role.collection',
174 'menu_name' => 'admin',
175 'parent' => 'entity.user.collection',
177 $links['admin_toolbar_tools.user.logout'] = array(
178 'title' => t('Logout'),
179 'route_name' => 'user.logout',
180 'parent' => 'admin_toolbar_tools.help',
183 $links['user.role_add'] = array(
184 'title' => t('Add a new role'),
185 'route_name' => 'user.role_add',
186 'menu_name' => 'admin',
187 'parent' => 'entity.user_role.collection',
190 if ($moduleHandler->moduleExists('field_ui')) {
191 $links['entity.user.field_ui_fields_'] = array(
192 'title' => t('Manage fields'),
193 'route_name' => 'entity.user.field_ui_fields',
194 'menu_name' => 'admin',
195 'parent' => 'entity.user.admin_form',
197 $links['entity.entity_form_display.user.default_'] = array(
198 'title' => t('Manage form display'),
199 'route_name' => 'entity.entity_form_display.user.default',
200 'menu_name' => 'admin',
201 'parent' => 'entity.user.admin_form',
203 $links['entity.entity_view_display.user.default_'] = array(
204 'title' => t('Manage display'),
205 'route_name' => 'entity.entity_view_display.user.default',
206 'menu_name' => 'admin',
207 'parent' => 'entity.user.admin_form',
210 foreach (user_roles() as $role) {
211 $links['entity.user_role.edit_form.' . $role->id()] = array(
212 'title' => $role->label(),
213 'route_name' => 'entity.user_role.edit_form',
214 'menu_name' => 'admin',
215 'parent' => 'entity.user_role.collection',
216 'route_parameters' => array('user_role' => $role->id()),
218 $links['entity.user_role.edit_permissions_form.' . $role->id()] = array(
219 'title' => t('Edit permissions'),
220 'route_name' => 'entity.user_role.edit_permissions_form',
221 'menu_name' => 'admin',
222 'parent' => 'entity.user_role.edit_form.' . $role->id(),
223 'route_parameters' => array('user_role' => $role->id()),
225 $links['entity.user_role.delete_form.' . $role->id()] = array(
226 'title' => t('Delete'),
227 'route_name' => 'entity.user_role.delete_form',
228 'menu_name' => 'admin',
229 'parent' => 'entity.user_role.edit_form.' . $role->id(),
230 'route_parameters' => array('user_role' => $role->id()),
232 if ($moduleHandler->moduleExists('devel')) {
233 $links['entity.user_role.devel_load.' . $role->id()] = array(
234 'title' => t('Devel'),
235 'route_name' => 'entity.user_role.devel_load',
236 'menu_name' => 'admin',
237 'parent' => 'entity.user_role.edit_form.' . $role->id(),
238 'route_parameters' => array('user_role' => $role->id()),
243 if ($moduleHandler->moduleExists('node')) {
244 $links['node.add_page']['parent'] = 'system.admin_content';
245 $links['node.type_add'] = array(
246 'title' => t('Add content type'),
247 'route_name' => 'node.type_add',
248 'menu_name' => 'admin',
249 'parent' => 'entity.node_type.collection',
252 // Add node links for each content type.
253 foreach (\Drupal::entityTypeManager()->getStorage('node_type')->loadMultiple() as $type) {
254 $links['node.add.' . $type->id()] = array(
255 'title' => t($type->label()),
256 'route_name' => 'node.add',
257 'parent' => 'node.add_page',
258 'route_parameters' => array('node_type' => $type->id()),
263 if ($moduleHandler->moduleExists('field_ui')) {
264 $links['field_ui.entity_form_mode_add'] = array(
265 'title' => t('Add new form mode'),
266 'route_name' => 'field_ui.entity_form_mode_add',
267 'menu_name' => 'admin',
268 'parent' => 'entity.entity_form_mode.collection',
270 $links['field_ui.entity_view_mode_add'] = array(
271 'title' => t('Add new view mode'),
272 'route_name' => 'field_ui.entity_view_mode_add',
273 'menu_name' => 'admin',
274 'parent' => 'entity.entity_view_mode.collection',
278 if ($moduleHandler->moduleExists('taxonomy')) {
279 $links['entity.taxonomy_vocabulary.add_form'] = array(
280 'title' => t('Add vocabulary'),
281 'route_name' => 'entity.taxonomy_vocabulary.add_form',
282 'menu_name' => 'admin',
283 'parent' => 'entity.taxonomy_vocabulary.collection',
288 if ($moduleHandler->moduleExists('menu_ui')) {
289 $links['entity.menu.add_form'] = array(
290 'title' => t('Add menu'),
291 'route_name' => 'entity.menu.add_form',
292 'menu_name' => 'admin',
293 'parent' => 'entity.menu.collection',
296 // Adds links to /admin/structure/menu.
297 foreach (menu_ui_get_menus() as $machine_name => $label) {
298 $links['entity.menu.edit_form.' . $machine_name] = array(
300 'route_name' => 'entity.menu.edit_form',
301 'menu_name' => 'admin',
302 'parent' => 'entity.menu.collection',
303 'route_parameters' => array('menu' => $machine_name),
305 $links['entity.menu.delete_form.' . $machine_name] = array(
306 'title' => t('Delete'),
307 'route_name' => 'entity.menu.delete_form',
308 'menu_name' => 'admin',
309 'parent' => 'entity.menu.edit_form.' . $machine_name,
310 'route_parameters' => array('menu' => $machine_name),
312 if ($moduleHandler->moduleExists('devel')) {
313 $links['entity.menu.devel_load.' . $machine_name] = array(
314 'title' => t('Devel'),
315 'route_name' => 'entity.menu.devel_load',
316 'menu_name' => 'admin',
317 'parent' => 'entity.menu.edit_form.' . $machine_name,
318 'route_parameters' => array('menu' => $machine_name),
321 $links['entity.menu.add_link_form.' . $machine_name] = array(
322 'title' => t('Add link'),
323 'route_name' => 'entity.menu.add_link_form',
324 'menu_name' => 'admin',
325 'parent' => 'entity.menu.edit_form.' . $machine_name,
326 'route_parameters' => array('menu' => $machine_name),
331 // If module block_content is enabled.
332 if ($moduleHandler->moduleExists('block_content')) {
333 $links['block_content.add_page'] = array(
334 'title' => t('Add custom block'),
335 'route_name' => 'block_content.add_page',
336 'menu_name' => 'admin',
337 'parent' => 'block.admin_display',
340 $links['entity.block_content.collection'] = array(
341 'title' => t('Custom block library'),
342 'route_name' => 'entity.block_content.collection',
343 'menu_name' => 'admin',
344 'parent' => 'block.admin_display',
346 $links['entity.block_content_type.collection'] = array(
347 'title' => t('Types'),
348 'route_name' => 'entity.block_content_type.collection',
349 'menu_name' => 'admin',
350 'parent' => 'block.admin_display',
354 // If module Contact is enabled.
355 if ($moduleHandler->moduleExists('contact')) {
356 $links['contact.form_add'] = array(
357 'title' => t('Add contact form'),
358 'route_name' => 'contact.form_add',
359 'menu_name' => 'admin',
360 'parent' => 'entity.contact_form.collection',
365 // If module Update Manager is enabled.
366 if ($moduleHandler->moduleExists('update')) {
367 $links['update.module_update'] = array(
368 'title' => t('Update'),
369 'route_name' => 'update.module_update',
370 'menu_name' => 'admin',
371 'parent' => 'system.modules_list',
373 $links['update.module_install'] = array(
374 'title' => t('Install new module'),
375 'route_name' => 'update.module_install',
376 'menu_name' => 'admin',
377 'parent' => 'system.modules_list',
381 // If module Devel is enabled.
382 if ($moduleHandler->moduleExists('devel')) {
383 $links['admin_development'] = array(
384 'title' => t('Development'),
385 'route_name' => 'system.admin_config_development',
386 'menu_name' => 'admin',
387 'parent' => 'admin_toolbar_tools.help',
390 $links['admin_toolbar_tools.devel.admin_settings'] = array(
391 'title' => t('Devel settings'),
392 'route_name' => 'devel.admin_settings',
393 'menu_name' => 'admin',
394 'parent' => 'admin_development',
397 $links['admin_toolbar_tools.devel.configs_list'] = array(
398 'title' => t('Config editor'),
399 'route_name' => 'devel.configs_list',
400 'menu_name' => 'admin',
401 'parent' => 'admin_development',
403 $links['admin_toolbar_tools.devel.reinstall'] = array(
404 'title' => t('Reinstall modules'),
405 'route_name' => 'devel.reinstall',
406 'parent' => 'admin_development',
408 $links['admin_toolbar_tools.devel.menu_rebuild'] = array(
409 'title' => t('Rebuild menu'),
410 'route_name' => 'devel.menu_rebuild',
411 'menu_name' => 'admin',
412 'parent' => 'admin_development',
414 $links['admin_toolbar_tools.devel.state_system_page'] = array(
415 'title' => t('State editor'),
416 'route_name' => 'devel.state_system_page',
417 'menu_name' => 'admin',
418 'parent' => 'admin_development',
420 $links['admin_toolbar_tools.devel.theme_registry'] = array(
421 'title' => t('Theme registry'),
422 'route_name' => 'devel.theme_registry',
423 'menu_name' => 'admin',
424 'parent' => 'admin_development',
426 $links['admin_toolbar_tools.devel.entity_info_page'] = array(
427 'title' => t('Entity Info'),
428 'route_name' => 'devel.entity_info_page',
429 'menu_name' => 'admin',
430 'parent' => 'admin_development',
432 $links['admin_toolbar_tools.devel.execute_php'] = array(
433 'title' => t('Execute PHP Code'),
434 'route_name' => 'devel.execute_php',
435 'menu_name' => 'admin',
436 'parent' => 'admin_development',
438 $links['admin_toolbar_tools.devel.session'] = array(
439 'title' => t('Session viewer'),
440 'route_name' => 'devel.session',
441 'menu_name' => 'admin',
442 'parent' => 'admin_development',
444 $links['admin_toolbar_tools.devel.elements_page'] = array(
445 'title' => t('Form API field types'),
446 'route_name' => 'devel.elements_page',
447 'menu_name' => 'admin',
448 'parent' => 'admin_development',
452 // If module Views Ui enabled.
453 if ($moduleHandler->moduleExists('views_ui')) {
454 $links['views_ui.add'] = array(
455 'title' => t('Add new view'),
456 'route_name' => 'views_ui.add',
457 'menu_name' => 'admin',
458 'parent' => 'entity.view.collection',
463 $links['system.theme_settings_'] = array(
464 'title' => t('Settings'),
465 'route_name' => 'system.theme_settings',
466 'menu_name' => 'admin',
467 'parent' => 'system.themes_page',
470 if ($moduleHandler->moduleExists('update')) {
471 $links['update.theme_install_'] = array(
472 'title' => t('Install new theme'),
473 'route_name' => 'update.theme_install',
474 'menu_name' => 'admin',
475 'parent' => 'system.themes_page',
477 $links['update.theme_update_'] = array(
478 'title' => t('Update'),
479 'route_name' => 'update.theme_update',
480 'menu_name' => 'admin',
481 'parent' => 'system.themes_page',
483 // Lists installed themes.
484 $installed_themes = admin_toolbar_tools_installed_themes();
485 foreach ($installed_themes as $key_theme => $label_theme) {
486 $links['system.theme_settings_theme.' . $key_theme] = array(
487 'title' => t($label_theme),
488 'route_name' => 'system.theme_settings_theme',
489 'menu_name' => 'admin',
490 'parent' => 'system.theme_settings_',
491 'route_parameters' => array(
492 'theme' => $key_theme,
500 * Return installed themes.
503 * An array of friendly theme names, keyed by the machine name.
505 function admin_toolbar_tools_installed_themes() {
506 $all_themes = \Drupal::service('theme_handler')->listInfo();
507 $themes_installed = array();
508 foreach ($all_themes as $key_theme => $theme) {
509 if (\Drupal::service('theme_handler')->hasUi($key_theme)) {
510 $themes_installed[$key_theme] = $theme->getName();
514 return $themes_installed;
518 * Get all links related to entity.
520 * @param string $entity_type_id
521 * The machine name for the entity type.
524 * The links for the entity type.
526 function admin_toolbar_tools_get_links($entity_type_id) {
527 $entity = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
528 // Get all links related to entity.
529 $links = $entity->getLinkTemplates();