annotate core/modules/tour/src/Plugin/HelpSection/TourHelpSection.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\tour\Plugin\HelpSection;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@0 6 use Drupal\Core\Link;
Chris@0 7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
Chris@0 8 use Drupal\Core\Url;
Chris@0 9 use Drupal\help\Plugin\HelpSection\HelpSectionPluginBase;
Chris@0 10 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Provides the tours list section for the help page.
Chris@0 14 *
Chris@0 15 * @HelpSection(
Chris@0 16 * id = "tour",
Chris@0 17 * title = @Translation("Tours"),
Chris@0 18 * 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:"),
Chris@0 19 * permission = "access tour"
Chris@0 20 * )
Chris@0 21 */
Chris@0 22 class TourHelpSection extends HelpSectionPluginBase implements ContainerFactoryPluginInterface {
Chris@0 23
Chris@0 24 /**
Chris@0 25 * The entity type manager.
Chris@0 26 *
Chris@0 27 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@0 28 */
Chris@0 29 protected $entityTypeManager;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * Constructs a TourHelpSection object.
Chris@0 33 *
Chris@0 34 * @param array $configuration
Chris@0 35 * A configuration array containing information about the plugin instance.
Chris@0 36 * @param string $plugin_id
Chris@0 37 * The plugin_id for the plugin instance.
Chris@0 38 * @param mixed $plugin_definition
Chris@0 39 * The plugin implementation definition.
Chris@0 40 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@0 41 * The entity manager service.
Chris@0 42 */
Chris@0 43 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
Chris@0 44 parent::__construct($configuration, $plugin_id, $plugin_definition);
Chris@0 45 $this->entityTypeManager = $entity_type_manager;
Chris@0 46 }
Chris@0 47
Chris@0 48 /**
Chris@0 49 * {@inheritdoc}
Chris@0 50 */
Chris@0 51 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
Chris@0 52 return new static(
Chris@0 53 $configuration,
Chris@0 54 $plugin_id,
Chris@0 55 $plugin_definition,
Chris@0 56 $container->get('entity_type.manager')
Chris@0 57 );
Chris@0 58 }
Chris@0 59
Chris@0 60 /**
Chris@0 61 * {@inheritdoc}
Chris@0 62 */
Chris@0 63 public function getCacheMaxAge() {
Chris@0 64 // The calculation of which URL (if any) gets put on which tour depends
Chris@0 65 // on a route access check. This can have a lot of inputs, including user
Chris@0 66 // permissions and other factors. Rather than doing a complicated
Chris@0 67 // accounting of the cache metadata for all of these possible factors, set
Chris@0 68 // the max age of the cache to zero to prevent using incorrect cached
Chris@0 69 // information.
Chris@0 70 return 0;
Chris@0 71 }
Chris@0 72
Chris@0 73 /**
Chris@0 74 * {@inheritdoc}
Chris@0 75 */
Chris@0 76 public function listTopics() {
Chris@0 77 /** @var \Drupal\tour\TourInterface[] $tours */
Chris@0 78 $tours = $this->entityTypeManager->getStorage('tour')->loadMultiple();
Chris@0 79 // Sort in the manner defined by Tour.
Chris@0 80 uasort($tours, ['Drupal\tour\Entity\Tour', 'sort']);
Chris@0 81
Chris@0 82 // Make a link to each tour, using the first of its routes that can
Chris@0 83 // be linked to by this user, if any.
Chris@0 84 $topics = [];
Chris@0 85 foreach ($tours as $tour) {
Chris@0 86 $title = $tour->label();
Chris@0 87 $id = $tour->id();
Chris@0 88 $routes = $tour->getRoutes();
Chris@0 89 $made_link = FALSE;
Chris@0 90 foreach ($routes as $route) {
Chris@0 91 // Some tours are for routes with parameters. For instance, there is
Chris@0 92 // currently a tour in the Language module for the language edit page,
Chris@0 93 // which appears on all pages with URLs like:
Chris@0 94 // /admin/config/regional/language/edit/LANGCODE.
Chris@0 95 // There is no way to make a link to the page that displays the tour,
Chris@0 96 // because it is a set of pages. The easiest way to detect this is to
Chris@0 97 // use a try/catch exception -- try to make a link, and it will error
Chris@0 98 // out with a missing parameter exception if the route leads to a set
Chris@0 99 // of pages instead of a single page.
Chris@0 100 try {
Chris@0 101 $params = isset($route['route_params']) ? $route['route_params'] : [];
Chris@0 102 $url = Url::fromRoute($route['route_name'], $params);
Chris@0 103 // Skip this route if the current user cannot access it.
Chris@0 104 if (!$url->access()) {
Chris@0 105 continue;
Chris@0 106 }
Chris@0 107
Chris@0 108 // Generate the link HTML directly, using toString(), to catch
Chris@0 109 // missing parameter exceptions now instead of at render time.
Chris@0 110 $topics[$id] = Link::fromTextAndUrl($title, $url)->toString();
Chris@0 111 // If the line above didn't generate an exception, we have a good
Chris@0 112 // link that the user can access.
Chris@0 113 $made_link = TRUE;
Chris@0 114 break;
Chris@0 115 }
Chris@0 116 catch (\Exception $e) {
Chris@0 117 // Exceptions are normally due to routes that need parameters. If
Chris@0 118 // there is an exception, just try the next route and see if we can
Chris@0 119 // find one that will work for us.
Chris@0 120 }
Chris@0 121 }
Chris@0 122 if (!$made_link) {
Chris@0 123 // None of the routes worked to make a link, so at least display the
Chris@0 124 // tour title.
Chris@0 125 $topics[$id] = $title;
Chris@0 126 }
Chris@0 127 }
Chris@0 128
Chris@0 129 return $topics;
Chris@0 130 }
Chris@0 131
Chris@0 132 }