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