Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / src / Plugin / views / filter / LatestRevision.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\filter;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\views\Plugin\ViewsHandlerManager;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Filter to show only the latest revision of an entity.
13  *
14  * @ingroup views_filter_handlers
15  *
16  * @ViewsFilter("latest_revision")
17  */
18 class LatestRevision extends FilterPluginBase implements ContainerFactoryPluginInterface {
19
20   /**
21    * Entity Type Manager service.
22    *
23    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
24    */
25   protected $entityTypeManager;
26
27   /**
28    * Views Handler Plugin Manager.
29    *
30    * @var \Drupal\views\Plugin\ViewsHandlerManager
31    */
32   protected $joinHandler;
33
34   /**
35    * Constructs a new LatestRevision.
36    *
37    * @param array $configuration
38    *   A configuration array containing information about the plugin instance.
39    * @param string $plugin_id
40    *   The plugin_id for the plugin instance.
41    * @param mixed $plugin_definition
42    *   The plugin implementation definition.
43    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
44    *   Entity Type Manager Service.
45    * @param \Drupal\views\Plugin\ViewsHandlerManager $join_handler
46    *   Views Handler Plugin Manager.
47    */
48   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ViewsHandlerManager $join_handler) {
49     parent::__construct($configuration, $plugin_id, $plugin_definition);
50
51     $this->entityTypeManager = $entity_type_manager;
52     $this->joinHandler = $join_handler;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
59     return new static(
60       $configuration, $plugin_id, $plugin_definition,
61       $container->get('entity_type.manager'),
62       $container->get('plugin.manager.views.join')
63     );
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function adminSummary() {
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   protected function operatorForm(&$form, FormStateInterface $form_state) {
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function canExpose() {
82     return FALSE;
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function query() {
89     /** @var \Drupal\views\Plugin\views\query\Sql $query */
90     $query = $this->query;
91     $query_base_table = $this->relationship ?: $this->view->storage->get('base_table');
92
93     $entity_type = $this->entityTypeManager->getDefinition($this->getEntityType());
94     $keys = $entity_type->getKeys();
95
96     $definition = [
97       'table' => $query_base_table,
98       'type' => 'LEFT',
99       'field' => $keys['id'],
100       'left_table' => $query_base_table,
101       'left_field' => $keys['id'],
102       'extra' => [
103         ['left_field' => $keys['revision'], 'field' => $keys['revision'], 'operator' => '>'],
104       ],
105     ];
106
107     $join = $this->joinHandler->createInstance('standard', $definition);
108
109     $join_table_alias = $query->addTable($query_base_table, $this->relationship, $join);
110     $query->addWhere($this->options['group'], "$join_table_alias.{$keys['id']}", NULL, 'IS NULL');
111   }
112
113 }