class NodeRouteContext in Drupal 10
Same name and namespace in other branches
- 8 core/modules/node/src/ContextProvider/NodeRouteContext.php \Drupal\node\ContextProvider\NodeRouteContext
- 9 core/modules/node/src/ContextProvider/NodeRouteContext.php \Drupal\node\ContextProvider\NodeRouteContext
Sets the current node as a context on node routes.
Hierarchy
- class \Drupal\node\ContextProvider\NodeRouteContext implements ContextProviderInterface uses StringTranslationTrait
Expanded class hierarchy of NodeRouteContext
1 string reference to 'NodeRouteContext'
- node.services.yml in core/
modules/ node/ node.services.yml - core/modules/node/node.services.yml
1 service uses NodeRouteContext
- node.node_route_context in core/
modules/ node/ node.services.yml - Drupal\node\ContextProvider\NodeRouteContext
File
- core/
modules/ node/ src/ ContextProvider/ NodeRouteContext.php, line 17
Namespace
Drupal\node\ContextProviderView source
class NodeRouteContext implements ContextProviderInterface {
use StringTranslationTrait;
/**
* The route match object.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Constructs a new NodeRouteContext.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match object.
*/
public function __construct(RouteMatchInterface $route_match) {
$this->routeMatch = $route_match;
}
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids) {
$result = [];
$context_definition = EntityContextDefinition::create('node')
->setRequired(FALSE);
$value = NULL;
if ($route_object = $this->routeMatch
->getRouteObject()) {
$route_contexts = $route_object
->getOption('parameters');
// Check for a node revision parameter first.
if (isset($route_contexts['node_revision']) && ($revision = $this->routeMatch
->getParameter('node_revision'))) {
$value = $revision;
}
elseif (isset($route_contexts['node']) && ($node = $this->routeMatch
->getParameter('node'))) {
$value = $node;
}
elseif (isset($route_contexts['node_preview']) && ($node = $this->routeMatch
->getParameter('node_preview'))) {
$value = $node;
}
elseif ($this->routeMatch
->getRouteName() == 'node.add') {
$node_type = $this->routeMatch
->getParameter('node_type');
$value = Node::create([
'type' => $node_type
->id(),
]);
}
}
$cacheability = new CacheableMetadata();
$cacheability
->setCacheContexts([
'route',
]);
$context = new Context($context_definition, $value);
$context
->addCacheableDependency($cacheability);
$result['node'] = $context;
return $result;
}
/**
* {@inheritdoc}
*/
public function getAvailableContexts() {
$context = EntityContext::fromEntityTypeId('node', $this
->t('Node from URL'));
return [
'node' => $context,
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
NodeRouteContext:: |
protected | property | The route match object. | |
NodeRouteContext:: |
public | function |
Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface:: |
|
NodeRouteContext:: |
public | function |
Gets runtime context values for the given context IDs. Overrides ContextProviderInterface:: |
|
NodeRouteContext:: |
public | function | Constructs a new NodeRouteContext. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 3 |
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. | 1 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |