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