Version 1
[yaffs-website] / web / core / modules / comment / src / Plugin / views / argument / UserUid.php
1 <?php
2
3 namespace Drupal\comment\Plugin\views\argument;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8
9 /**
10  * Argument handler to accept a user id to check for nodes that
11  * user posted or commented on.
12  *
13  * @ingroup views_argument_handlers
14  *
15  * @ViewsArgument("argument_comment_user_uid")
16  */
17 class UserUid extends ArgumentPluginBase {
18
19   /**
20    * Database Service Object.
21    *
22    * @var \Drupal\Core\Database\Connection
23    */
24   protected $database;
25
26   /**
27    * Constructs a Drupal\Component\Plugin\PluginBase object.
28    *
29    * @param array $configuration
30    *   A configuration array containing information about the plugin instance.
31    * @param string $plugin_id
32    *   The plugin_id for the plugin instance.
33    * @param mixed $plugin_definition
34    *   The plugin implementation definition.
35    * @param \Drupal\Core\Database\Connection $database
36    *   Database Service Object.
37    */
38   public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database) {
39     parent::__construct($configuration, $plugin_id, $plugin_definition);
40
41     $this->database = $database;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
48     return new static($configuration, $plugin_id, $plugin_definition, $container->get('database'));
49   }
50
51   public function title() {
52     if (!$this->argument) {
53       $title = \Drupal::config('user.settings')->get('anonymous');
54     }
55     else {
56       $title = $this->database->query('SELECT name FROM {users_field_data} WHERE uid = :uid AND default_langcode = 1', [':uid' => $this->argument])->fetchField();
57     }
58     if (empty($title)) {
59       return $this->t('No user');
60     }
61
62     return $title;
63   }
64
65   protected function defaultActions($which = NULL) {
66     // Disallow summary views on this argument.
67     if (!$which) {
68       $actions = parent::defaultActions();
69       unset($actions['summary asc']);
70       unset($actions['summary desc']);
71       return $actions;
72     }
73
74     if ($which != 'summary asc' && $which != 'summary desc') {
75       return parent::defaultActions($which);
76     }
77   }
78
79   public function query($group_by = FALSE) {
80     $this->ensureMyTable();
81
82     // Use the table definition to correctly add this user ID condition.
83     if ($this->table != 'comment_field_data') {
84       $subselect = $this->database->select('comment_field_data', 'c');
85       $subselect->addField('c', 'cid');
86       $subselect->condition('c.uid', $this->argument);
87
88       $entity_id = $this->definition['entity_id'];
89       $entity_type = $this->definition['entity_type'];
90       $subselect->where("c.entity_id = $this->tableAlias.$entity_id");
91       $subselect->condition('c.entity_type', $entity_type);
92
93       $condition = db_or()
94         ->condition("$this->tableAlias.uid", $this->argument, '=')
95         ->exists($subselect);
96
97       $this->query->addWhere(0, $condition);
98     }
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function getSortName() {
105     return $this->t('Numerical', [], ['context' => 'Sort order']);
106   }
107
108 }