2013-02-18 22:06:10 +00:00
< ? php
/**
* @ file
* Main functions of the module .
*/
2014-06-30 03:33:08 +00:00
use Drupal\Core\Routing\RouteMatchInterface ;
2016-05-17 09:45:28 +00:00
use Drupal\tour\Entity\Tour ;
2013-02-18 22:06:10 +00:00
2014-02-13 16:07:59 +00:00
/**
* Implements hook_help () .
*/
2014-06-30 03:33:08 +00:00
function tour_help ( $route_name , RouteMatchInterface $route_match ) {
2014-05-07 02:04:53 +00:00
switch ( $route_name ) {
case 'help.page.tour' :
2014-02-13 16:07:59 +00:00
$output = '' ;
$output .= '<h3>' . t ( 'About' ) . '</h3>' ;
2017-03-04 01:20:24 +00:00
$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>' ;
2014-02-13 16:07:59 +00:00
$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>' ;
2017-03-04 01:20:24 +00:00
$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>' ;
2014-02-13 16:07:59 +00:00
$output .= '</dl>' ;
return $output ;
}
}
2013-02-18 22:06:10 +00:00
/**
* Implements hook_toolbar () .
*/
function tour_toolbar () {
2015-06-01 16:49:02 +00:00
$items = [];
$items [ 'tour' ] = [
'#cache' => [
'contexts' => [
'user.permissions' ,
],
],
];
2013-09-16 03:58:06 +00:00
if ( ! \Drupal :: currentUser () -> hasPermission ( 'access tour' )) {
2015-06-01 16:49:02 +00:00
return $items ;
2013-02-18 22:06:10 +00:00
}
2017-03-04 01:20:24 +00:00
$items [ 'tour' ] += [
2013-02-18 22:06:10 +00:00
'#type' => 'toolbar_item' ,
2017-03-04 01:20:24 +00:00
'tab' => [
2013-02-18 22:06:10 +00:00
'#type' => 'html_tag' ,
'#tag' => 'button' ,
'#value' => t ( 'Tour' ),
2017-03-04 01:20:24 +00:00
'#attributes' => [
'class' => [ 'toolbar-icon' , 'toolbar-icon-help' ],
2013-02-18 22:06:10 +00:00
'aria-pressed' => 'false' ,
2017-03-07 01:13:44 +00:00
'type' => 'button' ,
2017-03-04 01:20:24 +00:00
],
],
'#wrapper_attributes' => [
'class' => [ 'tour-toolbar-tab' , 'hidden' ],
2013-02-18 22:06:10 +00:00
'id' => 'toolbar-tab-tour' ,
2017-03-04 01:20:24 +00:00
],
'#attached' => [
'library' => [
2014-03-09 19:59:45 +00:00
'tour/tour' ,
2017-03-04 01:20:24 +00:00
],
],
];
2013-02-18 22:06:10 +00:00
2015-06-01 16:49:02 +00:00
return $items ;
2013-02-18 22:06:10 +00:00
}
/**
2014-10-16 12:36:06 +00:00
* Implements hook_page_bottom () .
2013-02-18 22:06:10 +00:00
*/
2014-10-16 12:36:06 +00:00
function tour_page_bottom ( array & $page_bottom ) {
2013-09-16 03:58:06 +00:00
if ( ! \Drupal :: currentUser () -> hasPermission ( 'access tour' )) {
2013-02-18 23:22:01 +00:00
return ;
}
2014-01-25 20:53:51 +00:00
// Load all of the items and match on route name.
2014-06-24 12:39:26 +00:00
$route_match = \Drupal :: routeMatch ();
$route_name = $route_match -> getRouteName ();
2014-01-25 20:53:51 +00:00
$results = \Drupal :: entityQuery ( 'tour' )
-> condition ( 'routes.*.route_name' , $route_name )
-> execute ();
2016-05-17 09:45:28 +00:00
if ( ! empty ( $results ) && $tours = Tour :: loadMultiple ( array_keys ( $results ))) {
2014-01-25 20:53:51 +00:00
foreach ( $tours as $id => $tour ) {
// Match on params.
2014-06-24 12:39:26 +00:00
if ( ! $tour -> hasMatchingRoute ( $route_name , $route_match -> getRawParameters () -> all ())) {
2014-01-25 20:53:51 +00:00
unset ( $tours [ $id ]);
}
}
if ( ! empty ( $tours )) {
2019-04-29 21:35:23 +00:00
$page_bottom [ 'tour' ] = \Drupal :: entityTypeManager ()
-> getViewBuilder ( 'tour' )
-> viewMultiple ( $tours , 'full' );
2013-02-18 22:06:10 +00:00
}
}
}
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_insert () for tour entities .
2013-02-18 22:06:10 +00:00
*/
function tour_tour_insert ( $entity ) {
2013-09-16 03:58:06 +00:00
\Drupal :: service ( 'plugin.manager.tour.tip' ) -> clearCachedDefinitions ();
2013-02-18 22:06:10 +00:00
}
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_update () for tour entities .
2013-02-18 22:06:10 +00:00
*/
function tour_tour_update ( $entity ) {
2013-09-16 03:58:06 +00:00
\Drupal :: service ( 'plugin.manager.tour.tip' ) -> clearCachedDefinitions ();
2013-02-18 22:06:10 +00:00
}