You are here

public function Roles::preRender in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Plugin/views/field/Roles.php \Drupal\user\Plugin\views\field\Roles::preRender()

Runs before any fields are rendered.

This gives the handlers some time to set up before any handler has been rendered.

Parameters

\Drupal\views\ResultRow[] $values: An array of all ResultRow objects returned from the query.

Overrides FieldPluginBase::preRender

File

core/modules/user/src/Plugin/views/field/Roles.php, line 71
Contains \Drupal\user\Plugin\views\field\Roles.

Class

Roles
Field handler to provide a list of roles.

Namespace

Drupal\user\Plugin\views\field

Code

public function preRender(&$values) {
  $uids = array();
  $this->items = array();
  foreach ($values as $result) {
    $uids[] = $this
      ->getValue($result);
  }
  if ($uids) {
    $roles = user_roles();
    $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[] )', array(
      ':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_merge($sorted_keys, $user_roles);
    }
  }
}