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"
30 * "routes.*.route_name"
34 class Tour extends ConfigEntityBase implements TourInterface {
37 * The name (plugin ID) of the tour.
44 * The module which this tour is assigned to.
51 * The label of the tour.
58 * The routes on which this tour should be displayed.
62 protected $routes = [];
65 * The routes on which this tour should be displayed, keyed by route id.
69 protected $keyedRoutes;
72 * Holds the collection of tips that are attached to this tour.
74 * @var \Drupal\tour\TipsPluginCollection
76 protected $tipsCollection;
79 * The array of plugin config, only used for export and to populate the $tipsCollection.
88 public function __construct(array $values, $entity_type) {
89 parent::__construct($values, $entity_type);
91 $this->tipsCollection = new TipsPluginCollection(\Drupal::service('plugin.manager.tour.tip'), $this->tips);
97 public function getRoutes() {
104 public function getTip($id) {
105 return $this->tipsCollection->get($id);
111 public function getTips() {
113 foreach ($this->tips as $id => $tip) {
114 $tips[] = $this->getTip($id);
116 uasort($tips, function ($a, $b) {
117 if ($a->getWeight() == $b->getWeight()) {
120 return ($a->getWeight() < $b->getWeight()) ? -1 : 1;
123 \Drupal::moduleHandler()->alter('tour_tips', $tips, $this);
124 return array_values($tips);
130 public function getModule() {
131 return $this->module;
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'] : [];
144 if (!isset($this->keyedRoutes[$route_name])) {
145 // We don't know about this route.
148 if (empty($this->keyedRoutes[$route_name])) {
149 // We don't need to worry about route params, the route name is enough.
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) {
164 public function resetKeyedRoutes() {
165 unset($this->keyedRoutes);
171 public function calculateDependencies() {
172 parent::calculateDependencies();
174 foreach ($this->tipsCollection as $instance) {
175 $definition = $instance->getPluginDefinition();
176 $this->addDependency('module', $definition['provider']);
179 $this->addDependency('module', $this->module);