You are here

public function Permissions::preRender in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/src/Plugin/views/field/Permissions.php \Drupal\user\Plugin\views\field\Permissions::preRender()
  2. 9 core/modules/user/src/Plugin/views/field/Permissions.php \Drupal\user\Plugin\views\field\Permissions::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 FieldHandlerInterface::preRender

File

core/modules/user/src/Plugin/views/field/Permissions.php, line 83

Class

Permissions
Field handler to provide a list of permissions.

Namespace

Drupal\user\Plugin\views\field

Code

public function preRender(&$values) {
  $this->items = [];
  $permission_names = \Drupal::service('user.permissions')
    ->getPermissions();
  $rids = [];
  foreach ($values as $result) {
    $user_rids = $this
      ->getEntity($result)
      ->getRoles();
    $uid = $this
      ->getValue($result);
    foreach ($user_rids as $rid) {
      $rids[$rid][] = $uid;
    }
  }
  if ($rids) {
    $roles = $this->roleStorage
      ->loadMultiple(array_keys($rids));
    foreach ($rids as $rid => $role_uids) {
      foreach ($roles[$rid]
        ->getPermissions() as $permission) {
        foreach ($role_uids as $uid) {
          $this->items[$uid][$permission]['permission'] = $permission_names[$permission]['title'];
        }
      }
    }
    foreach ($this->items as &$permission) {
      ksort($permission);
    }
  }
}