Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Roles.php

function 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);
        }
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal