Move into nested docroot
This commit is contained in:
parent
83a0d3a149
commit
c8b70abde9
13405 changed files with 0 additions and 0 deletions
39
web/core/modules/tour/src/Annotation/Tip.php
Normal file
39
web/core/modules/tour/src/Annotation/Tip.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour\Annotation;
|
||||
|
||||
use Drupal\Component\Annotation\Plugin;
|
||||
|
||||
/**
|
||||
* Defines a tour item annotation object.
|
||||
*
|
||||
* Plugin Namespace: Plugin\tour\tip
|
||||
*
|
||||
* For a working example, see \Drupal\tour\Plugin\tour\tip\TipPluginText
|
||||
*
|
||||
* @see \Drupal\tour\TipPluginBase
|
||||
* @see \Drupal\tour\TipPluginInterface
|
||||
* @see \Drupal\tour\TipPluginManager
|
||||
* @see plugin_api
|
||||
*
|
||||
* @Annotation
|
||||
*/
|
||||
class Tip extends Plugin {
|
||||
|
||||
/**
|
||||
* The plugin ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* The title of the plugin.
|
||||
*
|
||||
* @var \Drupal\Core\Annotation\Translation
|
||||
*
|
||||
* @ingroup plugin_translatable
|
||||
*/
|
||||
public $title;
|
||||
|
||||
}
|
183
web/core/modules/tour/src/Entity/Tour.php
Normal file
183
web/core/modules/tour/src/Entity/Tour.php
Normal file
|
@ -0,0 +1,183 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour\Entity;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
||||
use Drupal\tour\TipsPluginCollection;
|
||||
use Drupal\tour\TourInterface;
|
||||
|
||||
/**
|
||||
* Defines the configured tour entity.
|
||||
*
|
||||
* @ConfigEntityType(
|
||||
* id = "tour",
|
||||
* label = @Translation("Tour"),
|
||||
* handlers = {
|
||||
* "view_builder" = "Drupal\tour\TourViewBuilder"
|
||||
* },
|
||||
* entity_keys = {
|
||||
* "id" = "id",
|
||||
* "label" = "label"
|
||||
* },
|
||||
* config_export = {
|
||||
* "id",
|
||||
* "label",
|
||||
* "module",
|
||||
* "routes",
|
||||
* "tips",
|
||||
* },
|
||||
* lookup_keys = {
|
||||
* "routes.*.route_name"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class Tour extends ConfigEntityBase implements TourInterface {
|
||||
|
||||
/**
|
||||
* The name (plugin ID) of the tour.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* The module which this tour is assigned to.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $module;
|
||||
|
||||
/**
|
||||
* The label of the tour.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $label;
|
||||
|
||||
/**
|
||||
* The routes on which this tour should be displayed.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $routes = array();
|
||||
|
||||
/**
|
||||
* The routes on which this tour should be displayed, keyed by route id.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $keyedRoutes;
|
||||
|
||||
/**
|
||||
* Holds the collection of tips that are attached to this tour.
|
||||
*
|
||||
* @var \Drupal\tour\TipsPluginCollection
|
||||
*/
|
||||
protected $tipsCollection;
|
||||
|
||||
/**
|
||||
* The array of plugin config, only used for export and to populate the $tipsCollection.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $tips = array();
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $values, $entity_type) {
|
||||
parent::__construct($values, $entity_type);
|
||||
|
||||
$this->tipsCollection = new TipsPluginCollection(\Drupal::service('plugin.manager.tour.tip'), $this->tips);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRoutes() {
|
||||
return $this->routes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTip($id) {
|
||||
return $this->tipsCollection->get($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTips() {
|
||||
$tips = array();
|
||||
foreach ($this->tips as $id => $tip) {
|
||||
$tips[] = $this->getTip($id);
|
||||
}
|
||||
uasort($tips, function ($a, $b) {
|
||||
if ($a->getWeight() == $b->getWeight()) {
|
||||
return 0;
|
||||
}
|
||||
return ($a->getWeight() < $b->getWeight()) ? -1 : 1;
|
||||
});
|
||||
|
||||
\Drupal::moduleHandler()->alter('tour_tips', $tips, $this);
|
||||
return array_values($tips);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getModule() {
|
||||
return $this->module;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasMatchingRoute($route_name, $route_params) {
|
||||
if (!isset($this->keyedRoutes)) {
|
||||
$this->keyedRoutes = array();
|
||||
foreach ($this->getRoutes() as $route) {
|
||||
$this->keyedRoutes[$route['route_name']] = isset($route['route_params']) ? $route['route_params'] : array();
|
||||
}
|
||||
}
|
||||
if (!isset($this->keyedRoutes[$route_name])) {
|
||||
// We don't know about this route.
|
||||
return FALSE;
|
||||
}
|
||||
if (empty($this->keyedRoutes[$route_name])) {
|
||||
// We don't need to worry about route params, the route name is enough.
|
||||
return TRUE;
|
||||
}
|
||||
foreach ($this->keyedRoutes[$route_name] as $key => $value) {
|
||||
// If a required param is missing or doesn't match, return FALSE.
|
||||
if (empty($route_params[$key]) || $route_params[$key] !== $value) {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function resetKeyedRoutes() {
|
||||
unset($this->keyedRoutes);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function calculateDependencies() {
|
||||
parent::calculateDependencies();
|
||||
|
||||
foreach ($this->tipsCollection as $instance) {
|
||||
$definition = $instance->getPluginDefinition();
|
||||
$this->addDependency('module', $definition['provider']);
|
||||
}
|
||||
|
||||
$this->addDependency('module', $this->module);
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
132
web/core/modules/tour/src/Plugin/HelpSection/TourHelpSection.php
Normal file
132
web/core/modules/tour/src/Plugin/HelpSection/TourHelpSection.php
Normal file
|
@ -0,0 +1,132 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour\Plugin\HelpSection;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\Core\Link;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\help\Plugin\HelpSection\HelpSectionPluginBase;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides the tours list section for the help page.
|
||||
*
|
||||
* @HelpSection(
|
||||
* id = "tour",
|
||||
* title = @Translation("Tours"),
|
||||
* description = @Translation("Tours guide you through workflows or explain concepts on various user interface pages. The tours with links in this list are on user interface landing pages; the tours without links will show on individual pages (such as when editing a View using the Views UI module). Available tours:"),
|
||||
* permission = "access tour"
|
||||
* )
|
||||
*/
|
||||
class TourHelpSection extends HelpSectionPluginBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* The entity type manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
||||
*/
|
||||
protected $entityTypeManager;
|
||||
|
||||
/**
|
||||
* Constructs a TourHelpSection object.
|
||||
*
|
||||
* @param array $configuration
|
||||
* A configuration array containing information about the plugin instance.
|
||||
* @param string $plugin_id
|
||||
* The plugin_id for the plugin instance.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
||||
* The entity manager service.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->entityTypeManager = $entity_type_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('entity_type.manager')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCacheMaxAge() {
|
||||
// The calculation of which URL (if any) gets put on which tour depends
|
||||
// on a route access check. This can have a lot of inputs, including user
|
||||
// permissions and other factors. Rather than doing a complicated
|
||||
// accounting of the cache metadata for all of these possible factors, set
|
||||
// the max age of the cache to zero to prevent using incorrect cached
|
||||
// information.
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function listTopics() {
|
||||
/** @var \Drupal\tour\TourInterface[] $tours */
|
||||
$tours = $this->entityTypeManager->getStorage('tour')->loadMultiple();
|
||||
// Sort in the manner defined by Tour.
|
||||
uasort($tours, ['Drupal\tour\Entity\Tour', 'sort']);
|
||||
|
||||
// Make a link to each tour, using the first of its routes that can
|
||||
// be linked to by this user, if any.
|
||||
$topics = [];
|
||||
foreach ($tours as $tour) {
|
||||
$title = $tour->label();
|
||||
$id = $tour->id();
|
||||
$routes = $tour->getRoutes();
|
||||
$made_link = FALSE;
|
||||
foreach ($routes as $route) {
|
||||
// Some tours are for routes with parameters. For instance, there is
|
||||
// currently a tour in the Language module for the language edit page,
|
||||
// which appears on all pages with URLs like:
|
||||
// /admin/config/regional/language/edit/LANGCODE.
|
||||
// There is no way to make a link to the page that displays the tour,
|
||||
// because it is a set of pages. The easiest way to detect this is to
|
||||
// use a try/catch exception -- try to make a link, and it will error
|
||||
// out with a missing parameter exception if the route leads to a set
|
||||
// of pages instead of a single page.
|
||||
try {
|
||||
$params = isset($route['route_params']) ? $route['route_params'] : [];
|
||||
$url = Url::fromRoute($route['route_name'], $params);
|
||||
// Skip this route if the current user cannot access it.
|
||||
if (!$url->access()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Generate the link HTML directly, using toString(), to catch
|
||||
// missing parameter exceptions now instead of at render time.
|
||||
$topics[$id] = Link::fromTextAndUrl($title, $url)->toString();
|
||||
// If the line above didn't generate an exception, we have a good
|
||||
// link that the user can access.
|
||||
$made_link = TRUE;
|
||||
break;
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
// Exceptions are normally due to routes that need parameters. If
|
||||
// there is an exception, just try the next route and see if we can
|
||||
// find one that will work for us.
|
||||
}
|
||||
}
|
||||
if (!$made_link) {
|
||||
// None of the routes worked to make a link, so at least display the
|
||||
// tour title.
|
||||
$topics[$id] = $title;
|
||||
}
|
||||
}
|
||||
|
||||
return $topics;
|
||||
}
|
||||
|
||||
}
|
122
web/core/modules/tour/src/Plugin/tour/tip/TipPluginText.php
Normal file
122
web/core/modules/tour/src/Plugin/tour/tip/TipPluginText.php
Normal file
|
@ -0,0 +1,122 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour\Plugin\tour\tip;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\Core\Utility\Token;
|
||||
use Drupal\tour\TipPluginBase;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Displays some text as a tip.
|
||||
*
|
||||
* @Tip(
|
||||
* id = "text",
|
||||
* title = @Translation("Text")
|
||||
* )
|
||||
*/
|
||||
class TipPluginText extends TipPluginBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* The body text which is used for render of this Text Tip.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $body;
|
||||
|
||||
/**
|
||||
* Token service.
|
||||
*
|
||||
* @var \Drupal\Core\Utility\Token
|
||||
*/
|
||||
protected $token;
|
||||
|
||||
/**
|
||||
* The forced position of where the tip will be located.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $location;
|
||||
|
||||
/**
|
||||
* Constructs a \Drupal\tour\Plugin\tour\tip\TipPluginText object.
|
||||
*
|
||||
* @param array $configuration
|
||||
* A configuration array containing information about the plugin instance.
|
||||
* @param string $plugin_id
|
||||
* The plugin_id for the plugin instance.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\Core\Utility\Token $token
|
||||
* The token service.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static($configuration, $plugin_id, $plugin_definition, $container->get('token'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a ID that is guaranteed uniqueness.
|
||||
*
|
||||
* @return string
|
||||
* A unique id to be used to generate aria attributes.
|
||||
*/
|
||||
public function getAriaId() {
|
||||
static $id;
|
||||
if (!isset($id)) {
|
||||
$id = Html::getUniqueId($this->get('id'));
|
||||
}
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns body of the text tip.
|
||||
*
|
||||
* @return string
|
||||
* The tip body.
|
||||
*/
|
||||
public function getBody() {
|
||||
return $this->get('body');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns location of the text tip.
|
||||
*
|
||||
* @return string
|
||||
* The tip location.
|
||||
*/
|
||||
public function getLocation() {
|
||||
return $this->get('location');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAttributes() {
|
||||
$attributes = parent::getAttributes();
|
||||
$attributes['data-aria-describedby'] = 'tour-tip-' . $this->getAriaId() . '-contents';
|
||||
$attributes['data-aria-labelledby'] = 'tour-tip-' . $this->getAriaId() . '-label';
|
||||
if ($location = $this->get('location')) {
|
||||
$attributes['data-options'] = 'tipLocation:' . $location;
|
||||
}
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getOutput() {
|
||||
$output = '<h2 class="tour-tip-label" id="tour-tip-' . $this->getAriaId() . '-label">' . Html::escape($this->getLabel()) . '</h2>';
|
||||
$output .= '<p class="tour-tip-body" id="tour-tip-' . $this->getAriaId() . '-contents">' . $this->token->replace($this->getBody()) . '</p>';
|
||||
return array('#markup' => $output);
|
||||
}
|
||||
|
||||
}
|
76
web/core/modules/tour/src/Tests/TourCacheTagsTest.php
Normal file
76
web/core/modules/tour/src/Tests/TourCacheTagsTest.php
Normal file
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour\Tests;
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\system\Tests\Cache\PageCacheTagsTestBase;
|
||||
use Drupal\tour\Entity\Tour;
|
||||
use Drupal\user\Entity\Role;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests the Tour entity's cache tags.
|
||||
*
|
||||
* @group tour
|
||||
*/
|
||||
class TourCacheTagsTest extends PageCacheTagsTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = array('tour', 'tour_test');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Give anonymous users permission to view nodes, so that we can verify the
|
||||
// cache tags of cached versions of node pages.
|
||||
Role::load(RoleInterface::ANONYMOUS_ID)->grantPermission('access tour')
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests cache tags presence and invalidation of the Tour entity.
|
||||
*
|
||||
* Tests the following cache tags:
|
||||
* - 'tour:<tour ID>'
|
||||
*/
|
||||
public function testRenderedTour() {
|
||||
$url = Url::fromRoute('tour_test.1');
|
||||
|
||||
// Prime the page cache.
|
||||
$this->verifyPageCache($url, 'MISS');
|
||||
|
||||
// Verify a cache hit, but also the presence of the correct cache tags.
|
||||
$expected_tags = [
|
||||
'config:tour.tour.tour-test',
|
||||
'config:user.role.anonymous',
|
||||
'rendered',
|
||||
];
|
||||
$this->verifyPageCache($url, 'HIT', $expected_tags);
|
||||
|
||||
// Verify that after modifying the tour, there is a cache miss.
|
||||
$this->pass('Test modification of tour.', 'Debug');
|
||||
Tour::load('tour-test')->save();
|
||||
$this->verifyPageCache($url, 'MISS');
|
||||
|
||||
// Verify a cache hit.
|
||||
$this->verifyPageCache($url, 'HIT', $expected_tags);
|
||||
|
||||
// Verify that after deleting the tour, there is a cache miss.
|
||||
$this->pass('Test deletion of tour.', 'Debug');
|
||||
Tour::load('tour-test')->delete();
|
||||
$this->verifyPageCache($url, 'MISS');
|
||||
|
||||
// Verify a cache hit.
|
||||
$expected_tags = [
|
||||
'config:user.role.anonymous',
|
||||
'rendered',
|
||||
];
|
||||
$this->verifyPageCache($url, 'HIT', $expected_tags);
|
||||
}
|
||||
|
||||
}
|
141
web/core/modules/tour/src/Tests/TourHelpPageTest.php
Normal file
141
web/core/modules/tour/src/Tests/TourHelpPageTest.php
Normal file
|
@ -0,0 +1,141 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Verifies help page display of tours.
|
||||
*
|
||||
* @group help
|
||||
*/
|
||||
class TourHelpPageTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable, including some providing tours.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['help', 'tour', 'locale', 'language'];
|
||||
|
||||
/**
|
||||
* User that can access tours and help.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $tourUser;
|
||||
|
||||
/**
|
||||
* A user who can access help but not tours.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $noTourUser;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create users. For the Tour user, include permissions for the language
|
||||
// tours' parent pages, but not the translation tour's parent page. See
|
||||
// self:getTourList().
|
||||
$this->tourUser = $this->drupalCreateUser(['access administration pages', 'access tour', 'administer languages']);
|
||||
$this->noTourUser = $this->drupalCreateUser(['access administration pages']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs in users, tests help pages.
|
||||
*/
|
||||
public function testHelp() {
|
||||
$this->drupalLogin($this->tourUser);
|
||||
$this->verifyHelp();
|
||||
|
||||
$this->drupalLogin($this->noTourUser);
|
||||
$this->verifyHelp(FALSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the logged in user has access to the help properly.
|
||||
*
|
||||
* @param bool $tours_ok
|
||||
* (optional) TRUE (default) if the user should see tours, FALSE if not.
|
||||
*/
|
||||
protected function verifyHelp($tours_ok = TRUE) {
|
||||
$this->drupalGet('admin/help');
|
||||
|
||||
// All users should be able to see the module section.
|
||||
$this->assertText('Module overviews are provided by modules');
|
||||
foreach ($this->getModuleList() as $name) {
|
||||
$this->assertLink($name);
|
||||
}
|
||||
|
||||
// Some users should be able to see the tour section.
|
||||
if ($tours_ok) {
|
||||
$this->assertText('Tours guide you through workflows');
|
||||
}
|
||||
else {
|
||||
$this->assertNoText('Tours guide you through workflows');
|
||||
}
|
||||
|
||||
$titles = $this->getTourList();
|
||||
|
||||
// Test the titles that should be links.
|
||||
foreach ($titles[0] as $title) {
|
||||
if ($tours_ok) {
|
||||
$this->assertLink($title);
|
||||
}
|
||||
else {
|
||||
$this->assertNoLink($title);
|
||||
// Just test the first item in the list of links that should not
|
||||
// be there, because the second matches the name of a module that is
|
||||
// in the Module overviews section, so the link will be there and
|
||||
// this test will fail. Testing one should be sufficient to verify
|
||||
// the page is working correctly.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Test the titles that should not be links.
|
||||
foreach ($titles[1] as $title) {
|
||||
if ($tours_ok) {
|
||||
$this->assertText($title);
|
||||
$this->assertNoLink($title);
|
||||
}
|
||||
else {
|
||||
$this->assertNoText($title);
|
||||
// Just test the first item in the list of text that should not
|
||||
// be there, because the second matches part of the name of a module
|
||||
// that is in the Module overviews section, so the text will be there
|
||||
// and this test will fail. Testing one should be sufficient to verify
|
||||
// the page is working correctly.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of modules to test for hook_help() pages.
|
||||
*
|
||||
* @return array
|
||||
* A list of module names to test.
|
||||
*/
|
||||
protected function getModuleList() {
|
||||
return ['Help', 'Tour'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of tours to test.
|
||||
*
|
||||
* @return array
|
||||
* A list of tour titles to test. The first array element is a list of tours
|
||||
* with links, and the second is a list of tours without links. Assumes
|
||||
* that the user being tested has 'administer languages' permission but
|
||||
* not 'translate interface'.
|
||||
*/
|
||||
protected function getTourList() {
|
||||
return [['Adding languages', 'Language'], ['Editing languages', 'Translation']];
|
||||
}
|
||||
|
||||
}
|
188
web/core/modules/tour/src/Tests/TourTest.php
Normal file
188
web/core/modules/tour/src/Tests/TourTest.php
Normal file
|
@ -0,0 +1,188 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour\Tests;
|
||||
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
use Drupal\tour\Entity\Tour;
|
||||
|
||||
/**
|
||||
* Tests the functionality of tour tips.
|
||||
*
|
||||
* @group tour
|
||||
*/
|
||||
class TourTest extends TourTestBasic {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['block', 'tour', 'locale', 'language', 'tour_test'];
|
||||
|
||||
/**
|
||||
* The permissions required for a logged in user to test tour tips.
|
||||
*
|
||||
* @var array
|
||||
* A list of permissions.
|
||||
*/
|
||||
protected $permissions = array('access tour', 'administer languages');
|
||||
|
||||
/**
|
||||
* Tour tip attributes to be tested. Keyed by the path.
|
||||
*
|
||||
* @var array
|
||||
* An array of tip attributes, keyed by path.
|
||||
*/
|
||||
protected $tips = array(
|
||||
'tour-test-1' => array(),
|
||||
);
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->drupalPlaceBlock('local_actions_block', [
|
||||
'theme' => 'seven',
|
||||
'region' => 'content'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test tour functionality.
|
||||
*/
|
||||
public function testTourFunctionality() {
|
||||
// Navigate to tour-test-1 and verify the tour_test_1 tip is found with appropriate classes.
|
||||
$this->drupalGet('tour-test-1');
|
||||
|
||||
// Test the TourTestBase class assertTourTips() method.
|
||||
$tips = array();
|
||||
$tips[] = array('data-id' => 'tour-test-1');
|
||||
$tips[] = array('data-class' => 'tour-test-5');
|
||||
$this->assertTourTips($tips);
|
||||
$this->assertTourTips();
|
||||
|
||||
$elements = $this->xpath('//li[@data-id=:data_id and @class=:classes and ./p//a[@href=:href and contains(., :text)]]', array(
|
||||
':classes' => 'tip-module-tour-test tip-type-text tip-tour-test-1',
|
||||
':data_id' => 'tour-test-1',
|
||||
':href' => \Drupal::url('<front>', [], ['absolute' => TRUE]),
|
||||
':text' => 'Drupal',
|
||||
));
|
||||
$this->assertEqual(count($elements), 1, 'Found Token replacement.');
|
||||
|
||||
$elements = $this->cssSelect("li[data-id=tour-test-1] h2:contains('The first tip')");
|
||||
$this->assertEqual(count($elements), 1, 'Found English variant of tip 1.');
|
||||
|
||||
$elements = $this->cssSelect("li[data-id=tour-test-2] h2:contains('The quick brown fox')");
|
||||
$this->assertNotEqual(count($elements), 1, 'Did not find English variant of tip 2.');
|
||||
|
||||
$elements = $this->cssSelect("li[data-id=tour-test-1] h2:contains('La pioggia cade in spagna')");
|
||||
$this->assertNotEqual(count($elements), 1, 'Did not find Italian variant of tip 1.');
|
||||
|
||||
// Ensure that plugins work.
|
||||
$elements = $this->xpath('//img[@src="http://local/image.png"]');
|
||||
$this->assertEqual(count($elements), 1, 'Image plugin tip found.');
|
||||
|
||||
// Navigate to tour-test-2/subpath and verify the tour_test_2 tip is found.
|
||||
$this->drupalGet('tour-test-2/subpath');
|
||||
$elements = $this->cssSelect("li[data-id=tour-test-2] h2:contains('The quick brown fox')");
|
||||
$this->assertEqual(count($elements), 1, 'Found English variant of tip 2.');
|
||||
|
||||
$elements = $this->cssSelect("li[data-id=tour-test-1] h2:contains('The first tip')");
|
||||
$this->assertNotEqual(count($elements), 1, 'Did not find English variant of tip 1.');
|
||||
|
||||
// Enable Italian language and navigate to it/tour-test1 and verify italian
|
||||
// version of tip is found.
|
||||
ConfigurableLanguage::createFromLangcode('it')->save();
|
||||
$this->drupalGet('it/tour-test-1');
|
||||
|
||||
$elements = $this->cssSelect("li[data-id=tour-test-1] h2:contains('La pioggia cade in spagna')");
|
||||
$this->assertEqual(count($elements), 1, 'Found Italian variant of tip 1.');
|
||||
|
||||
$elements = $this->cssSelect("li[data-id=tour-test-2] h2:contains('The quick brown fox')");
|
||||
$this->assertNotEqual(count($elements), 1, 'Did not find English variant of tip 1.');
|
||||
|
||||
// Programmatically create a tour for use through the remainder of the test.
|
||||
$tour = Tour::create(array(
|
||||
'id' => 'tour-entity-create-test-en',
|
||||
'label' => 'Tour test english',
|
||||
'langcode' => 'en',
|
||||
'module' => 'system',
|
||||
'routes' => array(
|
||||
array('route_name' => 'tour_test.1'),
|
||||
),
|
||||
'tips' => array(
|
||||
'tour-test-1' => array(
|
||||
'id' => 'tour-code-test-1',
|
||||
'plugin' => 'text',
|
||||
'label' => 'The rain in spain',
|
||||
'body' => 'Falls mostly on the plain.',
|
||||
'weight' => '100',
|
||||
'attributes' => array(
|
||||
'data-id' => 'tour-code-test-1',
|
||||
),
|
||||
),
|
||||
'tour-code-test-2' => array(
|
||||
'id' => 'tour-code-test-2',
|
||||
'plugin' => 'image',
|
||||
'label' => 'The awesome image',
|
||||
'url' => 'http://local/image.png',
|
||||
'weight' => 1,
|
||||
'attributes' => array(
|
||||
'data-id' => 'tour-code-test-2'
|
||||
),
|
||||
),
|
||||
),
|
||||
));
|
||||
$tour->save();
|
||||
|
||||
// Ensure that a tour entity has the expected dependencies based on plugin
|
||||
// providers and the module named in the configuration entity.
|
||||
$dependencies = $tour->calculateDependencies()->getDependencies();
|
||||
$this->assertEqual($dependencies['module'], array('system', 'tour_test'));
|
||||
|
||||
$this->drupalGet('tour-test-1');
|
||||
|
||||
// Load it back from the database and verify storage worked.
|
||||
$entity_save_tip = Tour::load('tour-entity-create-test-en');
|
||||
// Verify that hook_ENTITY_TYPE_load() integration worked.
|
||||
$this->assertEqual($entity_save_tip->loaded, 'Load hooks work');
|
||||
// Verify that hook_ENTITY_TYPE_presave() integration worked.
|
||||
$this->assertEqual($entity_save_tip->label(), 'Tour test english alter');
|
||||
|
||||
// Navigate to tour-test-1 and verify the new tip is found.
|
||||
$this->drupalGet('tour-test-1');
|
||||
$elements = $this->cssSelect("li[data-id=tour-code-test-1] h2:contains('The rain in spain')");
|
||||
$this->assertEqual(count($elements), 1, 'Found the required tip markup for tip 4');
|
||||
|
||||
// Verify that the weight sorting works by ensuring the lower weight item
|
||||
// (tip 4) has the 'End tour' button.
|
||||
$elements = $this->cssSelect("li[data-id=tour-code-test-1][data-text='End tour']");
|
||||
$this->assertEqual(count($elements), 1, 'Found code tip was weighted last and had "End tour".');
|
||||
|
||||
// Test hook_tour_alter().
|
||||
$this->assertText('Altered by hook_tour_tips_alter');
|
||||
|
||||
// Navigate to tour-test-3 and verify the tour_test_1 tip is found with
|
||||
// appropriate classes.
|
||||
$this->drupalGet('tour-test-3/foo');
|
||||
$elements = $this->xpath('//li[@data-id=:data_id and @class=:classes and ./h2[contains(., :text)]]', array(
|
||||
':classes' => 'tip-module-tour-test tip-type-text tip-tour-test-1',
|
||||
':data_id' => 'tour-test-1',
|
||||
':text' => 'The first tip',
|
||||
));
|
||||
$this->assertEqual(count($elements), 1, 'Found English variant of tip 1.');
|
||||
|
||||
// Navigate to tour-test-3 and verify the tour_test_1 tip is not found with
|
||||
// appropriate classes.
|
||||
$this->drupalGet('tour-test-3/bar');
|
||||
$elements = $this->xpath('//li[@data-id=:data_id and @class=:classes and ./h2[contains(., :text)]]', array(
|
||||
':classes' => 'tip-module-tour-test tip-type-text tip-tour-test-1',
|
||||
':data_id' => 'tour-test-1',
|
||||
':text' => 'The first tip',
|
||||
));
|
||||
$this->assertEqual(count($elements), 0, 'Did not find English variant of tip 1.');
|
||||
}
|
||||
|
||||
}
|
70
web/core/modules/tour/src/Tests/TourTestBase.php
Normal file
70
web/core/modules/tour/src/Tests/TourTestBase.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Base class for testing Tour functionality.
|
||||
*/
|
||||
abstract class TourTestBase extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Assert function to determine if tips rendered to the page
|
||||
* have a corresponding page element.
|
||||
*
|
||||
* @param array $tips
|
||||
* A list of tips which provide either a "data-id" or "data-class".
|
||||
*
|
||||
* @code
|
||||
* // Basic example.
|
||||
* $this->assertTourTips();
|
||||
*
|
||||
* // Advanced example. The following would be used for multipage or
|
||||
* // targeting a specific subset of tips.
|
||||
* $tips = array();
|
||||
* $tips[] = array('data-id' => 'foo');
|
||||
* $tips[] = array('data-id' => 'bar');
|
||||
* $tips[] = array('data-class' => 'baz');
|
||||
* $this->assertTourTips($tips);
|
||||
* @endcode
|
||||
*/
|
||||
public function assertTourTips($tips = array()) {
|
||||
// Get the rendered tips and their data-id and data-class attributes.
|
||||
if (empty($tips)) {
|
||||
// Tips are rendered as <li> elements inside <ol id="tour">.
|
||||
$rendered_tips = $this->xpath('//ol[@id = "tour"]//li[starts-with(@class, "tip")]');
|
||||
foreach ($rendered_tips as $rendered_tip) {
|
||||
$attributes = (array) $rendered_tip->attributes();
|
||||
$tips[] = $attributes['@attributes'];
|
||||
}
|
||||
}
|
||||
|
||||
// If the tips are still empty we need to fail.
|
||||
if (empty($tips)) {
|
||||
$this->fail('Could not find tour tips on the current page.');
|
||||
}
|
||||
else {
|
||||
// Check for corresponding page elements.
|
||||
$total = 0;
|
||||
$modals = 0;
|
||||
foreach ($tips as $tip) {
|
||||
if (!empty($tip['data-id'])) {
|
||||
$elements = \PHPUnit_Util_XML::cssSelect('#' . $tip['data-id'], TRUE, $this->content, TRUE);
|
||||
$this->assertTrue(!empty($elements) && count($elements) === 1, format_string('Found corresponding page element for tour tip with id #%data-id', array('%data-id' => $tip['data-id'])));
|
||||
}
|
||||
elseif (!empty($tip['data-class'])) {
|
||||
$elements = \PHPUnit_Util_XML::cssSelect('.' . $tip['data-class'], TRUE, $this->content, TRUE);
|
||||
$this->assertFalse(empty($elements), format_string('Found corresponding page element for tour tip with class .%data-class', array('%data-class' => $tip['data-class'])));
|
||||
}
|
||||
else {
|
||||
// It's a modal.
|
||||
$modals++;
|
||||
}
|
||||
$total++;
|
||||
}
|
||||
$this->pass(format_string('Total %total Tips tested of which %modals modal(s).', array('%total' => $total, '%modals' => $modals)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
70
web/core/modules/tour/src/Tests/TourTestBasic.php
Normal file
70
web/core/modules/tour/src/Tests/TourTestBasic.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour\Tests;
|
||||
|
||||
/**
|
||||
* Simple tour tips test base.
|
||||
*/
|
||||
abstract class TourTestBasic extends TourTestBase {
|
||||
|
||||
/**
|
||||
* Tour tip attributes to be tested. Keyed by the path.
|
||||
*
|
||||
* @var array
|
||||
* An array of tip attributes, keyed by path.
|
||||
*
|
||||
* @code
|
||||
* protected $tips = array(
|
||||
* '/foo/bar' => array(
|
||||
* array('data-id' => 'foo'),
|
||||
* array('data-class' => 'bar'),
|
||||
* ),
|
||||
* );
|
||||
* @endcode
|
||||
*/
|
||||
protected $tips = array();
|
||||
|
||||
/**
|
||||
* An admin user with administrative permissions for tour.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* The permissions required for a logged in user to test tour tips.
|
||||
*
|
||||
* @var array
|
||||
* A list of permissions.
|
||||
*/
|
||||
protected $permissions = array('access tour');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Make sure we are using distinct default and administrative themes for
|
||||
// the duration of these tests.
|
||||
$this->container->get('theme_handler')->install(array('bartik', 'seven'));
|
||||
$this->config('system.theme')
|
||||
->set('default', 'bartik')
|
||||
->set('admin', 'seven')
|
||||
->save();
|
||||
|
||||
$this->permissions[] = 'view the administration theme';
|
||||
|
||||
// Create an admin user to view tour tips.
|
||||
$this->adminUser = $this->drupalCreateUser($this->permissions);
|
||||
$this->drupalLogin($this->adminUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple tip test.
|
||||
*/
|
||||
public function testTips() {
|
||||
foreach ($this->tips as $path => $attributes) {
|
||||
$this->drupalGet($path);
|
||||
$this->assertTourTips($attributes);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
82
web/core/modules/tour/src/TipPluginBase.php
Normal file
82
web/core/modules/tour/src/TipPluginBase.php
Normal file
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour;
|
||||
|
||||
use Drupal\Core\Plugin\PluginBase;
|
||||
|
||||
/**
|
||||
* Defines a base tour item implementation.
|
||||
*
|
||||
* @see \Drupal\tour\Annotation\Tip
|
||||
* @see \Drupal\tour\TipPluginInterface
|
||||
* @see \Drupal\tour\TipPluginManager
|
||||
* @see plugin_api
|
||||
*/
|
||||
abstract class TipPluginBase extends PluginBase implements TipPluginInterface {
|
||||
|
||||
/**
|
||||
* The label which is used for render of this tip.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $label;
|
||||
|
||||
/**
|
||||
* Allows tips to take more priority that others.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $weight;
|
||||
|
||||
/**
|
||||
* The attributes that will be applied to the markup of this tip.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $attributes;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function id() {
|
||||
return $this->get('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLabel() {
|
||||
return $this->get('label');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getWeight() {
|
||||
return $this->get('weight');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAttributes() {
|
||||
return $this->get('attributes') ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($key) {
|
||||
if (!empty($this->configuration[$key])) {
|
||||
return $this->configuration[$key];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set($key, $value) {
|
||||
$this->configuration[$key] = $value;
|
||||
}
|
||||
|
||||
}
|
77
web/core/modules/tour/src/TipPluginInterface.php
Normal file
77
web/core/modules/tour/src/TipPluginInterface.php
Normal file
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour;
|
||||
|
||||
/**
|
||||
* Defines an interface for tour items.
|
||||
*
|
||||
* @see \Drupal\tour\Annotation\Tip
|
||||
* @see \Drupal\tour\TipPluginBase
|
||||
* @see \Drupal\tour\TipPluginManager
|
||||
* @see plugin_api
|
||||
*/
|
||||
interface TipPluginInterface {
|
||||
|
||||
/**
|
||||
* Returns id of the tip.
|
||||
*
|
||||
* @return string
|
||||
* The id of the tip.
|
||||
*/
|
||||
public function id();
|
||||
|
||||
/**
|
||||
* Returns label of the tip.
|
||||
*
|
||||
* @return string
|
||||
* The label of the tip.
|
||||
*/
|
||||
public function getLabel();
|
||||
|
||||
/**
|
||||
* Returns weight of the tip.
|
||||
*
|
||||
* @return string
|
||||
* The weight of the tip.
|
||||
*/
|
||||
public function getWeight();
|
||||
|
||||
/**
|
||||
* Returns an array of attributes for the tip wrapper.
|
||||
*
|
||||
* @return array
|
||||
* An array of classes and values.
|
||||
*/
|
||||
public function getAttributes();
|
||||
|
||||
/**
|
||||
* Used for returning values by key.
|
||||
*
|
||||
* @var string
|
||||
* Key of the value.
|
||||
*
|
||||
* @return string
|
||||
* Value of the key.
|
||||
*/
|
||||
public function get($key);
|
||||
|
||||
/**
|
||||
* Used for returning values by key.
|
||||
*
|
||||
* @var string
|
||||
* Key of the value.
|
||||
*
|
||||
* @var string
|
||||
* Value of the key.
|
||||
*/
|
||||
public function set($key, $value);
|
||||
|
||||
/**
|
||||
* Returns a renderable array.
|
||||
*
|
||||
* @return array
|
||||
* A renderable array.
|
||||
*/
|
||||
public function getOutput();
|
||||
|
||||
}
|
37
web/core/modules/tour/src/TipPluginManager.php
Normal file
37
web/core/modules/tour/src/TipPluginManager.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour;
|
||||
|
||||
use Drupal\Core\Cache\CacheBackendInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\Plugin\DefaultPluginManager;
|
||||
|
||||
/**
|
||||
* Provides a plugin manager for tour items.
|
||||
*
|
||||
* @see \Drupal\tour\Annotation\Tip
|
||||
* @see \Drupal\tour\TipPluginBase
|
||||
* @see \Drupal\tour\TipPluginInterface
|
||||
* @see plugin_api
|
||||
*/
|
||||
class TipPluginManager extends DefaultPluginManager {
|
||||
|
||||
/**
|
||||
* Constructs a new TipPluginManager.
|
||||
*
|
||||
* @param \Traversable $namespaces
|
||||
* An object that implements \Traversable which contains the root paths
|
||||
* keyed by the corresponding namespace to look for plugin implementations,
|
||||
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
|
||||
* Cache backend instance to use.
|
||||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
||||
* The module handler to invoke the alter hook with.
|
||||
*/
|
||||
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
|
||||
parent::__construct('Plugin/tour/tip', $namespaces, $module_handler, 'Drupal\tour\TipPluginInterface', 'Drupal\tour\Annotation\Tip');
|
||||
|
||||
$this->alterInfo('tour_tips_info');
|
||||
$this->setCacheBackend($cache_backend, 'tour_plugins');
|
||||
}
|
||||
|
||||
}
|
26
web/core/modules/tour/src/TipsPluginCollection.php
Normal file
26
web/core/modules/tour/src/TipsPluginCollection.php
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour;
|
||||
|
||||
use Drupal\Core\Plugin\DefaultLazyPluginCollection;
|
||||
|
||||
/**
|
||||
* A collection of tips.
|
||||
*/
|
||||
class TipsPluginCollection extends DefaultLazyPluginCollection {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $pluginKey = 'plugin';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return \Drupal\tour\TipPluginInterface
|
||||
*/
|
||||
public function &get($instance_id) {
|
||||
return parent::get($instance_id);
|
||||
}
|
||||
|
||||
}
|
65
web/core/modules/tour/src/TourInterface.php
Normal file
65
web/core/modules/tour/src/TourInterface.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
||||
|
||||
/**
|
||||
* Provides an interface defining a tour entity.
|
||||
*/
|
||||
interface TourInterface extends ConfigEntityInterface {
|
||||
|
||||
/**
|
||||
* The routes that this tour will appear on.
|
||||
*
|
||||
* @return array
|
||||
* Returns array of routes for the tour.
|
||||
*/
|
||||
public function getRoutes();
|
||||
|
||||
/**
|
||||
* Whether the tour matches a given set of route parameters.
|
||||
*
|
||||
* @param string $route_name
|
||||
* The route name the parameters are for.
|
||||
* @param array $route_params
|
||||
* Associative array of raw route params.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the tour matches the route parameters.
|
||||
*/
|
||||
public function hasMatchingRoute($route_name, $route_params);
|
||||
|
||||
/**
|
||||
* Returns tip plugin.
|
||||
*
|
||||
* @param string $id
|
||||
* The identifier of the tip.
|
||||
*
|
||||
* @return \Drupal\tour\TipPluginInterface
|
||||
* The tip plugin.
|
||||
*/
|
||||
public function getTip($id);
|
||||
|
||||
/**
|
||||
* Returns the tips for this tour.
|
||||
*
|
||||
* @return array
|
||||
* An array of tip plugins.
|
||||
*/
|
||||
public function getTips();
|
||||
|
||||
/**
|
||||
* Gets the module this tour belongs to.
|
||||
*
|
||||
* @return string
|
||||
* The module this tour belongs to.
|
||||
*/
|
||||
public function getModule();
|
||||
|
||||
/**
|
||||
* Resets the statically cached keyed routes.
|
||||
*/
|
||||
public function resetKeyedRoutes();
|
||||
|
||||
}
|
75
web/core/modules/tour/src/TourViewBuilder.php
Normal file
75
web/core/modules/tour/src/TourViewBuilder.php
Normal file
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\tour;
|
||||
|
||||
use Drupal\Core\Entity\EntityViewBuilder;
|
||||
use Drupal\Component\Utility\Html;
|
||||
|
||||
/**
|
||||
* Provides a Tour view builder.
|
||||
*/
|
||||
class TourViewBuilder extends EntityViewBuilder {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewMultiple(array $entities = array(), $view_mode = 'full', $langcode = NULL) {
|
||||
/** @var \Drupal\tour\TourInterface[] $entities */
|
||||
$build = array();
|
||||
foreach ($entities as $entity_id => $entity) {
|
||||
$tips = $entity->getTips();
|
||||
$count = count($tips);
|
||||
$list_items = array();
|
||||
foreach ($tips as $index => $tip) {
|
||||
if ($output = $tip->getOutput()) {
|
||||
$attributes = array(
|
||||
'class' => array(
|
||||
'tip-module-' . Html::cleanCssIdentifier($entity->getModule()),
|
||||
'tip-type-' . Html::cleanCssIdentifier($tip->getPluginId()),
|
||||
'tip-' . Html::cleanCssIdentifier($tip->id()),
|
||||
),
|
||||
);
|
||||
$list_items[] = array(
|
||||
'output' => $output,
|
||||
'counter' => array(
|
||||
'#type' => 'container',
|
||||
'#attributes' => array(
|
||||
'class' => array(
|
||||
'tour-progress',
|
||||
),
|
||||
),
|
||||
'#children' => t('@tour_item of @total', array('@tour_item' => $index + 1, '@total' => $count)),
|
||||
),
|
||||
'#wrapper_attributes' => $tip->getAttributes() + $attributes,
|
||||
);
|
||||
}
|
||||
}
|
||||
// If there is at least one tour item, build the tour.
|
||||
if ($list_items) {
|
||||
end($list_items);
|
||||
$key = key($list_items);
|
||||
$list_items[$key]['#wrapper_attributes']['data-text'] = t('End tour');
|
||||
$build[$entity_id] = array(
|
||||
'#theme' => 'item_list',
|
||||
'#items' => $list_items,
|
||||
'#list_type' => 'ol',
|
||||
'#attributes' => array(
|
||||
'id' => 'tour',
|
||||
'class' => array(
|
||||
'hidden',
|
||||
),
|
||||
),
|
||||
'#cache' => [
|
||||
'tags' => $entity->getCacheTags(),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
// If at least one tour was built, attach the tour library.
|
||||
if ($build) {
|
||||
$build['#attached']['library'][] = 'tour/tour';
|
||||
}
|
||||
return $build;
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue