Security update for Core, with self-updated composer
[yaffs-website] / web / modules / contrib / admin_toolbar / admin_toolbar_tools / admin_toolbar_tools.module
1 <?php
2
3 /**
4  * @file
5  * Provides extra menu links for the core drupal toolbar.
6  */
7
8 use Drupal\Core\Routing\RouteMatchInterface;
9
10 /**
11  * Implements hook_toolbar().
12  */
13 function admin_toolbar_tools_toolbar() {
14   $items = array();
15   $items['admin_toolbar_tools'] = array(
16     '#type' => 'toolbar_item',
17     'tab' => array(
18       '#type' => 'link',
19       '#attributes' => array(
20         'class' => array('toolbar-icon', 'toolbar-icon-admin-toolbar-tools-help'),
21       ),
22     ),
23     '#attached' => array('library' => array('admin_toolbar_tools/toolbar.icon'),
24     ),
25   );
26
27   return $items;
28 }
29
30 /**
31  * Implements hook_help().
32  */
33 function admin_toolbar_tools_help($route_name, RouteMatchInterface $route_match) {
34   switch ($route_name) {
35     case 'help.page.admin_toolbar_tools':
36       $output = '';
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>';
40
41       return $output;
42   }
43 }
44
45 /**
46  * Implements hook_menu_links_discovered_alter().
47  */
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');
52   $routes = array();
53   foreach ($routeProvider->getAllRoutes() as $route_name => $route) {
54     $routes[] = $route_name;
55   }
56
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(),
64       );
65     }
66   }
67
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),
84         );
85       }
86       if (in_array('entity.' . $content_entity_bundle . '.edit_form', $routes)) {
87         $key = 'entity.' . $content_entity_bundle . '.edit_form.' . $machine_name;
88         $links[$key] = array(
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),
94         );
95         if (empty($content_entity_bundle_root)) {
96           $content_entity_bundle_root = $key;
97         }
98         else {
99           $links[$key]['parent'] = $content_entity_bundle_root;
100           $links[$key]['title'] = t('Edit');
101         }
102       }
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),
111             'weight' => 1,
112           );
113         }
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),
121             'weight' => 2,
122           );
123         }
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),
131             'weight' => 3,
132           );
133         }
134       }
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),
142           'weight' => 4,
143         );
144       }
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),
152           'weight' => 5,
153         );
154       }
155     }
156   }
157
158   // Add user links.
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',
164   );
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',
170   );
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',
176   );
177   $links['admin_toolbar_tools.user.logout'] = array(
178     'title' => t('Logout'),
179     'route_name' => 'user.logout',
180     'parent' => 'admin_toolbar_tools.help',
181     'weight' => 10,
182   );
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',
188     'weight' => -5,
189   );
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',
196     );
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',
202     );
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',
208     );
209   }
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()),
217     );
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()),
224     );
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()),
231     );
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()),
239       );
240     }
241   }
242
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',
250       'weight' => -5,
251     );
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()),
259       );
260     }
261   }
262
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',
269     );
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',
275     );
276   }
277
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',
284       'weight' => -5,
285     );
286   }
287
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',
294       'weight' => -50,
295     );
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(
299         'title' => $label,
300         'route_name' => 'entity.menu.edit_form',
301         'menu_name' => 'admin',
302         'parent' => 'entity.menu.collection',
303         'route_parameters' => array('menu' => $machine_name),
304       );
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),
311       );
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),
319         );
320       }
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),
327       );
328     }
329   }
330
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',
338       'weight' => -100,
339     );
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',
345     );
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',
351     );
352   }
353
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',
361       'weight' => -5,
362     );
363   }
364
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',
372     );
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',
378     );
379   }
380
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',
388       'weight' => '-8',
389     );
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',
395       'weight' => '-1',
396     );
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',
402     );
403     $links['admin_toolbar_tools.devel.reinstall'] = array(
404       'title' => t('Reinstall modules'),
405       'route_name' => 'devel.reinstall',
406       'parent' => 'admin_development',
407     );
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',
413     );
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',
419     );
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',
425     );
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',
431     );
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',
437     );
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',
443     );
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',
449     );
450   }
451
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',
459       'weight' => -5,
460     );
461   }
462
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',
468   );
469
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',
476     );
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',
482     );
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,
493         ),
494       );
495     }
496   }
497 }
498
499 /**
500  * Return installed themes.
501  *
502  * @return array
503  *   An array of friendly theme names, keyed by the machine name.
504  */
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();
511     }
512   }
513
514   return $themes_installed;
515 }
516
517 /**
518  * Get all links related to entity.
519  *
520  * @param string $entity_type_id
521  *   The machine name for the entity type.
522  *
523  * @return array
524  *   The links for the entity type.
525  */
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();
530
531   return $links;
532 }