Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,44 @@
<?php
/**
* @file
* Contains \Drupal\tour\Annotation\Tip.
*/
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;
}

View file

@ -0,0 +1,189 @@
<?php
/**
* @file
* Contains \Drupal\tour\Entity\Tour.
*/
namespace Drupal\tour\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
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();
/**
* Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::__construct();
*/
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->dependencies;
}
}

View file

@ -0,0 +1,129 @@
<?php
/**
* @file
* Contains \Drupal\tour\Plugin\tour\tip\TipPluginText.
*/
namespace Drupal\tour\Plugin\tour\tip;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\Xss;
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">' . SafeMarkup::checkPlain($this->getLabel()) . '</h2>';
$output .= '<p class="tour-tip-body" id="tour-tip-' . $this->getAriaId() . '-contents">' . Xss::filterAdmin($this->token->replace($this->getBody())) . '</p>';
return array('#markup' => $output);
}
}

View file

@ -0,0 +1,79 @@
<?php
/**
* @file
* Contains \Drupal\tour\Tests\TourCacheTagsTest.
*/
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',
'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 = [
'rendered',
];
$this->verifyPageCache($url, 'HIT', $expected_tags);
}
}

View file

@ -0,0 +1,47 @@
<?php
/**
* @file
* Contains \Drupal\tour\Tests\TourPluginTest.
*/
namespace Drupal\tour\Tests;
use Drupal\simpletest\KernelTestBase;
/**
* Tests the functionality of tour plugins.
*
* @group tour
*/
class TourPluginTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('tour');
/**
* Stores the tour plugin manager.
*
* @var \Drupal\tour\TipPluginManager
*/
protected $pluginManager;
protected function setUp() {
parent::setUp();
$this->installConfig(array('tour'));
$this->pluginManager = $this->container->get('plugin.manager.tour.tip');
}
/**
* Test tour plugins.
*/
public function testTourPlugins() {
$this->assertIdentical(count($this->pluginManager->getDefinitions()), 1, 'Only tour plugins for the enabled modules were returned.');
}
}

View file

@ -0,0 +1,179 @@
<?php
/**
* @file
* Contains \Drupal\tour\Tests\TourTest.
*/
namespace Drupal\tour\Tests;
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Tests the functionality of tour tips.
*
* @group tour
*/
class TourTest extends TourTestBasic {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('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(),
);
/**
* 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 = entity_create('tour', 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();
$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 = entity_load('tour', '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.');
}
}

View file

@ -0,0 +1,75 @@
<?php
/**
* @file
* Contains \Drupal\tour\Tests\TourTestBase.
*/
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'])));
}
else if (!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)));
}
}
}

View file

@ -0,0 +1,75 @@
<?php
/**
* @file
* Contains \Drupal\tour\Tests\TourTestBasic.
*/
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);
}
}
}

View file

@ -0,0 +1,87 @@
<?php
/**
* @file
* Contains \Drupal\tour\TipPluginBase.
*/
namespace Drupal\tour;
use Drupal\Core\Plugin\PluginBase;
use Drupal\tour\TipPluginInterface;
/**
* 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;
}
}

View file

@ -0,0 +1,82 @@
<?php
/**
* @file
* Contains \Drupal\tour\TipPluginInterface.
*/
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();
}

View file

@ -0,0 +1,42 @@
<?php
/**
* @file
* Contains \Drupal\tour\TipPluginManager.
*/
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');
}
}

View file

@ -0,0 +1,31 @@
<?php
/**
* @file
* Contains \Drupal\tour\TipsPluginCollection.
*/
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);
}
}

View file

@ -0,0 +1,70 @@
<?php
/**
* @file
* Contains \Drupal\tour\TourInterface.
*/
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();
}

View file

@ -0,0 +1,81 @@
<?php
/**
* @file
* Contains \Drupal\tour\TourViewBuilder.
*/
namespace Drupal\tour;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Entity\EntityInterface;
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;
}
}