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

/**
 * @file
 * Main functions of the module.
 */
use Drupal\Core\Cache\CacheBackendInterface;
8
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/**
 * Implements hook_help().
 */
function tour_help($path, $arg) {
  switch ($path) {
    case 'admin/help#tour':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $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 <a href='!tour'>the online documentation for the Tour module</a>.", array('!tour' => 'https://drupal.org/documentation/modules/tour')) . '</p>';
      $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>';
      $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>.", array('!doc_url' => 'https://drupal.org/developing/api/tour', '!tour_ui' => 'https://drupal.org/project/tour_ui')) . '</dd>';
      $output .= '</dl>';
      return $output;
  }
}

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/**
 * Implements hook_permission().
 */
function tour_permission() {
  return array(
    'access tour' => array(
      'title' => t('Access tour'),
      'description' => t('View tour tips.'),
    ),
  );
}

/**
 * Implements hook_toolbar().
 */
function tour_toolbar() {
46
  if (!\Drupal::currentUser()->hasPermission('access tour')) {
47 48 49 50 51 52 53 54 55 56
    return;
  }

  $tab['tour'] = array(
    '#type' => 'toolbar_item',
    'tab' => array(
      '#type' => 'html_tag',
      '#tag' => 'button',
      '#value' => t('Tour'),
      '#attributes' => array(
57
        'class' => array('toolbar-icon', 'toolbar-icon-help'),
58 59 60 61 62
        'role' => 'button',
        'aria-pressed' => 'false',
      ),
    ),
    '#wrapper_attributes' => array(
63
      'class' => array('tour-toolbar-tab', 'hidden'),
64 65 66 67
      'id' => 'toolbar-tab-tour',
    ),
    '#attached' => array(
      'library' => array(
68
        'tour/tour',
69 70 71 72 73 74 75 76
      ),
    ),
  );

  return $tab;
}

/**
77
 * Implements hook_preprocess_HOOK() for page templates.
78 79
 */
function tour_preprocess_page(&$variables) {
80
  if (!\Drupal::currentUser()->hasPermission('access tour')) {
81 82 83
    return;
  }

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  // Load all of the items and match on route name.
  $request = \Drupal::request();
  $route_name = $request->attributes->get(RouteObjectInterface::ROUTE_NAME);

  $results = \Drupal::entityQuery('tour')
    ->condition('routes.*.route_name', $route_name)
    ->execute();
  if (!empty($results) && $tours = entity_load_multiple('tour', array_keys($results))) {
    foreach ($tours as $id => $tour) {
      // Match on params.
      if (!$tour->hasMatchingRoute($route_name, $request->attributes->get('_raw_variables')->all())) {
        unset($tours[$id]);
      }
    }
    if (!empty($tours)) {
      $variables['page']['help']['tour'] = entity_view_multiple($tours, 'full');
100 101 102 103 104
    }
  }
}

/**
105
 * Implements hook_tour_insert().
106 107
 */
function tour_tour_insert($entity) {
108
  \Drupal::service('plugin.manager.tour.tip')->clearCachedDefinitions();
109 110 111
}

/**
112
 * Implements hook_tour_update().
113 114
 */
function tour_tour_update($entity) {
115
  \Drupal::service('plugin.manager.tour.tip')->clearCachedDefinitions();
116
}