abstract class RouteAwareContextProviderBase in Core Context 8
Provides a base class for context providers which use the current route.
Hierarchy
- class \Drupal\core_context\ContextProvider\ContextProviderBase implements ContextProviderInterface
- class \Drupal\core_context\ContextProvider\RouteAwareContextProviderBase uses CacheableContextTrait
Expanded class hierarchy of RouteAwareContextProviderBase
File
- src/
ContextProvider/ RouteAwareContextProviderBase.php, line 13
Namespace
Drupal\core_context\ContextProviderView source
abstract class RouteAwareContextProviderBase extends ContextProviderBase {
use CacheableContextTrait;
/**
* The route match service.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* RouteAwareContextProviderBase constructor.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match service.
* @param mixed ...$arguments
* Additional arguments to pass to the parent constructor.
*/
public function __construct(RouteMatchInterface $route_match, ...$arguments) {
$this->routeMatch = $route_match;
parent::__construct(...$arguments);
}
/**
* Determines if this provider can extract contexts from the current route.
*
* @param \Symfony\Component\Routing\Route $route
* The route object.
*
* @return bool
* TRUE if this provider can extract contexts from the route, otherwise
* FALSE.
*/
protected abstract function appliesTo(Route $route) : bool;
/**
* Extracts contexts from the current route.
*
* @param \Symfony\Component\Routing\Route $route
* The route object.
*
* @return \Drupal\Component\Plugin\Context\ContextInterface[]
* The contexts extracted from the route, keyed by name. Any contexts which
* can accept cache metadata will get the 'route' cache context applied.
*/
protected abstract function getContextsFromRoute(Route $route) : array;
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids) {
$route = $this->routeMatch
->getRouteObject();
if (empty($route) || $this
->appliesTo($route) === FALSE) {
return [];
}
$contexts = $this
->getContextsFromRoute($route);
if ($unqualified_context_ids) {
$contexts = array_intersect_key($contexts, array_flip($unqualified_context_ids));
}
$cache_metadata = new CacheableMetadata();
$cache_metadata
->addCacheContexts([
'route',
]);
return $this
->applyCaching($contexts, $cache_metadata);
}
/**
* {@inheritdoc}
*/
public function getAvailableContexts() {
return $this
->getRuntimeContexts([]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableContextTrait:: |
private | function | Adds cache metadata to a set of contexts. | |
ContextProviderBase:: |
protected | property | The entity type manager service. | |
ContextProviderBase:: |
protected | function | Extracts contexts from an entity. | |
RouteAwareContextProviderBase:: |
protected | property | The route match service. | |
RouteAwareContextProviderBase:: |
abstract protected | function | Determines if this provider can extract contexts from the current route. | 2 |
RouteAwareContextProviderBase:: |
public | function |
Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface:: |
|
RouteAwareContextProviderBase:: |
abstract protected | function | Extracts contexts from the current route. | 2 |
RouteAwareContextProviderBase:: |
public | function |
Gets runtime context values for the given context IDs. Overrides ContextProviderInterface:: |
|
RouteAwareContextProviderBase:: |
public | function |
RouteAwareContextProviderBase constructor. Overrides ContextProviderBase:: |
2 |