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