function Permissions::query

Same name in this branch
  1. 10 core/modules/user/src/Plugin/views/field/Permissions.php \Drupal\user\Plugin\views\field\Permissions::query()
Same name and namespace in other branches
  1. 9 core/modules/user/src/Plugin/views/field/Permissions.php \Drupal\user\Plugin\views\field\Permissions::query()
  2. 9 core/modules/user/src/Plugin/views/filter/Permissions.php \Drupal\user\Plugin\views\filter\Permissions::query()
  3. 8.9.x core/modules/user/src/Plugin/views/field/Permissions.php \Drupal\user\Plugin\views\field\Permissions::query()
  4. 8.9.x core/modules/user/src/Plugin/views/filter/Permissions.php \Drupal\user\Plugin\views\filter\Permissions::query()
  5. 11.x core/modules/user/src/Plugin/views/field/Permissions.php \Drupal\user\Plugin\views\field\Permissions::query()
  6. 11.x core/modules/user/src/Plugin/views/filter/Permissions.php \Drupal\user\Plugin\views\filter\Permissions::query()

Replace the configured permission with a filter by all roles that have this permission.

Overrides InOperator::query

File

core/modules/user/src/Plugin/views/filter/Permissions.php, line 101

Class

Permissions
Filter handler for user roles.

Namespace

Drupal\user\Plugin\views\filter

Code

public function query() {
    $rids = [];
    $all_roles = Role::loadMultiple();
    // Get all role IDs that have the configured permissions.
    foreach ($this->value as $permission) {
        $roles = array_filter($all_roles, fn(RoleInterface $role) => $role->hasPermission($permission));
        // Method Role::loadMultiple() returns an array with the role IDs as keys,
        // so take the array keys and merge them with previously found role IDs.
        $rids = array_merge($rids, array_keys($roles));
    }
    // Remove any duplicate role IDs.
    $rids = array_unique($rids);
    $this->value = $rids;
    // $this->value contains the role IDs that have the configured permission.
    parent::query();
}

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