3 namespace Drupal\tour\Entity;
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
6 use Drupal\tour\TipsPluginCollection;
7 use Drupal\tour\TourInterface;
10 * Defines the configured tour entity.
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",
23 * "view_builder" = "Drupal\tour\TourViewBuilder",
24 * "access" = "Drupal\tour\TourAccessControlHandler",
26 * admin_permission = "administer site configuration",
39 * "routes.*.route_name"
43 class Tour extends ConfigEntityBase implements TourInterface {
46 * The name (plugin ID) of the tour.
53 * The module which this tour is assigned to.
60 * The label of the tour.
67 * The routes on which this tour should be displayed.
71 protected $routes = [];
74 * The routes on which this tour should be displayed, keyed by route id.
78 protected $keyedRoutes;
81 * Holds the collection of tips that are attached to this tour.
83 * @var \Drupal\tour\TipsPluginCollection
85 protected $tipsCollection;
88 * The array of plugin config, only used for export and to populate the $tipsCollection.
97 public function __construct(array $values, $entity_type) {
98 parent::__construct($values, $entity_type);
100 $this->tipsCollection = new TipsPluginCollection(\Drupal::service('plugin.manager.tour.tip'), $this->tips);
106 public function getRoutes() {
107 return $this->routes;
113 public function getTip($id) {
114 return $this->tipsCollection->get($id);
120 public function getTips() {
122 foreach ($this->tips as $id => $tip) {
123 $tips[] = $this->getTip($id);
125 uasort($tips, function ($a, $b) {
126 if ($a->getWeight() == $b->getWeight()) {
129 return ($a->getWeight() < $b->getWeight()) ? -1 : 1;
132 \Drupal::moduleHandler()->alter('tour_tips', $tips, $this);
133 return array_values($tips);
139 public function getModule() {
140 return $this->module;
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'] : [];
153 if (!isset($this->keyedRoutes[$route_name])) {
154 // We don't know about this route.
157 if (empty($this->keyedRoutes[$route_name])) {
158 // We don't need to worry about route params, the route name is enough.
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) {
173 public function resetKeyedRoutes() {
174 unset($this->keyedRoutes);
180 public function calculateDependencies() {
181 parent::calculateDependencies();
183 foreach ($this->tipsCollection as $instance) {
184 $definition = $instance->getPluginDefinition();
185 $this->addDependency('module', $definition['provider']);
188 $this->addDependency('module', $this->module);