Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\node\ContextProvider;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\CacheableMetadata;
|
Chris@0
|
6 use Drupal\Core\Plugin\Context\Context;
|
Chris@0
|
7 use Drupal\Core\Plugin\Context\ContextProviderInterface;
|
Chris@17
|
8 use Drupal\Core\Plugin\Context\EntityContext;
|
Chris@17
|
9 use Drupal\Core\Plugin\Context\EntityContextDefinition;
|
Chris@0
|
10 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
11 use Drupal\node\Entity\Node;
|
Chris@0
|
12 use Drupal\Core\StringTranslation\StringTranslationTrait;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Sets the current node as a context on node routes.
|
Chris@0
|
16 */
|
Chris@0
|
17 class NodeRouteContext implements ContextProviderInterface {
|
Chris@0
|
18
|
Chris@0
|
19 use StringTranslationTrait;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * The route match object.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var \Drupal\Core\Routing\RouteMatchInterface
|
Chris@0
|
25 */
|
Chris@0
|
26 protected $routeMatch;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * Constructs a new NodeRouteContext.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
|
Chris@0
|
32 * The route match object.
|
Chris@0
|
33 */
|
Chris@0
|
34 public function __construct(RouteMatchInterface $route_match) {
|
Chris@0
|
35 $this->routeMatch = $route_match;
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * {@inheritdoc}
|
Chris@0
|
40 */
|
Chris@0
|
41 public function getRuntimeContexts(array $unqualified_context_ids) {
|
Chris@0
|
42 $result = [];
|
Chris@17
|
43 $context_definition = EntityContextDefinition::create('node')->setRequired(FALSE);
|
Chris@0
|
44 $value = NULL;
|
Chris@0
|
45 if (($route_object = $this->routeMatch->getRouteObject()) && ($route_contexts = $route_object->getOption('parameters')) && isset($route_contexts['node'])) {
|
Chris@0
|
46 if ($node = $this->routeMatch->getParameter('node')) {
|
Chris@0
|
47 $value = $node;
|
Chris@0
|
48 }
|
Chris@0
|
49 }
|
Chris@0
|
50 elseif ($this->routeMatch->getRouteName() == 'node.add') {
|
Chris@0
|
51 $node_type = $this->routeMatch->getParameter('node_type');
|
Chris@0
|
52 $value = Node::create(['type' => $node_type->id()]);
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 $cacheability = new CacheableMetadata();
|
Chris@0
|
56 $cacheability->setCacheContexts(['route']);
|
Chris@0
|
57
|
Chris@0
|
58 $context = new Context($context_definition, $value);
|
Chris@0
|
59 $context->addCacheableDependency($cacheability);
|
Chris@0
|
60 $result['node'] = $context;
|
Chris@0
|
61
|
Chris@0
|
62 return $result;
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * {@inheritdoc}
|
Chris@0
|
67 */
|
Chris@0
|
68 public function getAvailableContexts() {
|
Chris@17
|
69 $context = EntityContext::fromEntityTypeId('node', $this->t('Node from URL'));
|
Chris@0
|
70 return ['node' => $context];
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 }
|