examples.module 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @file
  4. * Module file for Examples for Developers.
  5. *
  6. * This file serves as a stub file for the many Examples modules in the
  7. * @link http://drupal.org/project/examples Examples for Developers Project @endlink
  8. * which you can download and experiment with.
  9. *
  10. * One might say that examples.module is an example of documentation. However,
  11. * note that the example submodules define many doxygen groups, which may or
  12. * may not be a good strategy for other modules.
  13. */
  14. use Drupal\Core\Url;
  15. /**
  16. * @defgroup examples Examples
  17. * @{
  18. * Well-documented API examples for a broad range of Drupal core functionality.
  19. *
  20. * Developers can learn how to use a particular API quickly by experimenting
  21. * with the examples, and adapt them for their own use.
  22. *
  23. * Download the Examples for Developers Project (and participate with
  24. * submissions, bug reports, patches, and documentation) at
  25. * http://drupal.org/project/examples
  26. */
  27. /**
  28. * Implements hook_toolbar().
  29. */
  30. function examples_toolbar() {
  31. // First, build an array of all example modules and their routes.
  32. // We resort to this hard-coded way so as not to muck up each example.
  33. $examples = [
  34. 'ajax_example' => 'ajax_example.description',
  35. 'batch_example' => 'batch_example.form',
  36. 'block_example' => 'block_example.description',
  37. 'cache_example' => 'cache_example.description',
  38. 'config_entity_example' => 'entity.robot.list',
  39. 'content_entity_example' => 'entity.content_entity_example_contact.collection',
  40. 'cron_example' => 'cron_example',
  41. 'dbtng_example' => 'dbtng_example',
  42. 'email_example' => 'email_example.description',
  43. 'events_example' => 'events_example.description',
  44. 'form_api_example' => 'form_api_example.description',
  45. 'field_example' => 'field_example.description',
  46. 'field_permission_example' => 'field_permission_example.description',
  47. 'file_example' => 'file_example.fileapi',
  48. 'hooks_example' => 'hooks_example.description',
  49. 'js_example' => 'js_example.info',
  50. 'node_type_example' => 'config_node_type_example.description',
  51. 'page_example' => 'page_example_description',
  52. 'pager_example' => 'pager_example.page',
  53. 'phpunit_example' => 'phpunit_example_description',
  54. 'plugin_type_example' => 'plugin_type_example.description',
  55. 'simpletest_example' => 'simpletest_example_description',
  56. 'tabledrag_example' => 'tabledrag_example.description',
  57. 'stream_wrapper_example' => 'stream_wrapper_example.description',
  58. 'testing_example' => 'testing_example.description',
  59. 'queue_example' => 'queue_example',
  60. 'tablesort_example' => 'tablesort_example_description',
  61. 'tour_example' => 'tour_example_description',
  62. ];
  63. // Build a list of links for the menu.
  64. $links = [];
  65. foreach ($examples as $module => $route) {
  66. // Get the module info (title, description) from Drupal.
  67. $info = system_get_info('module', $module);
  68. // If there's no info, the example isn't enabled, so don't display it.
  69. if (!empty($info)) {
  70. $links[$module] = [
  71. 'title' => t($info['name']),
  72. 'url' => Url::fromRoute($route),
  73. 'attributes' => [
  74. 'title' => t($info['description']),
  75. ],
  76. ];
  77. }
  78. }
  79. // Add a link to enable all examples.
  80. $links['enable_examples'] = [
  81. 'title' => t('Enable Examples'),
  82. 'url' => Url::fromRoute('system.modules_list'),
  83. 'options' => [
  84. 'title' => t('Enable more examples in on the Extend page.'),
  85. ],
  86. 'fragment' => 'edit-modules-example-modules',
  87. ];
  88. // Create the examples toolbar render array.
  89. $items['examples'] = [
  90. '#type' => 'toolbar_item',
  91. 'tab' => [
  92. '#type' => 'link',
  93. '#title' => t('Examples'),
  94. '#url' => Url::fromRoute('<front>'),
  95. '#attributes' => [
  96. 'title' => t('Developer Examples'),
  97. 'class' => ['toolbar-icon', 'toolbar-icon-examples'],
  98. ],
  99. ],
  100. 'tray' => [
  101. '#heading' => t('Developer Examples'),
  102. 'shortcuts' => [
  103. '#theme' => 'links__toolbar_example',
  104. '#links' => $links,
  105. '#attributes' => [
  106. 'class' => ['toolbar-menu'],
  107. ],
  108. ],
  109. ],
  110. '#weight' => 99,
  111. '#attached' => [
  112. 'library' => [
  113. 'examples/examples.icons',
  114. ],
  115. ],
  116. ];
  117. return $items;
  118. }
  119. /**
  120. * @} End of 'defgroup examples'.
  121. */