tour.module 3.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
<?php

/**
 * @file
 * Main functions of the module.
 */
use Drupal\Core\Cache\CacheBackendInterface;

/**
 * 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',
    'version' => VERSION,
    'js' => array(
      // Add the JavaScript, with a group and weight such that it will run
      // before modules/overlay/overlay-parent.js.
      $path . '/js/tour.js' => array('group' => JS_LIBRARY, 'weight' => -1),
    ),
    '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',
    'version' => VERSION,
    'css' => array(
      $path . '/css/tour.css' => array('media' => 'screen'),
    )
  );

  $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() {
  if (!user_access('access tour')) {
    return;
  }

  $tab['tour'] = array(
    '#type' => 'toolbar_item',
    'tab' => array(
      '#type' => 'html_tag',
      '#tag' => 'button',
      '#value' => t('Tour'),
      '#attributes' => array(
        'class' => array('icon', 'icon-help'),
        'role' => 'button',
        'aria-pressed' => 'false',
      ),
    ),
    '#wrapper_attributes' => array(
      'class' => array('tour-toolbar-tab', 'element-hidden'),
      'id' => 'toolbar-tab-tour',
    ),
    '#attached' => array(
      'library' => array(
        array('tour', 'tour'),
      ),
    ),
  );

  return $tab;
}

/**
 * Implements hook_preprocess_HOOK() for page.tpl.php.
 */
function tour_preprocess_page(&$variables) {
109 110 111 112 113
  if (!user_access('access tour')) {
    return;
  }

  // @todo replace this with http://drupal.org/node/1918768 once it is committed.
114 115 116 117 118 119 120 121 122 123
  $path = current_path();
  $langcode = language(LANGUAGE_TYPE_CONTENT)->langcode;
  $tour_items = array();
  // Load all of the items and match on path.
  $tour_ids = entity_query('tour')
    ->condition('langcode', $langcode)
    ->execute();
  $tours = entity_load_multiple('tour', $tour_ids);

  $path_alias = drupal_strtolower(drupal_container()->get('path.alias_manager')->getPathAlias($path));
124
  foreach ($tours as $tour_id => $tour) {
125 126 127
    // @todo replace this with an entity_query() that does path matching when
    // http://drupal.org/node/1918768 lands.
    $pages = implode("\n", $tour->getPaths());
128 129
    if (!drupal_match_path($path_alias, $pages) && (($path == $path_alias) || drupal_match_path($path, $pages))) {
      unset($tours[$tour_id]);
130 131 132
    }
  }

133 134
  if ($tours) {
    $variables['page']['help']['tour'] = entity_view_multiple($tours, 'full');
135 136 137 138
  }
}

/**
139
 * Implements hook_tour_insert().
140 141
 */
function tour_tour_insert($entity) {
142
  drupal_container()->get('plugin.manager.tour.tip')->clearCachedDefinitions();
143 144 145
}

/**
146
 * Implements hook_tour_update().
147 148
 */
function tour_tour_update($entity) {
149
  drupal_container()->get('plugin.manager.tour.tip')->clearCachedDefinitions();
150
}