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"),
16 * "view_builder" = "Drupal\tour\TourViewBuilder",
17 * "access" = "Drupal\tour\TourAccessControlHandler",
19 * admin_permission = "administer site configuration",
32 * "routes.*.route_name"
36 class Tour extends ConfigEntityBase implements TourInterface {
39 * The name (plugin ID) of the tour.
46 * The module which this tour is assigned to.
53 * The label of the tour.
60 * The routes on which this tour should be displayed.
64 protected $routes = [];
67 * The routes on which this tour should be displayed, keyed by route id.
71 protected $keyedRoutes;
74 * Holds the collection of tips that are attached to this tour.
76 * @var \Drupal\tour\TipsPluginCollection
78 protected $tipsCollection;
81 * The array of plugin config, only used for export and to populate the $tipsCollection.
90 public function __construct(array $values, $entity_type) {
91 parent::__construct($values, $entity_type);
93 $this->tipsCollection = new TipsPluginCollection(\Drupal::service('plugin.manager.tour.tip'), $this->tips);
99 public function getRoutes() {
100 return $this->routes;
106 public function getTip($id) {
107 return $this->tipsCollection->get($id);
113 public function getTips() {
115 foreach ($this->tips as $id => $tip) {
116 $tips[] = $this->getTip($id);
118 uasort($tips, function ($a, $b) {
119 if ($a->getWeight() == $b->getWeight()) {
122 return ($a->getWeight() < $b->getWeight()) ? -1 : 1;
125 \Drupal::moduleHandler()->alter('tour_tips', $tips, $this);
126 return array_values($tips);
132 public function getModule() {
133 return $this->module;
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'] : [];
146 if (!isset($this->keyedRoutes[$route_name])) {
147 // We don't know about this route.
150 if (empty($this->keyedRoutes[$route_name])) {
151 // We don't need to worry about route params, the route name is enough.
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) {
166 public function resetKeyedRoutes() {
167 unset($this->keyedRoutes);
173 public function calculateDependencies() {
174 parent::calculateDependencies();
176 foreach ($this->tipsCollection as $instance) {
177 $definition = $instance->getPluginDefinition();
178 $this->addDependency('module', $definition['provider']);
181 $this->addDependency('module', $this->module);