Version 1
[yaffs-website] / web / core / modules / node / src / Plugin / views / argument_default / Node.php
1 <?php
2
3 namespace Drupal\node\Plugin\views\argument_default;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheableDependencyInterface;
7 use Drupal\Core\Routing\RouteMatchInterface;
8 use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
9 use Drupal\node\NodeInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Default argument plugin to extract a node.
14  *
15  * @ViewsArgumentDefault(
16  *   id = "node",
17  *   title = @Translation("Content ID from URL")
18  * )
19  */
20 class Node extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
21
22   /**
23    * The route match.
24    *
25    * @var \Drupal\Core\Routing\RouteMatchInterface
26    */
27   protected $routeMatch;
28
29   /**
30    * Constructs a new Node instance.
31    *
32    * @param array $configuration
33    *   A configuration array containing information about the plugin instance.
34    * @param string $plugin_id
35    *   The plugin_id for the plugin instance.
36    * @param mixed $plugin_definition
37    *   The plugin implementation definition.
38    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
39    *   The route match.
40    */
41   public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
42     parent::__construct($configuration, $plugin_id, $plugin_definition);
43
44     $this->routeMatch = $route_match;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
51     return new static(
52       $configuration,
53       $plugin_id,
54       $plugin_definition,
55       $container->get('current_route_match')
56     );
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function getArgument() {
63     if (($node = $this->routeMatch->getParameter('node')) && $node instanceof NodeInterface) {
64       return $node->id();
65     }
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function getCacheMaxAge() {
72     return Cache::PERMANENT;
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function getCacheContexts() {
79     return ['url'];
80   }
81
82 }