e7d9495bcadd636a6fee21ef050993b552bdae99
[yaffs-website] / web / core / modules / user / src / Plugin / views / field / Roles.php
1 <?php
2
3 namespace Drupal\user\Plugin\views\field;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\views\Plugin\views\display\DisplayPluginBase;
7 use Drupal\views\ViewExecutable;
8 use Drupal\views\Plugin\views\field\PrerenderList;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Field handler to provide a list of roles.
13  *
14  * @ingroup views_field_handlers
15  *
16  * @ViewsField("user_roles")
17  */
18 class Roles extends PrerenderList {
19
20   /**
21    * Database Service Object.
22    *
23    * @var \Drupal\Core\Database\Connection
24    */
25   protected $database;
26
27   /**
28    * Constructs a Drupal\Component\Plugin\PluginBase 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   /**
53    * {@inheritdoc}
54    */
55   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
56     parent::init($view, $display, $options);
57
58     $this->additional_fields['uid'] = ['table' => 'users_field_data', 'field' => 'uid'];
59   }
60
61   public function query() {
62     $this->addAdditionalFields();
63     $this->field_alias = $this->aliases['uid'];
64   }
65
66   public function preRender(&$values) {
67     $uids = [];
68     $this->items = [];
69
70     foreach ($values as $result) {
71       $uids[] = $this->getValue($result);
72     }
73
74     if ($uids) {
75       $roles = user_roles();
76       $result = $this->database->query('SELECT u.entity_id as uid, u.roles_target_id as rid FROM {user__roles} u WHERE u.entity_id IN ( :uids[] ) AND u.roles_target_id IN ( :rids[] )', [':uids[]' => $uids, ':rids[]' => array_keys($roles)]);
77       foreach ($result as $role) {
78         $this->items[$role->uid][$role->rid]['role'] = $roles[$role->rid]->label();
79         $this->items[$role->uid][$role->rid]['rid'] = $role->rid;
80       }
81       // Sort the roles for each user by role weight.
82       $ordered_roles = array_flip(array_keys($roles));
83       foreach ($this->items as &$user_roles) {
84         // Create an array of rids that the user has in the role weight order.
85         $sorted_keys  = array_intersect_key($ordered_roles, $user_roles);
86         // Merge with the unsorted array of role information which has the
87         // effect of sorting it.
88         $user_roles = array_merge($sorted_keys, $user_roles);
89       }
90     }
91   }
92
93   public function render_item($count, $item) {
94     return $item['role'];
95   }
96
97   protected function documentSelfTokens(&$tokens) {
98     $tokens['{{ ' . $this->options['id'] . '__role' . ' }}'] = $this->t('The name of the role.');
99     $tokens['{{ ' . $this->options['id'] . '__rid' . ' }}'] = $this->t('The role machine-name of the role.');
100   }
101
102   protected function addSelfTokens(&$tokens, $item) {
103     if (!empty($item['role'])) {
104       $tokens['{{ ' . $this->options['id'] . '__role' . ' }}'] = $item['role'];
105       $tokens['{{ ' . $this->options['id'] . '__rid' . ' }}'] = $item['rid'];
106     }
107   }
108
109 }