1de83992bbf4db285dd9171bbc7ca97bf7586814
[yaffs-website] / web / core / modules / tour / tour.module
1 <?php
2
3 /**
4  * @file
5  * Main functions of the module.
6  */
7
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Drupal\tour\Entity\Tour;
10
11 /**
12  * Implements hook_help().
13  */
14 function tour_help($route_name, RouteMatchInterface $route_match) {
15   switch ($route_name) {
16     case 'help.page.tour':
17       $output = '';
18       $output .= '<h3>' . t('About') . '</h3>';
19       $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>';
20       $output .= '<h3>' . t('Uses') . '</h3>';
21       $output .= '<dl>';
22       $output .= '<dt>' . t('Viewing tours') . '</dt>';
23       $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>';
24       $output .= '<dt>' . t('Creating tours') . '</dt>';
25       $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>';
26       $output .= '</dl>';
27       return $output;
28   }
29 }
30
31 /**
32  * Implements hook_toolbar().
33  */
34 function tour_toolbar() {
35   $items = [];
36   $items['tour'] = [
37     '#cache' => [
38       'contexts' => [
39         'user.permissions',
40       ],
41     ],
42   ];
43
44   if (!\Drupal::currentUser()->hasPermission('access tour')) {
45     return $items;
46   }
47
48   $items['tour'] += [
49     '#type' => 'toolbar_item',
50     'tab' => [
51       '#type' => 'html_tag',
52       '#tag' => 'button',
53       '#value' => t('Tour'),
54       '#attributes' => [
55         'class' => ['toolbar-icon', 'toolbar-icon-help'],
56         'aria-pressed' => 'false',
57       ],
58     ],
59     '#wrapper_attributes' => [
60       'class' => ['tour-toolbar-tab', 'hidden'],
61       'id' => 'toolbar-tab-tour',
62     ],
63     '#attached' => [
64       'library' => [
65         'tour/tour',
66       ],
67     ],
68   ];
69
70   return $items;
71 }
72
73 /**
74  * Implements hook_page_bottom().
75  */
76 function tour_page_bottom(array &$page_bottom) {
77   if (!\Drupal::currentUser()->hasPermission('access tour')) {
78     return;
79   }
80
81   // Load all of the items and match on route name.
82   $route_match = \Drupal::routeMatch();
83   $route_name = $route_match->getRouteName();
84
85   $results = \Drupal::entityQuery('tour')
86     ->condition('routes.*.route_name', $route_name)
87     ->execute();
88   if (!empty($results) && $tours = Tour::loadMultiple(array_keys($results))) {
89     foreach ($tours as $id => $tour) {
90       // Match on params.
91       if (!$tour->hasMatchingRoute($route_name, $route_match->getRawParameters()->all())) {
92         unset($tours[$id]);
93       }
94     }
95     if (!empty($tours)) {
96       $page_bottom['tour'] = entity_view_multiple($tours, 'full');
97     }
98   }
99 }
100
101 /**
102  * Implements hook_ENTITY_TYPE_insert() for tour entities.
103  */
104 function tour_tour_insert($entity) {
105   \Drupal::service('plugin.manager.tour.tip')->clearCachedDefinitions();
106 }
107
108 /**
109  * Implements hook_ENTITY_TYPE_update() for tour entities.
110  */
111 function tour_tour_update($entity) {
112   \Drupal::service('plugin.manager.tour.tip')->clearCachedDefinitions();
113 }