StatisticsLastCommentName.php in Zircon Profile 8.0
File
core/modules/comment/src/Plugin/views/field/StatisticsLastCommentName.php
View source
<?php
namespace Drupal\comment\Plugin\views\field;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
class StatisticsLastCommentName extends FieldPluginBase {
public function query() {
$this
->ensureMyTable();
$definition = array(
'table' => 'users_field_data',
'field' => 'uid',
'left_table' => 'comment_entity_statistics',
'left_field' => 'last_comment_uid',
'extra' => array(
array(
'field' => 'uid',
'operator' => '!=',
'value' => '0',
),
),
);
$join = \Drupal::service('plugin.manager.views.join')
->createInstance('standard', $definition);
$this->user_table = $this->query
->ensureTable('ces_users', $this->relationship, $join);
$this->field_alias = $this->query
->addField(NULL, "COALESCE({$this->user_table}.name, {$this->tableAlias}.{$this->field})", $this->tableAlias . '_' . $this->field);
$this->user_field = $this->query
->addField($this->user_table, 'name');
$this->uid = $this->query
->addField($this->tableAlias, 'last_comment_uid');
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['link_to_user'] = array(
'default' => TRUE,
);
return $options;
}
public function render(ResultRow $values) {
if (!empty($this->options['link_to_user'])) {
$account = entity_create('user');
$account->name = $this
->getValue($values);
$account->uid = $values->{$this->uid};
$username = array(
'#theme' => 'username',
'#account' => $account,
);
return drupal_render($username);
}
else {
return $this
->sanitizeValue($this
->getValue($values));
}
}
}