function Roles::preRender

Same name in other branches
  1. 9 core/modules/user/src/Plugin/views/field/Roles.php \Drupal\user\Plugin\views\field\Roles::preRender()
  2. 8.9.x core/modules/user/src/Plugin/views/field/Roles.php \Drupal\user\Plugin\views\field\Roles::preRender()
  3. 10 core/modules/user/src/Plugin/views/field/Roles.php \Drupal\user\Plugin\views\field\Roles::preRender()

Overrides FieldPluginBase::preRender

File

core/modules/user/src/Plugin/views/field/Roles.php, line 67

Class

Roles
Field handler to provide a list of roles.

Namespace

Drupal\user\Plugin\views\field

Code

public function preRender(&$values) {
    $uids = [];
    $this->items = [];
    foreach ($values as $result) {
        $uids[] = $this->getValue($result);
    }
    if ($uids) {
        $roles = Role::loadMultiple();
        $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),
        ]);
        foreach ($result as $role) {
            $this->items[$role->uid][$role->rid]['role'] = $roles[$role->rid]
                ->label();
            $this->items[$role->uid][$role->rid]['rid'] = $role->rid;
        }
        // Sort the roles for each user by role weight.
        $ordered_roles = array_flip(array_keys($roles));
        foreach ($this->items as &$user_roles) {
            // Create an array of rids that the user has in the role weight order.
            $sorted_keys = array_intersect_key($ordered_roles, $user_roles);
            // Merge with the unsorted array of role information which has the
            // effect of sorting it.
            $user_roles = array_replace($sorted_keys, $user_roles);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.