48f556e68ac7a513f1e1347082e60f53f59540a2
[yaffs-website] / web / core / modules / comment / src / Plugin / views / sort / StatisticsLastCommentName.php
1 <?php
2
3 namespace Drupal\comment\Plugin\views\sort;
4
5 use Drupal\views\Plugin\views\sort\SortPluginBase;
6
7 /**
8  * Sort handler to sort by last comment name which might be in 2 different
9  * fields.
10  *
11  * @ingroup views_sort_handlers
12  *
13  * @ViewsSort("comment_ces_last_comment_name")
14  */
15 class StatisticsLastCommentName extends SortPluginBase {
16
17   public function query() {
18     $this->ensureMyTable();
19     $definition = [
20       'table' => 'users_field_data',
21       'field' => 'uid',
22       'left_table' => 'comment_entity_statistics',
23       'left_field' => 'last_comment_uid',
24     ];
25     $join = \Drupal::service('plugin.manager.views.join')->createInstance('standard', $definition);
26
27     // @todo this might be safer if we had an ensure_relationship rather than guessing
28     // the table alias. Though if we did that we'd be guessing the relationship name
29     // so that doesn't matter that much.
30     $this->user_table = $this->query->ensureTable('ces_users', $this->relationship, $join);
31     $this->user_field = $this->query->addField($this->user_table, 'name');
32
33     // Add the field.
34     $this->query->addOrderBy(NULL, "LOWER(COALESCE($this->user_table.name, $this->tableAlias.$this->field))", $this->options['order'], $this->tableAlias . '_' . $this->field);
35   }
36
37 }