tour.module 3.67 KB
Newer Older
1 2 3 4 5 6
<?php

/**
 * @file
 * Main functions of the module.
 */
7 8

use Drupal\Core\Routing\RouteMatchInterface;
9
use Drupal\tour\Entity\Tour;
10

11 12 13
/**
 * Implements hook_help().
 */
14
function tour_help($route_name, RouteMatchInterface $route_match) {
15 16
  switch ($route_name) {
    case 'help.page.tour':
17 18
      $output = '';
      $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 21 22 23 24
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Viewing tours') . '</dt>';
      $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>';
      $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 27 28 29 30
      $output .= '</dl>';
      return $output;
  }
}

31 32 33 34
/**
 * Implements hook_toolbar().
 */
function tour_toolbar() {
35 36 37 38 39 40 41 42 43
  $items = [];
  $items['tour'] = [
    '#cache' => [
      'contexts' => [
        'user.permissions',
      ],
    ],
  ];

44
  if (!\Drupal::currentUser()->hasPermission('access tour')) {
45
    return $items;
46 47
  }

48
  $items['tour'] += [
49
    '#type' => 'toolbar_item',
50
    'tab' => [
51 52 53
      '#type' => 'html_tag',
      '#tag' => 'button',
      '#value' => t('Tour'),
54 55
      '#attributes' => [
        'class' => ['toolbar-icon', 'toolbar-icon-help'],
56
        'aria-pressed' => 'false',
57
        'type' => 'button',
58 59 60 61
      ],
    ],
    '#wrapper_attributes' => [
      'class' => ['tour-toolbar-tab', 'hidden'],
62
      'id' => 'toolbar-tab-tour',
63 64 65
    ],
    '#attached' => [
      'library' => [
66
        'tour/tour',
67 68 69
      ],
    ],
  ];
70

71
  return $items;
72 73 74
}

/**
75
 * Implements hook_page_bottom().
76
 */
77
function tour_page_bottom(array &$page_bottom) {
78
  if (!\Drupal::currentUser()->hasPermission('access tour')) {
79 80 81
    return;
  }

82
  // Load all of the items and match on route name.
83 84
  $route_match = \Drupal::routeMatch();
  $route_name = $route_match->getRouteName();
85 86 87 88

  $results = \Drupal::entityQuery('tour')
    ->condition('routes.*.route_name', $route_name)
    ->execute();
89
  if (!empty($results) && $tours = Tour::loadMultiple(array_keys($results))) {
90 91
    foreach ($tours as $id => $tour) {
      // Match on params.
92
      if (!$tour->hasMatchingRoute($route_name, $route_match->getRawParameters()->all())) {
93 94 95 96
        unset($tours[$id]);
      }
    }
    if (!empty($tours)) {
97
      $page_bottom['tour'] = entity_view_multiple($tours, 'full');
98 99 100 101 102
    }
  }
}

/**
103
 * Implements hook_ENTITY_TYPE_insert() for tour entities.
104 105
 */
function tour_tour_insert($entity) {
106
  \Drupal::service('plugin.manager.tour.tip')->clearCachedDefinitions();
107 108 109
}

/**
110
 * Implements hook_ENTITY_TYPE_update() for tour entities.
111 112
 */
function tour_tour_update($entity) {
113
  \Drupal::service('plugin.manager.tour.tip')->clearCachedDefinitions();
114
}