Mercurial > hg > isophonics-drupal-site
comparison core/modules/tour/src/Entity/Tour.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\tour\Entity; | |
4 | |
5 use Drupal\Core\Config\Entity\ConfigEntityBase; | |
6 use Drupal\tour\TipsPluginCollection; | |
7 use Drupal\tour\TourInterface; | |
8 | |
9 /** | |
10 * Defines the configured tour entity. | |
11 * | |
12 * @ConfigEntityType( | |
13 * id = "tour", | |
14 * label = @Translation("Tour"), | |
15 * handlers = { | |
16 * "view_builder" = "Drupal\tour\TourViewBuilder", | |
17 * "access" = "Drupal\tour\TourAccessControlHandler", | |
18 * }, | |
19 * admin_permission = "administer site configuration", | |
20 * entity_keys = { | |
21 * "id" = "id", | |
22 * "label" = "label" | |
23 * }, | |
24 * config_export = { | |
25 * "id", | |
26 * "label", | |
27 * "module", | |
28 * "routes", | |
29 * "tips", | |
30 * }, | |
31 * lookup_keys = { | |
32 * "routes.*.route_name" | |
33 * } | |
34 * ) | |
35 */ | |
36 class Tour extends ConfigEntityBase implements TourInterface { | |
37 | |
38 /** | |
39 * The name (plugin ID) of the tour. | |
40 * | |
41 * @var string | |
42 */ | |
43 protected $id; | |
44 | |
45 /** | |
46 * The module which this tour is assigned to. | |
47 * | |
48 * @var string | |
49 */ | |
50 protected $module; | |
51 | |
52 /** | |
53 * The label of the tour. | |
54 * | |
55 * @var string | |
56 */ | |
57 protected $label; | |
58 | |
59 /** | |
60 * The routes on which this tour should be displayed. | |
61 * | |
62 * @var array | |
63 */ | |
64 protected $routes = []; | |
65 | |
66 /** | |
67 * The routes on which this tour should be displayed, keyed by route id. | |
68 * | |
69 * @var array | |
70 */ | |
71 protected $keyedRoutes; | |
72 | |
73 /** | |
74 * Holds the collection of tips that are attached to this tour. | |
75 * | |
76 * @var \Drupal\tour\TipsPluginCollection | |
77 */ | |
78 protected $tipsCollection; | |
79 | |
80 /** | |
81 * The array of plugin config, only used for export and to populate the $tipsCollection. | |
82 * | |
83 * @var array | |
84 */ | |
85 protected $tips = []; | |
86 | |
87 /** | |
88 * {@inheritdoc} | |
89 */ | |
90 public function __construct(array $values, $entity_type) { | |
91 parent::__construct($values, $entity_type); | |
92 | |
93 $this->tipsCollection = new TipsPluginCollection(\Drupal::service('plugin.manager.tour.tip'), $this->tips); | |
94 } | |
95 | |
96 /** | |
97 * {@inheritdoc} | |
98 */ | |
99 public function getRoutes() { | |
100 return $this->routes; | |
101 } | |
102 | |
103 /** | |
104 * {@inheritdoc} | |
105 */ | |
106 public function getTip($id) { | |
107 return $this->tipsCollection->get($id); | |
108 } | |
109 | |
110 /** | |
111 * {@inheritdoc} | |
112 */ | |
113 public function getTips() { | |
114 $tips = []; | |
115 foreach ($this->tips as $id => $tip) { | |
116 $tips[] = $this->getTip($id); | |
117 } | |
118 uasort($tips, function ($a, $b) { | |
119 if ($a->getWeight() == $b->getWeight()) { | |
120 return 0; | |
121 } | |
122 return ($a->getWeight() < $b->getWeight()) ? -1 : 1; | |
123 }); | |
124 | |
125 \Drupal::moduleHandler()->alter('tour_tips', $tips, $this); | |
126 return array_values($tips); | |
127 } | |
128 | |
129 /** | |
130 * {@inheritdoc} | |
131 */ | |
132 public function getModule() { | |
133 return $this->module; | |
134 } | |
135 | |
136 /** | |
137 * {@inheritdoc} | |
138 */ | |
139 public function hasMatchingRoute($route_name, $route_params) { | |
140 if (!isset($this->keyedRoutes)) { | |
141 $this->keyedRoutes = []; | |
142 foreach ($this->getRoutes() as $route) { | |
143 $this->keyedRoutes[$route['route_name']] = isset($route['route_params']) ? $route['route_params'] : []; | |
144 } | |
145 } | |
146 if (!isset($this->keyedRoutes[$route_name])) { | |
147 // We don't know about this route. | |
148 return FALSE; | |
149 } | |
150 if (empty($this->keyedRoutes[$route_name])) { | |
151 // We don't need to worry about route params, the route name is enough. | |
152 return TRUE; | |
153 } | |
154 foreach ($this->keyedRoutes[$route_name] as $key => $value) { | |
155 // If a required param is missing or doesn't match, return FALSE. | |
156 if (empty($route_params[$key]) || $route_params[$key] !== $value) { | |
157 return FALSE; | |
158 } | |
159 } | |
160 return TRUE; | |
161 } | |
162 | |
163 /** | |
164 * {@inheritdoc} | |
165 */ | |
166 public function resetKeyedRoutes() { | |
167 unset($this->keyedRoutes); | |
168 } | |
169 | |
170 /** | |
171 * {@inheritdoc} | |
172 */ | |
173 public function calculateDependencies() { | |
174 parent::calculateDependencies(); | |
175 | |
176 foreach ($this->tipsCollection as $instance) { | |
177 $definition = $instance->getPluginDefinition(); | |
178 $this->addDependency('module', $definition['provider']); | |
179 } | |
180 | |
181 $this->addDependency('module', $this->module); | |
182 return $this; | |
183 } | |
184 | |
185 } |