tour.module 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. 'type' => 'button',
  52. ],
  53. ],
  54. '#wrapper_attributes' => [
  55. 'class' => ['tour-toolbar-tab', 'hidden'],
  56. 'id' => 'toolbar-tab-tour',
  57. ],
  58. '#attached' => [
  59. 'library' => [
  60. 'tour/tour',
  61. ],
  62. ],
  63. ];
  64. return $items;
  65. }
  66. /**
  67. * Implements hook_page_bottom().
  68. */
  69. function tour_page_bottom(array &$page_bottom) {
  70. if (!\Drupal::currentUser()->hasPermission('access tour')) {
  71. return;
  72. }
  73. // Load all of the items and match on route name.
  74. $route_match = \Drupal::routeMatch();
  75. $route_name = $route_match->getRouteName();
  76. $results = \Drupal::entityQuery('tour')
  77. ->condition('routes.*.route_name', $route_name)
  78. ->execute();
  79. if (!empty($results) && $tours = Tour::loadMultiple(array_keys($results))) {
  80. foreach ($tours as $id => $tour) {
  81. // Match on params.
  82. if (!$tour->hasMatchingRoute($route_name, $route_match->getRawParameters()->all())) {
  83. unset($tours[$id]);
  84. }
  85. }
  86. if (!empty($tours)) {
  87. $page_bottom['tour'] = entity_view_multiple($tours, 'full');
  88. }
  89. }
  90. }
  91. /**
  92. * Implements hook_ENTITY_TYPE_insert() for tour entities.
  93. */
  94. function tour_tour_insert($entity) {
  95. \Drupal::service('plugin.manager.tour.tip')->clearCachedDefinitions();
  96. }
  97. /**
  98. * Implements hook_ENTITY_TYPE_update() for tour entities.
  99. */
  100. function tour_tour_update($entity) {
  101. \Drupal::service('plugin.manager.tour.tip')->clearCachedDefinitions();
  102. }