UserUid.php 1.78 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1
2
3
4
<?php

/**
 * @file
5
 * Definition of Drupal\comment\Plugin\views\argument\UserUid.
merlinofchaos's avatar
merlinofchaos committed
6
7
 */

8
namespace Drupal\comment\Plugin\views\argument;
dawehner's avatar
dawehner committed
9

10
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
dawehner's avatar
dawehner committed
11
12
use Drupal\Core\Annotation\Plugin;

merlinofchaos's avatar
merlinofchaos committed
13
14
15
16
17
/**
 * Argument handler to accept a user id to check for nodes that
 * user posted or commented on.
 *
 * @ingroup views_argument_handlers
18
 *
dawehner's avatar
dawehner committed
19
 * @Plugin(
20
21
 *   id = "argument_comment_user_uid",
 *   module = "comment"
dawehner's avatar
dawehner committed
22
23
24
 * )
 */
class UserUid extends ArgumentPluginBase {
25

merlinofchaos's avatar
merlinofchaos committed
26
27
  function title() {
    if (!$this->argument) {
28
      $title = config('user.settings')->get('anonymous');
merlinofchaos's avatar
merlinofchaos committed
29
30
    }
    else {
31
32
33
34
      $query = db_select('users', 'u');
      $query->addField('u', 'name');
      $query->condition('u.uid', $this->argument);
      $title = $query->execute()->fetchField();
merlinofchaos's avatar
merlinofchaos committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    }
    if (empty($title)) {
      return t('No user');
    }

    return check_plain($title);
  }

  function default_actions($which = NULL) {
    // Disallow summary views on this argument.
    if (!$which) {
      $actions = parent::default_actions();
      unset($actions['summary asc']);
      unset($actions['summary desc']);
      return $actions;
    }

    if ($which != 'summary asc' && $which != 'summary desc') {
      return parent::default_actions($which);
    }
  }

57
  public function query($group_by = FALSE) {
58
    $this->ensureMyTable();
merlinofchaos's avatar
merlinofchaos committed
59
60
61
62

    $subselect = db_select('comment', 'c');
    $subselect->addField('c', 'cid');
    $subselect->condition('c.uid', $this->argument);
63
    $subselect->where("c.nid = $this->tableAlias.nid");
merlinofchaos's avatar
merlinofchaos committed
64
65

    $condition = db_or()
66
      ->condition("$this->tableAlias.uid", $this->argument, '=')
merlinofchaos's avatar
merlinofchaos committed
67
68
69
70
71
72
73
74
      ->exists($subselect);

    $this->query->add_where(0, $condition);
  }

  function get_sort_name() {
    return t('Numerical', array(), array('context' => 'Sort order'));
  }
75

merlinofchaos's avatar
merlinofchaos committed
76
}