Version 1
[yaffs-website] / web / core / modules / node / src / Plugin / views / argument / Vid.php
1 <?php
2
3 namespace Drupal\node\Plugin\views\argument;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\views\Plugin\views\argument\NumericArgument;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Drupal\node\NodeStorageInterface;
9
10 /**
11  * Argument handler to accept a node revision id.
12  *
13  * @ViewsArgument("node_vid")
14  */
15 class Vid extends NumericArgument {
16
17   /**
18    * Database Service Object.
19    *
20    * @var \Drupal\Core\Database\Connection
21    */
22   protected $database;
23
24   /**
25    * The node storage.
26    *
27    * @var \Drupal\node\NodeStorageInterface
28    */
29   protected $nodeStorage;
30
31   /**
32    * Constructs a Drupal\Component\Plugin\PluginBase object.
33    *
34    * @param array $configuration
35    *   A configuration array containing information about the plugin instance.
36    * @param string $plugin_id
37    *   The plugin_id for the plugin instance.
38    * @param mixed $plugin_definition
39    *   The plugin implementation definition.
40    * @param \Drupal\Core\Database\Connection $database
41    *   Database Service Object.
42    * @param \Drupal\node\NodeStorageInterface $node_storage
43    *   The node storage.
44    */
45   public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, NodeStorageInterface $node_storage) {
46     parent::__construct($configuration, $plugin_id, $plugin_definition);
47
48     $this->database = $database;
49     $this->nodeStorage = $node_storage;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
56     return new static(
57       $configuration,
58       $plugin_id,
59       $plugin_definition,
60       $container->get('database'),
61       $container->get('entity.manager')->getStorage('node')
62     );
63   }
64
65   /**
66    * Override the behavior of title(). Get the title of the revision.
67    */
68   public function titleQuery() {
69     $titles = [];
70
71     $results = $this->database->query('SELECT nr.vid, nr.nid, npr.title FROM {node_revision} nr WHERE nr.vid IN ( :vids[] )', [':vids[]' => $this->value])->fetchAllAssoc('vid', PDO::FETCH_ASSOC);
72     $nids = [];
73     foreach ($results as $result) {
74       $nids[] = $result['nid'];
75     }
76
77     $nodes = $this->nodeStorage->loadMultiple(array_unique($nids));
78
79     foreach ($results as $result) {
80       $nodes[$result['nid']]->set('title', $result['title']);
81       $titles[] = $nodes[$result['nid']]->label();
82     }
83
84     return $titles;
85   }
86
87 }