annotate core/modules/tour/tour.module @ 19:fa3358dc1485 tip

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