3 namespace Drupal\user\Plugin\views\field;
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;
12 * Field handler to provide a list of roles.
14 * @ingroup views_field_handlers
16 * @ViewsField("user_roles")
18 class Roles extends PrerenderList {
21 * Database Service Object.
23 * @var \Drupal\Core\Database\Connection
28 * Constructs a Drupal\Component\Plugin\PluginBase object.
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.
39 public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database) {
40 parent::__construct($configuration, $plugin_id, $plugin_definition);
42 $this->database = $database;
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'));
55 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
56 parent::init($view, $display, $options);
58 $this->additional_fields['uid'] = ['table' => 'users_field_data', 'field' => 'uid'];
61 public function query() {
62 $this->addAdditionalFields();
63 $this->field_alias = $this->aliases['uid'];
66 public function preRender(&$values) {
70 foreach ($values as $result) {
71 $uids[] = $this->getValue($result);
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;
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);
93 public function render_item($count, $item) {
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.');
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'];