Newer
Older
Dries Buytaert
committed
<?php
namespace Drupal\tour\Entity;
Dries Buytaert
committed
use Drupal\Core\Config\Entity\ConfigEntityBase;
Alex Pott
committed
use Drupal\tour\TipsPluginCollection;
use Drupal\tour\TourInterface;
Dries Buytaert
committed
/**
* Defines the configured tour entity.
Dries Buytaert
committed
*
Alex Pott
committed
* @ConfigEntityType(
Dries Buytaert
committed
* id = "tour",
* label = @Translation("Tour"),
Alex Pott
committed
* handlers = {
* "view_builder" = "Drupal\tour\TourViewBuilder",
* "access" = "Drupal\tour\TourAccessControlHandler",
* admin_permission = "administer site configuration",
Dries Buytaert
committed
* entity_keys = {
* "id" = "id",
Dries Buytaert
committed
* "label" = "label"
* },
* config_export = {
* "id",
* "label",
* "module",
* "routes",
* "tips",
catch
committed
* },
* lookup_keys = {
* "routes.*.route_name"
Dries Buytaert
committed
* }
* )
*/
class Tour extends ConfigEntityBase implements TourInterface {
Dries Buytaert
committed
/**
* The name (plugin ID) of the tour.
*
* @var string
*/
protected $id;
Dries Buytaert
committed
/**
* The module which this tour is assigned to.
*
* @var string
*/
protected $module;
Dries Buytaert
committed
/**
* The label of the tour.
*
* @var string
*/
protected $label;
Dries Buytaert
committed
/**
Alex Pott
committed
* The routes on which this tour should be displayed.
Dries Buytaert
committed
*
* @var array
*/
protected $routes = [];
Alex Pott
committed
/**
* The routes on which this tour should be displayed, keyed by route id.
*
* @var array
*/
protected $keyedRoutes;
Dries Buytaert
committed
/**
* Holds the collection of tips that are attached to this tour.
*
Alex Pott
committed
* @var \Drupal\tour\TipsPluginCollection
Dries Buytaert
committed
*/
Alex Pott
committed
protected $tipsCollection;
Dries Buytaert
committed
/**
Alex Pott
committed
* The array of plugin config, only used for export and to populate the $tipsCollection.
Dries Buytaert
committed
*
* @var array
*/
protected $tips = [];
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function __construct(array $values, $entity_type) {
parent::__construct($values, $entity_type);
Alex Pott
committed
$this->tipsCollection = new TipsPluginCollection(\Drupal::service('plugin.manager.tour.tip'), $this->tips);
Dries Buytaert
committed
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
Alex Pott
committed
public function getRoutes() {
return $this->routes;
Dries Buytaert
committed
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getTip($id) {
Alex Pott
committed
return $this->tipsCollection->get($id);
Dries Buytaert
committed
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
Dries Buytaert
committed
public function getTips() {
$tips = [];
Dries Buytaert
committed
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;
});
\Drupal::moduleHandler()->alter('tour_tips', $tips, $this);
Dries Buytaert
committed
return array_values($tips);
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
public function getModule() {
return $this->module;
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public function hasMatchingRoute($route_name, $route_params) {
if (!isset($this->keyedRoutes)) {
$this->keyedRoutes = [];
Alex Pott
committed
foreach ($this->getRoutes() as $route) {
$this->keyedRoutes[$route['route_name']] = isset($route['route_params']) ? $route['route_params'] : [];
Alex Pott
committed
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
}
}
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);
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
parent::calculateDependencies();
foreach ($this->tipsCollection as $instance) {
$definition = $instance->getPluginDefinition();
$this->addDependency('module', $definition['provider']);
}
$this->addDependency('module', $this->module);
return $this;
}