tour.module 3.38 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_permission().
 */
function tour_permission() {
  return array(
    'access tour' => array(
      'title' => t('Access tour'),
      'description' => t('View tour tips.'),
    ),
  );
}

/**
 * Implements hook_library_info().
 */
function tour_library_info() {
  $path = drupal_get_path('module', 'tour');

  $libraries['tour'] = array(
    'title' => 'Tour',
30
    'version' => \Drupal::VERSION,
31
    'js' => array(
32
      $path . '/js/tour.js' => array('group' => JS_LIBRARY),
33
34
35
36
37
38
39
40
41
42
43
44
    ),
    'dependencies' => array(
      array('system', 'jquery'),
      array('system', 'drupal'),
      array('system', 'backbone'),
      array('tour', 'jquery.joyride'),
      array('tour', 'tour-styling'),
    ),
  );

  $libraries['tour-styling'] = array(
    'title' => 'Tour',
45
    'version' => \Drupal::VERSION,
46
    'css' => array(
47
      $path . '/css/tour.module.css' => array('media' => 'screen'),
48
    ),
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  );

  $libraries['jquery.joyride'] = array(
    'title' => 'Joyride',
    'website' => 'https://github.com/zurb/joyride',
    'version' => '2.0.3',
    'js' => array(
      $path . '/js/jquery.joyride-2.0.3.js' => array(),
    ),
    'css' => array(
      $path . '/css/joyride-2.0.3.css' => array('media' => 'screen'),
    ),
    'dependencies' => array(
      array('system', 'jquery'),
      array('system', 'jquery.cookie'),
    ),
  );

  return $libraries;
}

/**
 * Implements hook_toolbar().
 */
function tour_toolbar() {
74
  if (!\Drupal::currentUser()->hasPermission('access tour')) {
75
76
77
78
79
80
81
82
83
84
    return;
  }

  $tab['tour'] = array(
    '#type' => 'toolbar_item',
    'tab' => array(
      '#type' => 'html_tag',
      '#tag' => 'button',
      '#value' => t('Tour'),
      '#attributes' => array(
85
        'class' => array('toolbar-icon', 'toolbar-icon-help'),
86
87
88
89
90
        'role' => 'button',
        'aria-pressed' => 'false',
      ),
    ),
    '#wrapper_attributes' => array(
91
      'class' => array('tour-toolbar-tab', 'hidden'),
92
93
94
95
96
97
98
99
100
101
102
103
104
      'id' => 'toolbar-tab-tour',
    ),
    '#attached' => array(
      'library' => array(
        array('tour', 'tour'),
      ),
    ),
  );

  return $tab;
}

/**
105
 * Implements hook_preprocess_HOOK() for page templates.
106
107
 */
function tour_preprocess_page(&$variables) {
108
  if (!\Drupal::currentUser()->hasPermission('access tour')) {
109
110
111
    return;
  }

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  // 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');
128
129
130
131
132
    }
  }
}

/**
133
 * Implements hook_tour_insert().
134
135
 */
function tour_tour_insert($entity) {
136
  \Drupal::service('plugin.manager.tour.tip')->clearCachedDefinitions();
137
138
139
}

/**
140
 * Implements hook_tour_update().
141
142
 */
function tour_tour_update($entity) {
143
  \Drupal::service('plugin.manager.tour.tip')->clearCachedDefinitions();
144
}