StatisticsLastCommentName.php 2.2 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2
<?php

3
namespace Drupal\comment\Plugin\views\field;
dawehner's avatar
dawehner committed
4

5
use Drupal\user\Entity\User;
6
use Drupal\views\Plugin\views\field\FieldPluginBase;
7
use Drupal\views\ResultRow;
8

merlinofchaos's avatar
merlinofchaos committed
9 10 11 12
/**
 * Field handler to present the name of the last comment poster.
 *
 * @ingroup views_field_handlers
13
 *
14
 * @ViewsField("comment_ces_last_comment_name")
dawehner's avatar
dawehner committed
15
 */
16
class StatisticsLastCommentName extends FieldPluginBase {
17

18 19 20
  /**
   * {@inheritdoc}
   */
21
  public function query() {
merlinofchaos's avatar
merlinofchaos committed
22 23
    // last_comment_name only contains data if the user is anonymous. So we
    // have to join in a specially related user table.
24
    $this->ensureMyTable();
merlinofchaos's avatar
merlinofchaos committed
25
    // join 'users' to this table via vid
26
    $definition = array(
27
      'table' => 'users_field_data',
28
      'field' => 'uid',
29
      'left_table' => 'comment_entity_statistics',
30 31 32 33 34 35 36 37 38
      'left_field' => 'last_comment_uid',
      'extra' => array(
        array(
          'field' => 'uid',
          'operator' => '!=',
          'value' => '0'
        )
      )
    );
39
    $join = \Drupal::service('plugin.manager.views.join')->createInstance('standard', $definition);
merlinofchaos's avatar
merlinofchaos committed
40

41 42
    // nes_user alias so this can work with the sort handler, below.
    $this->user_table = $this->query->ensureTable('ces_users', $this->relationship, $join);
merlinofchaos's avatar
merlinofchaos committed
43

44
    $this->field_alias = $this->query->addField(NULL, "COALESCE($this->user_table.name, $this->tableAlias.$this->field)", $this->tableAlias . '_' . $this->field);
merlinofchaos's avatar
merlinofchaos committed
45

46 47
    $this->user_field = $this->query->addField($this->user_table, 'name');
    $this->uid = $this->query->addField($this->tableAlias, 'last_comment_uid');
merlinofchaos's avatar
merlinofchaos committed
48 49
  }

50 51 52
  /**
   * {@inheritdoc}
   */
53 54
  protected function defineOptions() {
    $options = parent::defineOptions();
merlinofchaos's avatar
merlinofchaos committed
55

56
    $options['link_to_user'] = array('default' => TRUE);
merlinofchaos's avatar
merlinofchaos committed
57 58 59 60

    return $options;
  }

61 62 63 64
  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
merlinofchaos's avatar
merlinofchaos committed
65
    if (!empty($this->options['link_to_user'])) {
66
      $account = User::create();
67
      $account->name = $this->getValue($values);
merlinofchaos's avatar
merlinofchaos committed
68
      $account->uid = $values->{$this->uid};
69 70 71 72
      $username = array(
        '#theme' => 'username',
        '#account' => $account,
      );
73
      return \Drupal::service('renderer')->render($username);
merlinofchaos's avatar
merlinofchaos committed
74 75
    }
    else {
76
      return $this->sanitizeValue($this->getValue($values));
merlinofchaos's avatar
merlinofchaos committed
77 78
    }
  }
79

merlinofchaos's avatar
merlinofchaos committed
80
}