comparison core/modules/tour/src/Plugin/HelpSection/TourHelpSection.php @ 0:c75dbcec494b

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