class RngEventRouteContext in RNG - Events and Registrations 8
Same name and namespace in other branches
- 8.2 src/ContextProvider/RngEventRouteContext.php \Drupal\rng\ContextProvider\RngEventRouteContext
- 3.x src/ContextProvider/RngEventRouteContext.php \Drupal\rng\ContextProvider\RngEventRouteContext
Get the current RNG event from the route.
Hierarchy
- class \Drupal\rng\ContextProvider\RngEventRouteContext implements ContextProviderInterface uses StringTranslationTrait
Expanded class hierarchy of RngEventRouteContext
1 string reference to 'RngEventRouteContext'
1 service uses RngEventRouteContext
File
- src/
ContextProvider/ RngEventRouteContext.php, line 17
Namespace
Drupal\rng\ContextProviderView source
class RngEventRouteContext implements ContextProviderInterface {
use StringTranslationTrait;
/**
* The route match object.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The RNG event manager.
*
* @var \Drupal\rng\EventManagerInterface
*/
protected $eventManager;
/**
* Constructs a new RngEventRouteContext.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match object.
* @param \Drupal\rng\EventManagerInterface $event_manager
* The RNG event manager.
*/
public function __construct(RouteMatchInterface $route_match, EventManagerInterface $event_manager) {
$this->routeMatch = $route_match;
$this->eventManager = $event_manager;
}
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids) {
$cacheability = new CacheableMetadata();
$cacheability
->setCacheContexts([
'rng_event',
]);
$context_definition = new ContextDefinition('entity', NULL, FALSE);
$context = new Context($context_definition, $this
->getEventInRoute());
$context
->addCacheableDependency($cacheability);
return [
'rng_event' => $context,
];
}
/**
* {@inheritdoc}
*/
public function getAvailableContexts() {
$context = new Context(new ContextDefinition('entity', $this
->t('RNG event from route')));
return [
'rng_event' => $context,
];
}
/**
* Determine the event in the current route.
*
* @return \Drupal\Core\Entity\EntityInterface|NULL
* The event entity, or NULL.
*/
protected function getEventInRoute() {
// Will be NULL in CLI.
if (!($route = $this->routeMatch
->getRouteObject())) {
return NULL;
}
if ($event_param = $route
->getDefault('event')) {
$event = $this->routeMatch
->getParameter($event_param);
return $event instanceof EntityInterface ? $event : NULL;
}
if ($events = $this
->getEventEntitiesInRoute()) {
// There could be multiple events in a route, determine which event
// is the correct one.
foreach ($events as $entity) {
// Exact link templates
foreach ($entity
->getEntityType()
->getLinkTemplates() as $link_template => $path) {
if ($route
->getPath() === $path) {
return $entity;
}
}
// Or this route is a sub string of canonical.
if ($canonical_path = $entity
->getEntityType()
->getLinkTemplate('canonical')) {
if (strpos($route
->getPath(), $canonical_path) === 0) {
return $entity;
}
}
}
}
return NULL;
}
/**
* Get all event entities from the current route.
*
* @return \Drupal\Core\Entity\EntityInterface[]
* An array of event entities in route.
*/
protected function getEventEntitiesInRoute() {
$events = [];
if (($route = $this->routeMatch
->getRouteObject()) && ($parameters = $route
->getOption('parameters'))) {
foreach ($parameters as $parameter) {
if (isset($parameter['type']) && strpos($parameter['type'], 'entity:') !== FALSE) {
$entity_type_id = substr($parameter['type'], strlen('entity:'));
$entity = $this->routeMatch
->getParameter($entity_type_id);
if ($entity instanceof EntityInterface && $this->eventManager
->isEvent($entity)) {
$events[] = $entity;
}
}
}
}
return $events;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RngEventRouteContext:: |
protected | property | The RNG event manager. | |
RngEventRouteContext:: |
protected | property | The route match object. | |
RngEventRouteContext:: |
public | function |
Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface:: |
|
RngEventRouteContext:: |
protected | function | Get all event entities from the current route. | |
RngEventRouteContext:: |
protected | function | Determine the event in the current route. | |
RngEventRouteContext:: |
public | function |
Gets runtime context values for the given context IDs. Overrides ContextProviderInterface:: |
|
RngEventRouteContext:: |
public | function | Constructs a new RngEventRouteContext. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |