admin_toolbar_tools.module 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <?php
  2. /**
  3. * @file
  4. * Provides extra menu links for the core drupal toolbar.
  5. */
  6. use Drupal\Core\Link;
  7. use Drupal\Core\Routing\RouteMatchInterface;
  8. use Drupal\Core\Url;
  9. /**
  10. * Implements hook_toolbar().
  11. */
  12. function admin_toolbar_tools_toolbar() {
  13. $items = [];
  14. $items['admin_toolbar_tools'] = [
  15. '#type' => 'toolbar_item',
  16. 'tab' => [
  17. '#type' => 'link',
  18. '#attributes' => [
  19. 'class' => ['toolbar-icon', 'toolbar-icon-admin-toolbar-tools-help'],
  20. ],
  21. ],
  22. '#attached' => ['library' => ['admin_toolbar_tools/toolbar.icon']],
  23. ];
  24. return $items;
  25. }
  26. /**
  27. * Implements hook_help().
  28. */
  29. function admin_toolbar_tools_help($route_name, RouteMatchInterface $route_match) {
  30. switch ($route_name) {
  31. case 'help.page.admin_toolbar_tools':
  32. $url = Url::fromUri('https://www.drupal.org/project/admin_toolbar');
  33. $external_link = Link::fromTextAndUrl(t('Admin Toolbar'), $url);
  34. $external_link2 = Link::fromTextAndUrl(t('Toolbar'), $url);
  35. $url = Url::fromUri('https://www.drupal.org/node/2713693');
  36. $external_link3 = Link::fromTextAndUrl(t('online documentation for Admin Toolbar'), $url);
  37. $output = '';
  38. $output .= '<p>';
  39. $output .= t('The Admin Toolbar Extra Tools module comes packaged with the :link module and adds functionality to it. The additional functionality is accessed thru extra links on the main administration :link2. Some links to Admin Toolbar Extra Tools administration pages are located at the bottom of this page. For more information, see the :link3', [
  40. ':link' => $external_link,
  41. ':link2' => $external_link2,
  42. ':link3' => $external_link3,
  43. ]);
  44. $output .= '</p>';
  45. $output .= '<h3>' . t('Uses') . '</h3>';
  46. $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 :link for a complete listing of these complimentary modules.', [':link' => $external_link]) . '</p>';
  47. return $output;
  48. }
  49. }
  50. /**
  51. * Implements hook_menu_links_discovered_alter().
  52. */
  53. function admin_toolbar_tools_menu_links_discovered_alter(&$links) {
  54. $moduleHandler = \Drupal::moduleHandler();
  55. $entityTypeManager = \Drupal::entityTypeManager();
  56. $routeProvider = \Drupal::service('router.route_provider');
  57. $routes = [];
  58. foreach ($routeProvider->getAllRoutes() as $route_name => $route) {
  59. $routes[] = $route_name;
  60. }
  61. $entityTypes = $entityTypeManager->getDefinitions();
  62. $content_entities = [];
  63. foreach ($entityTypes as $key => $entityType) {
  64. if ($entityType->getBundleEntityType() && ($entityType->get('field_ui_base_route') != '')) {
  65. $content_entities[$key] = [
  66. 'content_entity' => $key,
  67. 'content_entity_bundle' => $entityType->getBundleEntityType(),
  68. ];
  69. }
  70. }
  71. // Adding a menu link to clean the Views cache.
  72. if ($moduleHandler->moduleExists('views')) {
  73. $links['admin_toolbar_tools.flush_views'] = [
  74. 'title' => t('Flush views cache'),
  75. 'route_name' => 'admin_toolbar_tools.flush_views',
  76. 'menu_name' => 'admin',
  77. 'parent' => 'admin_toolbar_tools.flush',
  78. ];
  79. }
  80. // Adds common links to entities.
  81. foreach ($content_entities as $entities) {
  82. $content_entity_bundle = $entities['content_entity_bundle'];
  83. $content_entity = $entities['content_entity'];
  84. foreach ($entityTypeManager->getStorage($content_entity_bundle)->loadMultiple() as $machine_name => $bundle) {
  85. // Normally, the edit form for the bundle would be its root link.
  86. $content_entity_bundle_root = NULL;
  87. if (in_array('entity.' . $content_entity_bundle . '.overview_form', $routes)) {
  88. // Some bundles have an overview/list form that make a better root link.
  89. $content_entity_bundle_root = 'entity.' . $content_entity_bundle . '.overview_form.' . $machine_name;
  90. $links[$content_entity_bundle_root] = [
  91. 'title' => t($bundle->label()),
  92. 'route_name' => 'entity.' . $content_entity_bundle . '.overview_form',
  93. 'menu_name' => 'admin',
  94. 'parent' => 'entity.' . $content_entity_bundle . '.collection',
  95. 'route_parameters' => [$content_entity_bundle => $machine_name],
  96. ];
  97. }
  98. if (in_array('entity.' . $content_entity_bundle . '.edit_form', $routes)) {
  99. $key = 'entity.' . $content_entity_bundle . '.edit_form.' . $machine_name;
  100. $links[$key] = [
  101. 'title' => t($bundle->label()),
  102. 'route_name' => 'entity.' . $content_entity_bundle . '.edit_form',
  103. 'menu_name' => 'admin',
  104. 'parent' => 'entity.' . $content_entity_bundle . '.collection',
  105. 'route_parameters' => [$content_entity_bundle => $machine_name],
  106. ];
  107. if (empty($content_entity_bundle_root)) {
  108. $content_entity_bundle_root = $key;
  109. }
  110. else {
  111. $links[$key]['parent'] = $content_entity_bundle_root;
  112. $links[$key]['title'] = t('Edit');
  113. }
  114. }
  115. if ($moduleHandler->moduleExists('field_ui')) {
  116. if (in_array('entity.' . $content_entity . '.field_ui_fields', $routes)) {
  117. $links['entity.' . $content_entity . '.field_ui_fields' . $machine_name] = [
  118. 'title' => t('Manage fields'),
  119. 'route_name' => 'entity.' . $content_entity . '.field_ui_fields',
  120. 'menu_name' => 'admin',
  121. 'parent' => $content_entity_bundle_root,
  122. 'route_parameters' => [$content_entity_bundle => $machine_name],
  123. 'weight' => 1,
  124. ];
  125. }
  126. if (in_array('entity.entity_form_display.' . $content_entity . '.default', $routes)) {
  127. $links['entity.entity_form_display.' . $content_entity . '.default' . $machine_name] = [
  128. 'title' => t('Manage form display'),
  129. 'route_name' => 'entity.entity_form_display.' . $content_entity . '.default',
  130. 'menu_name' => 'admin',
  131. 'parent' => $content_entity_bundle_root,
  132. 'route_parameters' => [$content_entity_bundle => $machine_name],
  133. 'weight' => 2,
  134. ];
  135. }
  136. if (in_array('entity.entity_view_display.' . $content_entity . '.default', $routes)) {
  137. $links['entity.entity_view_display.' . $content_entity . '.default.' . $machine_name] = [
  138. 'title' => t('Manage display'),
  139. 'route_name' => 'entity.entity_view_display.' . $content_entity . '.default',
  140. 'menu_name' => 'admin',
  141. 'parent' => $content_entity_bundle_root,
  142. 'route_parameters' => [$content_entity_bundle => $machine_name],
  143. 'weight' => 3,
  144. ];
  145. }
  146. }
  147. if ($moduleHandler->moduleExists('devel') && in_array('entity.' . $content_entity_bundle . '.devel_load', $routes)) {
  148. $links['entity.' . $content_entity_bundle . '.devel_load.' . $machine_name] = [
  149. 'title' => t('Devel'),
  150. 'route_name' => 'entity.' . $content_entity_bundle . '.devel_load',
  151. 'menu_name' => 'admin',
  152. 'parent' => $content_entity_bundle_root,
  153. 'route_parameters' => [$content_entity_bundle => $machine_name],
  154. 'weight' => 4,
  155. ];
  156. }
  157. if (in_array('entity.' . $content_entity_bundle . '.delete_form', $routes)) {
  158. $links['entity.' . $content_entity_bundle . '.delete_form.' . $machine_name] = [
  159. 'title' => t('Delete'),
  160. 'route_name' => 'entity.' . $content_entity_bundle . '.delete_form',
  161. 'menu_name' => 'admin',
  162. 'parent' => $content_entity_bundle_root,
  163. 'route_parameters' => [$content_entity_bundle => $machine_name],
  164. 'weight' => 5,
  165. ];
  166. }
  167. }
  168. }
  169. // Add user links.
  170. $links['user.admin_create'] = [
  171. 'title' => t('Add a new user'),
  172. 'route_name' => 'user.admin_create',
  173. 'menu_name' => 'admin',
  174. 'parent' => 'entity.user.collection',
  175. ];
  176. $links['user.admin_permissions'] = [
  177. 'title' => t('Permissions'),
  178. 'route_name' => 'user.admin_permissions',
  179. 'menu_name' => 'admin',
  180. 'parent' => 'entity.user.collection',
  181. ];
  182. $links['entity.user_role.collection'] = [
  183. 'title' => t('Roles'),
  184. 'route_name' => 'entity.user_role.collection',
  185. 'menu_name' => 'admin',
  186. 'parent' => 'entity.user.collection',
  187. ];
  188. $links['admin_toolbar_tools.user.logout'] = [
  189. 'title' => t('Logout'),
  190. 'route_name' => 'user.logout',
  191. 'parent' => 'admin_toolbar_tools.help',
  192. 'weight' => 10,
  193. ];
  194. $links['user.role_add'] = [
  195. 'title' => t('Add a new role'),
  196. 'route_name' => 'user.role_add',
  197. 'menu_name' => 'admin',
  198. 'parent' => 'entity.user_role.collection',
  199. 'weight' => -5,
  200. ];
  201. if ($moduleHandler->moduleExists('field_ui')) {
  202. $links['entity.user.field_ui_fields_'] = [
  203. 'title' => t('Manage fields'),
  204. 'route_name' => 'entity.user.field_ui_fields',
  205. 'menu_name' => 'admin',
  206. 'parent' => 'entity.user.admin_form',
  207. ];
  208. $links['entity.entity_form_display.user.default_'] = [
  209. 'title' => t('Manage form display'),
  210. 'route_name' => 'entity.entity_form_display.user.default',
  211. 'menu_name' => 'admin',
  212. 'parent' => 'entity.user.admin_form',
  213. ];
  214. $links['entity.entity_view_display.user.default_'] = [
  215. 'title' => t('Manage display'),
  216. 'route_name' => 'entity.entity_view_display.user.default',
  217. 'menu_name' => 'admin',
  218. 'parent' => 'entity.user.admin_form',
  219. ];
  220. }
  221. foreach (user_roles() as $role) {
  222. $links['entity.user_role.edit_form.' . $role->id()] = [
  223. 'title' => t($role->label()),
  224. 'route_name' => 'entity.user_role.edit_form',
  225. 'menu_name' => 'admin',
  226. 'parent' => 'entity.user_role.collection',
  227. 'route_parameters' => ['user_role' => $role->id()],
  228. ];
  229. $links['entity.user_role.edit_permissions_form.' . $role->id()] = [
  230. 'title' => t('Edit permissions'),
  231. 'route_name' => 'entity.user_role.edit_permissions_form',
  232. 'menu_name' => 'admin',
  233. 'parent' => 'entity.user_role.edit_form.' . $role->id(),
  234. 'route_parameters' => ['user_role' => $role->id()],
  235. ];
  236. $links['entity.user_role.delete_form.' . $role->id()] = [
  237. 'title' => t('Delete'),
  238. 'route_name' => 'entity.user_role.delete_form',
  239. 'menu_name' => 'admin',
  240. 'parent' => 'entity.user_role.edit_form.' . $role->id(),
  241. 'route_parameters' => ['user_role' => $role->id()],
  242. ];
  243. if ($moduleHandler->moduleExists('devel')) {
  244. $links['entity.user_role.devel_load.' . $role->id()] = [
  245. 'title' => t('Devel'),
  246. 'route_name' => 'entity.user_role.devel_load',
  247. 'menu_name' => 'admin',
  248. 'parent' => 'entity.user_role.edit_form.' . $role->id(),
  249. 'route_parameters' => ['user_role' => $role->id()],
  250. ];
  251. }
  252. }
  253. if ($moduleHandler->moduleExists('node')) {
  254. $links['admin_toolbar_tools.add_content'] = $links['node.add_page'];
  255. $links['admin_toolbar_tools.add_content']['parent'] = 'system.admin_content';
  256. $links['node.type_add'] = [
  257. 'title' => t('Add content type'),
  258. 'route_name' => 'node.type_add',
  259. 'menu_name' => 'admin',
  260. 'parent' => 'entity.node_type.collection',
  261. 'weight' => -5,
  262. ];
  263. // Add node links for each content type.
  264. foreach (\Drupal::entityTypeManager()->getStorage('node_type')->loadMultiple() as $type) {
  265. $links['node.add.' . $type->id()] = [
  266. 'title' => t($type->label()),
  267. 'route_name' => 'node.add',
  268. 'parent' => 'admin_toolbar_tools.add_content',
  269. 'route_parameters' => ['node_type' => $type->id()],
  270. ];
  271. }
  272. }
  273. if ($moduleHandler->moduleExists('field_ui')) {
  274. $links['field_ui.entity_form_mode_add'] = [
  275. 'title' => t('Add new form mode'),
  276. 'route_name' => 'field_ui.entity_form_mode_add',
  277. 'menu_name' => 'admin',
  278. 'parent' => 'entity.entity_form_mode.collection',
  279. ];
  280. $links['field_ui.entity_view_mode_add'] = [
  281. 'title' => t('Add new view mode'),
  282. 'route_name' => 'field_ui.entity_view_mode_add',
  283. 'menu_name' => 'admin',
  284. 'parent' => 'entity.entity_view_mode.collection',
  285. ];
  286. }
  287. if ($moduleHandler->moduleExists('taxonomy')) {
  288. $links['entity.taxonomy_vocabulary.add_form'] = [
  289. 'title' => t('Add vocabulary'),
  290. 'route_name' => 'entity.taxonomy_vocabulary.add_form',
  291. 'menu_name' => 'admin',
  292. 'parent' => 'entity.taxonomy_vocabulary.collection',
  293. 'weight' => -5,
  294. ];
  295. }
  296. if ($moduleHandler->moduleExists('menu_ui')) {
  297. $links['entity.menu.add_form'] = [
  298. 'title' => t('Add menu'),
  299. 'route_name' => 'entity.menu.add_form',
  300. 'menu_name' => 'admin',
  301. 'parent' => 'entity.menu.collection',
  302. 'weight' => -50,
  303. ];
  304. // Adds links to /admin/structure/menu.
  305. foreach (menu_ui_get_menus() as $machine_name => $label) {
  306. $links['entity.menu.edit_form.' . $machine_name] = [
  307. 'title' => t($label),
  308. 'route_name' => 'entity.menu.edit_form',
  309. 'menu_name' => 'admin',
  310. 'parent' => 'entity.menu.collection',
  311. 'route_parameters' => ['menu' => $machine_name],
  312. ];
  313. $links['entity.menu.delete_form.' . $machine_name] = [
  314. 'title' => t('Delete'),
  315. 'route_name' => 'entity.menu.delete_form',
  316. 'menu_name' => 'admin',
  317. 'parent' => 'entity.menu.edit_form.' . $machine_name,
  318. 'route_parameters' => ['menu' => $machine_name],
  319. ];
  320. if ($moduleHandler->moduleExists('devel')) {
  321. $links['entity.menu.devel_load.' . $machine_name] = [
  322. 'title' => t('Devel'),
  323. 'route_name' => 'entity.menu.devel_load',
  324. 'menu_name' => 'admin',
  325. 'parent' => 'entity.menu.edit_form.' . $machine_name,
  326. 'route_parameters' => ['menu' => $machine_name],
  327. ];
  328. }
  329. $links['entity.menu.add_link_form.' . $machine_name] = [
  330. 'title' => t('Add link'),
  331. 'route_name' => 'entity.menu.add_link_form',
  332. 'menu_name' => 'admin',
  333. 'parent' => 'entity.menu.edit_form.' . $machine_name,
  334. 'route_parameters' => ['menu' => $machine_name],
  335. ];
  336. }
  337. }
  338. // If module block_content is enabled.
  339. if ($moduleHandler->moduleExists('block_content')) {
  340. $links['block_content.add_page'] = [
  341. 'title' => t('Add custom block'),
  342. 'route_name' => 'block_content.add_page',
  343. 'menu_name' => 'admin',
  344. 'parent' => 'block.admin_display',
  345. 'weight' => -100,
  346. ];
  347. $links['entity.block_content.collection'] = [
  348. 'title' => t('Custom block library'),
  349. 'route_name' => 'entity.block_content.collection',
  350. 'menu_name' => 'admin',
  351. 'parent' => 'block.admin_display',
  352. ];
  353. $links['entity.block_content_type.collection'] = [
  354. 'title' => t('Types'),
  355. 'route_name' => 'entity.block_content_type.collection',
  356. 'menu_name' => 'admin',
  357. 'parent' => 'block.admin_display',
  358. ];
  359. }
  360. // If module Contact is enabled.
  361. if ($moduleHandler->moduleExists('contact')) {
  362. $links['contact.form_add'] = [
  363. 'title' => t('Add contact form'),
  364. 'route_name' => 'contact.form_add',
  365. 'menu_name' => 'admin',
  366. 'parent' => 'entity.contact_form.collection',
  367. 'weight' => -5,
  368. ];
  369. }
  370. // If module Update Manager is enabled.
  371. if ($moduleHandler->moduleExists('update')) {
  372. $links['update.module_update'] = [
  373. 'title' => t('Update'),
  374. 'route_name' => 'update.module_update',
  375. 'menu_name' => 'admin',
  376. 'parent' => 'system.modules_list',
  377. ];
  378. $links['update.module_install'] = [
  379. 'title' => t('Install new module'),
  380. 'route_name' => 'update.module_install',
  381. 'menu_name' => 'admin',
  382. 'parent' => 'system.modules_list',
  383. ];
  384. }
  385. // If module Devel is enabled.
  386. if ($moduleHandler->moduleExists('devel')) {
  387. $links['admin_development'] = [
  388. 'title' => t('Development'),
  389. 'route_name' => 'system.admin_config_development',
  390. 'menu_name' => 'admin',
  391. 'parent' => 'admin_toolbar_tools.help',
  392. 'weight' => '-8',
  393. ];
  394. $links['admin_toolbar_tools.devel.admin_settings'] = [
  395. 'title' => t('Devel settings'),
  396. 'route_name' => 'devel.admin_settings',
  397. 'menu_name' => 'admin',
  398. 'parent' => 'admin_development',
  399. 'weight' => '-1',
  400. ];
  401. if ($moduleHandler->moduleExists('webprofiler')) {
  402. $links['admin_menu_tools.devel.webprofiler'] = [
  403. 'title' => t('Web Profiler settings'),
  404. 'route_name' => 'webprofiler.settings',
  405. 'menu_name' => 'admin',
  406. 'parent' => 'admin_development',
  407. ];
  408. }
  409. $links['admin_toolbar_tools.devel.configs_list'] = [
  410. 'title' => t('Config editor'),
  411. 'route_name' => 'devel.configs_list',
  412. 'menu_name' => 'admin',
  413. 'parent' => 'admin_development',
  414. ];
  415. $links['admin_toolbar_tools.devel.reinstall'] = [
  416. 'title' => t('Reinstall modules'),
  417. 'route_name' => 'devel.reinstall',
  418. 'parent' => 'admin_development',
  419. ];
  420. $links['admin_toolbar_tools.devel.menu_rebuild'] = [
  421. 'title' => t('Rebuild menu'),
  422. 'route_name' => 'devel.menu_rebuild',
  423. 'menu_name' => 'admin',
  424. 'parent' => 'admin_development',
  425. ];
  426. $links['admin_toolbar_tools.devel.state_system_page'] = [
  427. 'title' => t('State editor'),
  428. 'route_name' => 'devel.state_system_page',
  429. 'menu_name' => 'admin',
  430. 'parent' => 'admin_development',
  431. ];
  432. $links['admin_toolbar_tools.devel.theme_registry'] = [
  433. 'title' => t('Theme registry'),
  434. 'route_name' => 'devel.theme_registry',
  435. 'menu_name' => 'admin',
  436. 'parent' => 'admin_development',
  437. ];
  438. $links['admin_toolbar_tools.devel.entity_info_page'] = [
  439. 'title' => t('Entity Info'),
  440. 'route_name' => 'devel.entity_info_page',
  441. 'menu_name' => 'admin',
  442. 'parent' => 'admin_development',
  443. ];
  444. $links['admin_toolbar_tools.devel.execute_php'] = [
  445. 'title' => t('Execute PHP Code'),
  446. 'route_name' => 'devel.execute_php',
  447. 'menu_name' => 'admin',
  448. 'parent' => 'admin_development',
  449. ];
  450. $links['admin_toolbar_tools.devel.session'] = [
  451. 'title' => t('Session viewer'),
  452. 'route_name' => 'devel.session',
  453. 'menu_name' => 'admin',
  454. 'parent' => 'admin_development',
  455. ];
  456. $links['admin_toolbar_tools.devel.elements_page'] = [
  457. 'title' => t('Form API field types'),
  458. 'route_name' => 'devel.elements_page',
  459. 'menu_name' => 'admin',
  460. 'parent' => 'admin_development',
  461. ];
  462. }
  463. // If module Views Ui enabled.
  464. if ($moduleHandler->moduleExists('views_ui')) {
  465. $links['views_ui.add'] = [
  466. 'title' => t('Add new view'),
  467. 'route_name' => 'views_ui.add',
  468. 'menu_name' => 'admin',
  469. 'parent' => 'entity.view.collection',
  470. 'weight' => -5,
  471. ];
  472. }
  473. $links['system.theme_settings_'] = [
  474. 'title' => t('Settings'),
  475. 'route_name' => 'system.theme_settings',
  476. 'menu_name' => 'admin',
  477. 'parent' => 'system.themes_page',
  478. ];
  479. if ($moduleHandler->moduleExists('webprofiler')) {
  480. $links['admin_menu_tools.devel.webprofiler'] = [
  481. 'title' => t('Webprofiler settings'),
  482. 'route_name' => 'webprofiler.settings',
  483. 'menu_name' => 'admin',
  484. 'parent' => 'admin_development',
  485. ];
  486. }
  487. if ($moduleHandler->moduleExists('update')) {
  488. $links['update.theme_install_'] = [
  489. 'title' => t('Install new theme'),
  490. 'route_name' => 'update.theme_install',
  491. 'menu_name' => 'admin',
  492. 'parent' => 'system.themes_page',
  493. ];
  494. $links['update.theme_update_'] = [
  495. 'title' => t('Update'),
  496. 'route_name' => 'update.theme_update',
  497. 'menu_name' => 'admin',
  498. 'parent' => 'system.themes_page',
  499. ];
  500. // Lists installed themes.
  501. $installed_themes = admin_toolbar_tools_installed_themes();
  502. foreach ($installed_themes as $key_theme => $label_theme) {
  503. $links['system.theme_settings_theme.' . $key_theme] = [
  504. 'title' => t($label_theme),
  505. 'route_name' => 'system.theme_settings_theme',
  506. 'menu_name' => 'admin',
  507. 'parent' => 'system.theme_settings_',
  508. 'route_parameters' => [
  509. 'theme' => $key_theme,
  510. ],
  511. ];
  512. }
  513. }
  514. // Add menu links for local tasks that sit below any admin menu link routes.
  515. $manager = \Drupal::service('plugin.manager.menu.local_task');
  516. foreach ($links as $key => $link) {
  517. // Ignore menu links that have no route name.
  518. if (empty($link['route_name'])) {
  519. continue;
  520. }
  521. // Ignore links that are not in the admin menu, include parents in the
  522. // check as they inherit the menu name from the parent if not set.
  523. $link_to_check = $link;
  524. do {
  525. // @todo Use configuration for admin menu name once added in
  526. // https://www.drupal.org/node/1869638.
  527. if (isset($link_to_check['menu_name'])) {
  528. // If the link belongs to the admin menu, then skip this loop and
  529. // look for local tasks.
  530. if ($link_to_check['menu_name'] === 'admin') {
  531. break;
  532. }
  533. else {
  534. // If it is explicitly not in the admin menu, skip.
  535. continue 2;
  536. }
  537. }
  538. if (!empty($link_to_check['parent']) && isset($links[$link_to_check['parent']])) {
  539. $link_to_check = $links[$link_to_check['parent']];
  540. }
  541. else {
  542. // No parent and we found no menu_name, skip.
  543. continue 2;
  544. }
  545. } while ($link_to_check);
  546. $route = $link['route_name'];
  547. $route_local_tasks = $manager->getLocalTasksForRoute($route);
  548. if (empty($route_local_tasks[0])) {
  549. continue;
  550. }
  551. foreach ($route_local_tasks[0] as $task_plugin) {
  552. $definition = $task_plugin->getPluginDefinition();
  553. $local_route = $definition['route_name'];
  554. if (empty($local_route)) {
  555. continue;
  556. }
  557. $exists = FALSE;
  558. $param = FALSE;
  559. if (isset($link['route_parameters'])) {
  560. $param = $link['route_parameters'];
  561. }
  562. // Check if local task has an already registered link in toolbar.
  563. foreach ($links as $link2) {
  564. // Route_name that could be a 'class'.
  565. if (!(isset($link2['route_name']) && ($link2['route_name'] == $local_route))) {
  566. continue;
  567. }
  568. // If route are equal, the parameters could be different.
  569. // @todo: check if this is true.
  570. if ($param) {
  571. if (isset($link2['route_parameters']) && ($param == $link2['route_parameters'])) {
  572. $exists = TRUE;
  573. break;
  574. }
  575. }
  576. else {
  577. $exists = TRUE;
  578. break;
  579. }
  580. }
  581. $parent = FALSE;
  582. if ($exists) {
  583. continue;
  584. }
  585. $title = $definition['title'];
  586. $title = $param ? $title . ' ' . $link['title'] : $title;
  587. // The following code is to choose the parent of the added link.
  588. // If the parent of the current tab also has the tab as local task
  589. // for example: "Content" has "File" as task" and and
  590. // "Content>Comment" also has "File" as a tab then we decide to
  591. // set this one as parent of the added link if not the current tab
  592. // will be the parent.
  593. if (!empty($link['parent']) && isset($links[$link['parent']])) {
  594. $parentlink = $links[$link['parent']];
  595. $parentlink_route = $parentlink['route_name'];
  596. $parent_primary = $manager->getLocalTasks($parentlink_route, 0);
  597. foreach ($parent_primary['tabs'] as $parent_tab) {
  598. if ($parent_tab['#link']['url']) {
  599. $parent_route = $parent_tab['#link']['url']->getRouteName();
  600. if ($parent_route == $local_route) {
  601. $parent = $link['parent'];
  602. break;
  603. }
  604. }
  605. }
  606. }
  607. $parent = $parent ? $parent : $key;
  608. // Make sure the key of the route is unique.
  609. $links[$key . '_' . $local_route] = [
  610. 'title' => $title,
  611. 'route_name' => $local_route,
  612. 'menu_name' => 'admin',
  613. 'parent' => $parent,
  614. 'att' => TRUE,
  615. ];
  616. if ($param) {
  617. $links[$key . '_' . $local_route]['route_parameters'] = $param;
  618. }
  619. }
  620. }
  621. }
  622. /**
  623. * Return installed themes.
  624. *
  625. * @return array
  626. * An array of friendly theme names, keyed by the machine name.
  627. */
  628. function admin_toolbar_tools_installed_themes() {
  629. $themeHandler = \Drupal::service('theme_handler');
  630. $all_themes = $themeHandler->listInfo();
  631. $themes_installed = [];
  632. foreach ($all_themes as $key_theme => $theme) {
  633. if ($themeHandler->hasUi($key_theme)) {
  634. $themes_installed[$key_theme] = $themeHandler->getName($key_theme);
  635. }
  636. }
  637. return $themes_installed;
  638. }
  639. /**
  640. * Get all links related to entity.
  641. *
  642. * @param string $entity_type_id
  643. * The machine name for the entity type.
  644. *
  645. * @return array
  646. * The links for the entity type.
  647. */
  648. function admin_toolbar_tools_get_links($entity_type_id) {
  649. $entity = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
  650. // Get all links related to entity.
  651. $links = $entity->getLinkTemplates();
  652. return $links;
  653. }