Version 1
[yaffs-website] / web / core / modules / node / src / Plugin / views / argument / Nid.php
1 <?php
2
3 namespace Drupal\node\Plugin\views\argument;
4
5 use Drupal\node\NodeStorageInterface;
6 use Drupal\views\Plugin\views\argument\NumericArgument;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8
9 /**
10  * Argument handler to accept a node id.
11  *
12  * @ViewsArgument("node_nid")
13  */
14 class Nid extends NumericArgument {
15
16   /**
17    * The node storage.
18    *
19    * @var \Drupal\node\NodeStorageInterface
20    */
21   protected $nodeStorage;
22
23   /**
24    * Constructs the Nid object.
25    *
26    * @param array $configuration
27    *   A configuration array containing information about the plugin instance.
28    * @param string $plugin_id
29    *   The plugin_id for the plugin instance.
30    * @param mixed $plugin_definition
31    *   The plugin implementation definition.
32    * @param NodeStorageInterface $node_storage
33    */
34   public function __construct(array $configuration, $plugin_id, $plugin_definition, NodeStorageInterface $node_storage) {
35     parent::__construct($configuration, $plugin_id, $plugin_definition);
36     $this->nodeStorage = $node_storage;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
43     return new static(
44       $configuration,
45       $plugin_id,
46       $plugin_definition,
47       $container->get('entity.manager')->getStorage('node')
48     );
49   }
50
51   /**
52    * Override the behavior of title(). Get the title of the node.
53    */
54   public function titleQuery() {
55     $titles = [];
56
57     $nodes = $this->nodeStorage->loadMultiple($this->value);
58     foreach ($nodes as $node) {
59       $titles[] = $node->label();
60     }
61     return $titles;
62   }
63
64 }