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