tour.module 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @file
  4. * Main functions of the module.
  5. */
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. use Drupal\tour\Entity\Tour;
  8. /**
  9. * Implements hook_help().
  10. */
  11. function tour_help($route_name, RouteMatchInterface $route_match) {
  12. switch ($route_name) {
  13. case 'help.page.tour':
  14. $output = '';
  15. $output .= '<h3>' . t('About') . '</h3>';
  16. $output .= '<p>' . t("The Tour module provides users with guided tours of the site interface. Each tour consists of several tips that highlight elements of the user interface, guide the user through a workflow, or explain key concepts of the website. For more information, see the <a href=':tour'>online documentation for the Tour module</a>.", [':tour' => 'https://www.drupal.org/documentation/modules/tour']) . '</p>';
  17. $output .= '<h3>' . t('Uses') . '</h3>';
  18. $output .= '<dl>';
  19. $output .= '<dt>' . t('Viewing tours') . '</dt>';
  20. $output .= '<dd>' . t("If a tour is available on a page, a <em>Tour</em> button will be visible in the toolbar. If you click this button the first tip of the tour will appear. The tour continues after clicking the <em>Next</em> button in the tip. To see a tour users must have the permission <em>Access tour</em> and JavaScript must be enabled in the browser") . '</dd>';
  21. $output .= '<dt>' . t('Creating tours') . '</dt>';
  22. $output .= '<dd>' . t("Tours can be written as YAML-documents with a text editor, or using the contributed <a href=':tour_ui'>Tour UI</a> module. For more information, see <a href=':doc_url'>the online documentation for writing tours</a>.", [':doc_url' => 'https://www.drupal.org/developing/api/tour', ':tour_ui' => 'https://www.drupal.org/project/tour_ui']) . '</dd>';
  23. $output .= '</dl>';
  24. return $output;
  25. }
  26. }
  27. /**
  28. * Implements hook_toolbar().
  29. */
  30. function tour_toolbar() {
  31. $items = [];
  32. $items['tour'] = [
  33. '#cache' => [
  34. 'contexts' => [
  35. 'user.permissions',
  36. ],
  37. ],
  38. ];
  39. if (!\Drupal::currentUser()->hasPermission('access tour')) {
  40. return $items;
  41. }
  42. $items['tour'] += [
  43. '#type' => 'toolbar_item',
  44. 'tab' => [
  45. '#type' => 'html_tag',
  46. '#tag' => 'button',
  47. '#value' => t('Tour'),
  48. '#attributes' => [
  49. 'class' => ['toolbar-icon', 'toolbar-icon-help'],
  50. 'aria-pressed' => 'false',
  51. ],
  52. ],
  53. '#wrapper_attributes' => [
  54. 'class' => ['tour-toolbar-tab', 'hidden'],
  55. 'id' => 'toolbar-tab-tour',
  56. ],
  57. '#attached' => [
  58. 'library' => [
  59. 'tour/tour',
  60. ],
  61. ],
  62. ];
  63. return $items;
  64. }
  65. /**
  66. * Implements hook_page_bottom().
  67. */
  68. function tour_page_bottom(array &$page_bottom) {
  69. if (!\Drupal::currentUser()->hasPermission('access tour')) {
  70. return;
  71. }
  72. // Load all of the items and match on route name.
  73. $route_match = \Drupal::routeMatch();
  74. $route_name = $route_match->getRouteName();
  75. $results = \Drupal::entityQuery('tour')
  76. ->condition('routes.*.route_name', $route_name)
  77. ->execute();
  78. if (!empty($results) && $tours = Tour::loadMultiple(array_keys($results))) {
  79. foreach ($tours as $id => $tour) {
  80. // Match on params.
  81. if (!$tour->hasMatchingRoute($route_name, $route_match->getRawParameters()->all())) {
  82. unset($tours[$id]);
  83. }
  84. }
  85. if (!empty($tours)) {
  86. $page_bottom['tour'] = entity_view_multiple($tours, 'full');
  87. }
  88. }
  89. }
  90. /**
  91. * Implements hook_ENTITY_TYPE_insert() for tour entities.
  92. */
  93. function tour_tour_insert($entity) {
  94. \Drupal::service('plugin.manager.tour.tip')->clearCachedDefinitions();
  95. }
  96. /**
  97. * Implements hook_ENTITY_TYPE_update() for tour entities.
  98. */
  99. function tour_tour_update($entity) {
  100. \Drupal::service('plugin.manager.tour.tip')->clearCachedDefinitions();
  101. }