Vid.php in Drupal 8
File
core/modules/node/src/Plugin/views/argument/Vid.php
View source
<?php
namespace Drupal\node\Plugin\views\argument;
use Drupal\Core\Database\Connection;
use Drupal\views\Plugin\views\argument\NumericArgument;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\node\NodeStorageInterface;
class Vid extends NumericArgument {
protected $database;
protected $nodeStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, NodeStorageInterface $node_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->database = $database;
$this->nodeStorage = $node_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('database'), $container
->get('entity_type.manager')
->getStorage('node'));
}
public function titleQuery() {
$titles = [];
$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);
$nids = [];
foreach ($results as $result) {
$nids[] = $result['nid'];
}
$nodes = $this->nodeStorage
->loadMultiple(array_unique($nids));
foreach ($results as $result) {
$nodes[$result['nid']]
->set('title', $result['title']);
$titles[] = $nodes[$result['nid']]
->label();
}
return $titles;
}
}
Classes
Name |
Description |
Vid |
Argument handler to accept a node revision id. |