diff --git a/core/modules/tour/lib/Drupal/tour/Plugin/Core/Entity/TourTooltip.php b/core/modules/tour/lib/Drupal/tour/Plugin/Core/Entity/TourTooltip.php index f401ebd..8261742 100644 --- a/core/modules/tour/lib/Drupal/tour/Plugin/Core/Entity/TourTooltip.php +++ b/core/modules/tour/lib/Drupal/tour/Plugin/Core/Entity/TourTooltip.php @@ -10,8 +10,6 @@ use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Annotation\Plugin; use Drupal\Core\Annotation\Translation; -use Drupal\Component\Plugin\Exception\PluginException; -use Drupal\tour\TourInterface; /** * Defines the configured text tour entity. @@ -24,10 +22,8 @@ * config_prefix = "tour.tooltip", * entity_keys = { * "id" = "id", - * "uuid" = "uuid", * "label" = "label", - * "langcode" = "langcode", - * "paths" = "paths", + * "uuid" = "uuid" * } * ) */ @@ -48,58 +44,79 @@ class TourTooltip extends ConfigEntityBase { public $label; /** - * The body of the tour. + * The UUID of the tour. * * @var string */ - public $body; + public $uuid; /** - * The array of settings for the tour. + * The body of the tour. * - * @var array + * @var string */ - public $settings = array(); + protected $body; /** - * The array of paths for the tour. + * The array of settings for the tour. * * @var array */ - public $paths = array(); + protected $settings = array(); /** - * The language for the tour. + * The array of paths for the tour. * - * @var string + * @var array */ - public $langcode; + protected $paths = array(); /** * The weight of the tour. * * @var int */ - public $weight; + protected $weight; /** * The location of the tip. Valid options are top|bottom|left|right. * * @var string */ - public $location; + protected $location; /** * The array of attributes for the tour. * * @var array */ - public $attributes = array(); + protected $attributes = array(); /** * The module for the tour. * * @var string */ - public $module; + protected $module; + + /** + * Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::getExportProperties(); + */ + public function getExportProperties() { + $properties = parent::getExportProperties(); + $names = array( + 'body', + 'paths', + 'settings', + 'weight', + 'location', + 'attributes', + 'module', + ); + foreach ($names as $name) { + $properties[$name] = $this->get($name); + } + return $properties; + } + } diff --git a/core/modules/tour/lib/Drupal/tour/Plugin/Derivative/Tooltip.php b/core/modules/tour/lib/Drupal/tour/Plugin/Derivative/Tooltip.php index 544ce96..011097e 100644 --- a/core/modules/tour/lib/Drupal/tour/Plugin/Derivative/Tooltip.php +++ b/core/modules/tour/lib/Drupal/tour/Plugin/Derivative/Tooltip.php @@ -23,11 +23,8 @@ class Tooltip implements DerivativeInterface { /** * Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinition(). - * - * Retrieves a specific tooltip. */ public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) { - if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) { return $this->derivatives[$derivative_id]; } @@ -37,20 +34,16 @@ public function getDerivativeDefinition($derivative_id, array $base_plugin_defin /** * Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions(). - * - * Retrieves tooltips. */ public function getDerivativeDefinitions(array $base_plugin_definition) { - $configs = config_get_storage_names_with_prefix('tour.tooltip.'); - $derivatives = array(); - $ids = array(); - foreach ($configs as $key) { - $config = config($key); - $id = $config->get('id'); - $paths = $config->get('paths'); - $langcode = $config->get('langcode'); - $this->derivatives[$id] = array('id' => $id, 'paths' => $paths, 'langcode' => $langcode) + $base_plugin_definition; + foreach (entity_load_multiple('tour_tooltip') as $id => $tooltip) { + $this->derivatives[$id] = array( + 'id' => $tooltip->id(), + 'paths' => $tooltip->get('paths'), + 'langcode' => $tooltip->get('langcode'), + ) + $base_plugin_definition; } return $this->derivatives; } + } diff --git a/core/modules/tour/lib/Drupal/tour/Plugin/tour/tour/Tooltip.php b/core/modules/tour/lib/Drupal/tour/Plugin/tour/tour/Tooltip.php index a02d058..145e872 100644 --- a/core/modules/tour/lib/Drupal/tour/Plugin/tour/tour/Tooltip.php +++ b/core/modules/tour/lib/Drupal/tour/Plugin/tour/tour/Tooltip.php @@ -28,24 +28,28 @@ class Tooltip extends TourBase { */ public function __construct(array $configuration, $plugin_id, $discovery) { parent::__construct($configuration, $plugin_id, $discovery); + $this->entity = entity_load('tour_tooltip', $configuration['id']); - $this->weight = $this->entity->weight; + $this->weight = $this->entity->get('weight'); } /** * Implements \Drupal\tour\TourInterface::getOutput(). */ public function getOutput() { - return theme('tour_tooltip', array('tooltip' => $this->entity)); + return array( + '#theme' => 'tour_tooltip', + '#tooltip' => $this->entity, + ); } /** * Implements \Drupal\tour\TourInterface::getAttributes(). */ public function getAttributes() { - $attributes = $this->entity->attributes; - if (!empty($this->entity->location)) { - $attributes['data-options'] = 'tipLocation:' . $this->entity->location; + $attributes = $this->entity->get('attributes'); + if ($location = $this->entity->get('location')) { + $attributes['data-options'] = 'tipLocation:' . $location; } // Add aria support. $this->entity->ariaId = drupal_html_id($this->entity->id()); @@ -58,14 +62,16 @@ public function getAttributes() { * Implements \Drupal\tour\TourInterface::getPaths(). */ public function getPaths() { - return $this->entity->paths; + return $this->entity->get('paths'); } /** * Implements \Drupal\tour\TourInterface::setAttribute(). */ public function setAttribute($key, $value) { - $this->entity->attributes[$key] = $value; + $attributes = $this->entity->get('attributes'); + $attributes[$key] = $value; + $this->entity->set('attributes', $attributes); } /** @@ -79,7 +85,7 @@ public function getAttachments() { * Implements \Drupal\tour\TourInterface::getLanguage(). */ public function getLanguage() { - return $this->entity->langcode; + return $this->entity->get('langcode'); } /** @@ -88,4 +94,5 @@ public function getLanguage() { public function label() { return $this->entity->label(); } + } diff --git a/core/modules/tour/lib/Drupal/tour/Tests/TourPluginTest.php b/core/modules/tour/lib/Drupal/tour/Tests/TourPluginTest.php new file mode 100644 index 0000000..63f8d57 --- /dev/null +++ b/core/modules/tour/lib/Drupal/tour/Tests/TourPluginTest.php @@ -0,0 +1,54 @@ + 'Tour plugin tests', + 'description' => 'Test the functionality of tour plugins.', + 'group' => 'Tour', + ); + } + + protected function setUp() { + parent::setUp(); + + config_install_default_config('module', 'tour'); + $this->pluginManager = $this->container->get('plugin.manager.tour'); + } + + /** + * Test tour plugins. + */ + public function testTourPlugins() { + $this->assertIdentical(count($this->pluginManager->getDefinitions()), 2, 'Only tour plugins for the enabled modules were returned.'); + } + +} + diff --git a/core/modules/tour/lib/Drupal/tour/Tests/TourTest.php b/core/modules/tour/lib/Drupal/tour/Tests/TourTest.php index 0a8bd21..989f87e 100644 --- a/core/modules/tour/lib/Drupal/tour/Tests/TourTest.php +++ b/core/modules/tour/lib/Drupal/tour/Tests/TourTest.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\tour\TourTest. + * Contains \Drupal\tour\Tests\TourTest. */ namespace Drupal\tour\Tests; @@ -20,33 +20,20 @@ class TourTest extends WebTestBase { * * @var array */ - public static $modules = array( - 'tour', - 'locale', - 'language', - 'tour_test' - ); + public static $modules = array('tour', 'locale', 'language', 'tour_test'); - protected $webUser; - - /** - * Declares test information. - */ public static function getInfo() { return array( - 'name' => 'Tour tests', - 'description' => "Test the functionality of tour tooltips.", - 'group' => 'Tour' + 'name' => 'Tour tests', + 'description' => 'Test the functionality of tour tooltips.', + 'group' => 'Tour', ); } - /** - * Sets up the test environment. - */ - public function setUp() { + protected function setUp() { parent::setUp(); - $this->webUser = $this->drupalCreateUser(array('access tour', 'administer languages')); - $this->drupalLogin($this->webUser); + + $this->drupalLogin($this->drupalCreateUser(array('access tour', 'administer languages'))); } /** @@ -67,9 +54,6 @@ public function testTourFunctionality() { // Enable Italian language and navigate to it/tour-test1 and verify italian // version of tip is found. language_save(new Language(array('langcode' => 'it'))); - drupal_static_reset(); - entity_info_cache_clear(); - menu_router_rebuild(); $this->drupalGet('it/tour-test-1'); $this->assertText('La pioggia cade in spagna'); @@ -79,12 +63,9 @@ public function testTourFunctionality() { // Revert back to english. language_save(new Language(array('langcode' => 'en'))); - drupal_static_reset(); - entity_info_cache_clear(); - menu_router_rebuild(); // Create a new tip (tip-3) visible on tour-test-1 with low weight. - $code_tip = array( + entity_create('tour_tooltip', array( 'id' => 'tour-code-test-1', 'label' => 'The rain in spain', 'langcode' => 'en', @@ -98,14 +79,13 @@ public function testTourFunctionality() { 'data-text' => 'Next', 'class' => 'custom', ), - ); - entity_create('tour_tooltip', $code_tip); + )); // Load it back from the database and verify storage worked. $this->drupalGet('tour-test-1'); // Create a new tip (tip-4) visible on tour-test-1 with low weight. - $tip4 = entity_create('tour_tooltip', array( + entity_create('tour_tooltip', array( 'id' => 'tip4', 'label' => 'Tip 4', 'langcode' => 'en', @@ -161,6 +141,7 @@ public function testTourFunctionality() { // Navigate to tour-test-3 and verify the TestTour plugin provides expected // markup. $this->drupalGet('tour-test-3'); - $this->assertText('this is the output of TourTest plugin'); + $this->assertText('This is the output of TourTest plugin'); } + } diff --git a/core/modules/tour/lib/Drupal/tour/TourBase.php b/core/modules/tour/lib/Drupal/tour/TourBase.php index ddb63e5..7735e10 100644 --- a/core/modules/tour/lib/Drupal/tour/TourBase.php +++ b/core/modules/tour/lib/Drupal/tour/TourBase.php @@ -9,12 +9,12 @@ use Drupal\Component\Plugin\PluginBase; use Drupal\tour\TourInterface; -use Drupal\tour\TourPluginInterface; /** * Defines a base tour implementation. */ abstract class TourBase extends PluginBase implements TourInterface { + /** * Weight of the tour item. * @@ -37,6 +37,13 @@ protected $paths = array(); /** + * The configuration entity for this tour. + * + * @var \Drupal\tour\Plugin\Core\Entity\TourTooltip + */ + public $entity; + + /** * Attributes for this tour. * * @var array @@ -58,11 +65,6 @@ public function getAttributes() { } /** - * Implements \Drupal\tour\TourInterface::getOutput(). - */ - public function getOutput() {} - - /** * Implements \Drupal\tour\TourInterface::setAttribute(). */ public function setAttribute($key, $value) { diff --git a/core/modules/tour/lib/Drupal/tour/TourInterface.php b/core/modules/tour/lib/Drupal/tour/TourInterface.php index 10fb680..c8472c8 100644 --- a/core/modules/tour/lib/Drupal/tour/TourInterface.php +++ b/core/modules/tour/lib/Drupal/tour/TourInterface.php @@ -1,4 +1,5 @@ discovery = new DerivativeDiscoveryDecorator(new AnnotatedClassDiscovery('tour', 'tour')); - $this->discovery = new AlterDecorator($this->discovery, 'tour_info'); + $this->discovery = new AnnotatedClassDiscovery('tour', 'tour'); + $this->discovery = new DerivativeDiscoveryDecorator($this->discovery); $this->discovery = new CacheDecorator($this->discovery, 'tour'); $this->factory = new DefaultFactory($this->discovery); } /** - * Overrides \Drupal\Component\Plugin\PluginManagerBase::createInstance(). - */ - public function createInstance($plugin_id, array $configuration = array(), Tour $entity = NULL) { - $plugin_class = DefaultFactory::getPluginClass($plugin_id, $this->discovery); - return new $plugin_class($configuration, $plugin_id, $this->discovery, $entity); - } - - /** - * Get all options for a given path. + * Gets all options for a given path. * * @param string $path - * The path for which the tour is valid. + * (optional) The path for which the tour is valid. Defaults to ''. * * @return array - * Array of tour elements + * Array of tour elements. */ public function getByPath($path = '') { $container = drupal_container(); - $language_content = language(LANGUAGE_TYPE_CONTENT); - $cid = 'tour:' . $language_content->langcode . ':' . $path; + $langcode = language(LANGUAGE_TYPE_CONTENT)->langcode; + $cid = 'tour:' . $langcode . ':' . $path; if ($cache = cache('cache_tour')->get($cid)) { return $cache->data; } @@ -63,9 +53,8 @@ public function getByPath($path = '') { // We need to load up the instance as the paths also live in here. $item = $this->createInstance($key, $definition); $pages = implode("\n", $item->getPaths()); - if ((drupal_match_path($path_alias, $pages) || - (($path != $path_alias) && drupal_match_path($path, $pages))) && - $item->getLanguage() == $language_content->langcode) { + $path_match = drupal_match_path($path_alias, $pages) || (($path != $path_alias) && drupal_match_path($path, $pages)); + if ($path_match && $item->getLanguage() == $langcode) { $tours[$key] = $item; } } diff --git a/core/modules/tour/tests/tour_test/lib/Drupal/tour_test/Plugin/tour/tour/TestTour.php b/core/modules/tour/tests/tour_test/lib/Drupal/tour_test/Plugin/tour/tour/TestTour.php index 84ab0da..f0a041f 100644 --- a/core/modules/tour/tests/tour_test/lib/Drupal/tour_test/Plugin/tour/tour/TestTour.php +++ b/core/modules/tour/tests/tour_test/lib/Drupal/tour_test/Plugin/tour/tour/TestTour.php @@ -1,4 +1,5 @@ t('This is the output of TourTest plugin'), + ); } + } diff --git a/core/modules/tour/tests/tour_test/tour_test.info b/core/modules/tour/tests/tour_test/tour_test.info index 34fd8cd..afc405d 100644 --- a/core/modules/tour/tests/tour_test/tour_test.info +++ b/core/modules/tour/tests/tour_test/tour_test.info @@ -1,6 +1,7 @@ -name = Tests for tour module -description = Tests for tour module -hidden = TRUE -core = 8.x +name = Tour module tests +description = Tests module for tour module. package = Core +version = VERSION +core = 8.x +hidden = TRUE dependencies[] = tour diff --git a/core/modules/tour/tests/tour_test/tour_test.module b/core/modules/tour/tests/tour_test/tour_test.module index 3abdf1d..cddca99 100644 --- a/core/modules/tour/tests/tour_test/tour_test.module +++ b/core/modules/tour/tests/tour_test/tour_test.module @@ -1,4 +1,5 @@ id() == 'tip4') { - $entity->label = $entity->label . ' alter'; + $entity->set('label', $entity->label() . ' alter'); } } @@ -49,10 +50,20 @@ function tour_test_tour_tooltip_presave($entity) { */ function tour_test_1() { return array( - array( - '#markup' => '
Where does the rain in Spain fail?
' . - '
Tip created later?
' - ) + 'tip-1' => array( + '#type' => 'container', + '#attributes' => array( + 'id' => 'tour-test-1', + ), + '#children' => t('Where does the rain in Spain fail?'), + ), + 'tip-4' => array( + '#type' => 'container', + '#attributes' => array( + 'id' => 'tour-test-4', + ), + '#children' => t('Tip created later?'), + ), ); } @@ -61,9 +72,11 @@ function tour_test_1() { */ function tour_test_2() { return array( - array( - '#markup' => '
Pangram example
' - ) + '#type' => 'container', + '#attributes' => array( + 'id' => 'tour-test-2', + ), + '#children' => t('Pangram example'), ); } @@ -72,17 +85,19 @@ function tour_test_2() { */ function tour_test_3() { return array( - array( - '#markup' => '
Third test
' - ) + '#type' => 'container', + '#attributes' => array( + 'id' => 'tour-test-3', + ), + '#children' => t('Third test'), ); } /** * Implements hook_tour_items_alter(). */ -function tour_tour_items_alter($tour_items, $path) { +function tour_tour_items_alter(array &$tour_items, $path) { if (!empty($tour_items['tooltip:tip4'])) { - $tour_items['tooltip:tip4']->entity->body = t('Altered by hook_tour_items_alter'); + $tour_items['tooltip:tip4']->entity->set('body', t('Altered by hook_tour_items_alter')); } } diff --git a/core/modules/tour/tour.api.php b/core/modules/tour/tour.api.php index f5be1cb..8a8fda2 100644 --- a/core/modules/tour/tour.api.php +++ b/core/modules/tour/tour.api.php @@ -1,23 +1,20 @@ entity->label = t('Search'); + $tour_items['tooltip:search-en']->entity->set('label', t('Search')); } } diff --git a/core/modules/tour/tour.info b/core/modules/tour/tour.info index 36425ad..77abd7b 100644 --- a/core/modules/tour/tour.info +++ b/core/modules/tour/tour.info @@ -1,4 +1,5 @@ name = Tour description = Provides a guided tour of the Drupal interface. -core = 8.x package = Core +version = VERSION +core = 8.x diff --git a/core/modules/tour/tour.install b/core/modules/tour/tour.install index adb809a..de5cc44 100644 --- a/core/modules/tour/tour.install +++ b/core/modules/tour/tour.install @@ -1,6 +1,11 @@ id . '-label">' . check_plain($tooltip->label()) . ' -

' . filter_xss_admin($tooltip->body) . '

'; + return '

' . check_plain($tooltip->label()) . '

+

' . filter_xss_admin($tooltip->get('body')) . '

'; } /** - * Implements hook_preprocess_page(). + * Implements hook_preprocess_HOOK() for page.tpl.php. */ function tour_preprocess_page(&$variables) { - $manager = drupal_container()->get('plugin.manager.tour'); - $tour_items = $manager->getByPath(current_path()); + $tour_items = drupal_container()->get('plugin.manager.tour')->getByPath(current_path()); if (empty($tour_items)) { return; } // Sort and set the last tooltip to show "End tour". - uasort($tour_items, 'tour_sort'); + uasort($tour_items, function ($a, $b) { + if ($a->weight == $b->weight) { + return 0; + } + return ($a->weight < $b->weight) ? -1 : 1; + }); $last = end($tour_items); $last->setAttribute('data-text', t('End tour')); $index = 1; $count = count($tour_items); foreach ($tour_items as $tour_item) { - $counter = '
(' . t('!tour_item of !total', array( - '!tour_item' => $index, - '!total' => $count - )) . ')
'; $list_items[] = array( - "#markup" => $tour_item->getOutput() . $counter, - "#wrapper_attributes" => $tour_item->getAttributes(), + 'output' => $tour_item->getOutput(), + 'counter' => array( + '#type' => 'container', + '#attributes' => array( + 'class' => array( + 'tour-progress', + ), + ), + '#children' => t('!tour_item of !total', array('!tour_item' => $index, '!total' => $count)), + ), + '#wrapper_attributes' => $tour_item->getAttributes(), ); $index++; } - // Re-use built-in sort from ConfigEntityBase. - drupal_add_library('tour', 'jquery.joyride'); - $variables['page']['help']['tour'] = array( - "#title" => "", - "#type" => "ol", - "#attributes" => array( - "id" => "tour-items", - "hidden" => TRUE, + '#theme' => 'item_list', + '#items' => $list_items, + '#type' => 'ol', + '#attributes' => array( + 'id' => 'tour-items', + 'hidden' => TRUE, + ), + '#attached' => array( + 'library' => array( + array('tour', 'jquery.joyride'), + ), ), - "#theme" => "item_list", - "#items" => $list_items, ); } @@ -176,13 +183,3 @@ function tour_tour_tooltip_update($entities) { drupal_container()->get('plugin.manager.tour')->clearCachedDefinitions(); cache('cache_tour')->deleteTags(array('tour')); } - -/** - * Helper function to sort tooltips. - */ -function tour_sort($a, $b) { - if ($a->weight == $b->weight) { - return 0; - } - return ($a->weight < $b->weight) ? -1 : 1; -}