Tour.php 3.69 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\tour\Entity\Tour.
6
7
 */

8
namespace Drupal\tour\Entity;
9
10
11

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\tour\TipsBag;
12
use Drupal\tour\TourInterface;
13
14

/**
15
 * Defines the configured tour entity.
16
 *
17
 * @EntityType(
18
19
 *   id = "tour",
 *   label = @Translation("Tour"),
20
21
 *   controllers = {
 *     "storage" = "Drupal\Core\Config\Entity\ConfigStorageController",
22
 *     "view_builder" = "Drupal\tour\TourViewBuilder"
23
 *   },
24
25
26
27
 *   config_prefix = "tour.tour",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
28
 *     "uuid" = "uuid"
29
30
31
 *   }
 * )
 */
32
class Tour extends ConfigEntityBase implements TourInterface {
33
34
35
36
37
38
39
40

  /**
   * The name (plugin ID) of the tour.
   *
   * @var string
   */
  public $id;

41
42
43
44
45
46
47
  /**
   * The module which this tour is assigned to.
   *
   * @var string
   */
  public $module;

48
49
50
51
52
53
54
55
  /**
   * The label of the tour.
   *
   * @var string
   */
  public $label;

  /**
56
   * The routes on which this tour should be displayed.
57
58
59
   *
   * @var array
   */
60
61
62
63
64
65
66
67
  protected $routes = array();

  /**
   * The routes on which this tour should be displayed, keyed by route id.
   *
   * @var array
   */
  protected $keyedRoutes;
68
69
70
71
72
73
74
75
76
77
78
79
80

  /**
   * Holds the collection of tips that are attached to this tour.
   *
   * @var \Drupal\tour\TipsBag
   */
  protected $tipsBag;

  /**
   * The array of plugin config, only used for export and to populate the $tipsBag.
   *
   * @var array
   */
81
  protected $tips = array();
82
83
84
85
86
87
88

  /**
   * Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::__construct();
   */
  public function __construct(array $values, $entity_type) {
    parent::__construct($values, $entity_type);

89
    $this->tipsBag = new TipsBag(\Drupal::service('plugin.manager.tour.tip'), $this->tips);
90
91
92
  }

  /**
93
   * {@inheritdoc}
94
   */
95
96
  public function getRoutes() {
    return $this->routes;
97
98
99
  }

  /**
100
   * {@inheritdoc}
101
102
103
104
105
106
   */
  public function getTip($id) {
    return $this->tipsBag->get($id);
  }

  /**
107
   * {@inheritdoc}
108
   */
109
110
111
112
113
114
115
116
117
118
119
120
  public function getTips() {
    $tips = array();
    foreach ($this->tips as $id => $tip) {
      $tips[] = $this->getTip($id);
    }
    uasort($tips, function ($a, $b) {
      if ($a->getWeight() == $b->getWeight()) {
        return 0;
      }
      return ($a->getWeight() < $b->getWeight()) ? -1 : 1;
    });

121
    \Drupal::moduleHandler()->alter('tour_tips', $tips, $this);
122
    return array_values($tips);
123
124
125
126
127
128
129
130
  }

  /**
   * Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::getExportProperties();
   */
  public function getExportProperties() {
    $properties = parent::getExportProperties();
    $names = array(
131
      'routes',
132
133
134
135
136
137
138
      'tips',
    );
    foreach ($names as $name) {
      $properties[$name] = $this->get($name);
    }
    return $properties;
  }
139

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  /**
   * {@inheritdoc}
   */
  public function hasMatchingRoute($route_name, $route_params) {
    if (!isset($this->keyedRoutes)) {
      $this->keyedRoutes = array();
      foreach ($this->getRoutes() as $route) {
        $this->keyedRoutes[$route['route_name']] = isset($route['route_params']) ? $route['route_params'] : array();
      }
    }
    if (!isset($this->keyedRoutes[$route_name])) {
      // We don't know about this route.
      return FALSE;
    }
    if (empty($this->keyedRoutes[$route_name])) {
      // We don't need to worry about route params, the route name is enough.
      return TRUE;
    }
    foreach ($this->keyedRoutes[$route_name] as $key => $value) {
      // If a required param is missing or doesn't match, return FALSE.
      if (empty($route_params[$key]) || $route_params[$key] !== $value) {
        return FALSE;
      }
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function resetKeyedRoutes() {
    unset($this->keyedRoutes);
  }

174
}