You are here

public function FieldPermissionsService::getPermissionsByRole in Field Permissions 8

Same name and namespace in other branches
  1. 8.2 src/FieldPermissionsService.php \Drupal\field_permissions\FieldPermissionsService::getPermissionsByRole()

Get all field permissions keyed by role ID.

Return value

array An array of permission information keyed by role ID.

Overrides FieldPermissionsServiceInterface::getPermissionsByRole

File

src/FieldPermissionsService.php, line 90

Class

FieldPermissionsService
The field permission service.

Namespace

Drupal\field_permissions

Code

public function getPermissionsByRole() {

  /** @var \Drupal\user\RoleInterface[] $roles */
  $roles = $this->entityTypeManager
    ->getStorage('user_role')
    ->loadMultiple();
  $field_field_permissions = [];
  $field_permission_perm = $this
    ->getAllPermissions();
  foreach ($roles as $role_name => $role) {
    $role_permissions = $role
      ->getPermissions();
    $field_field_permissions[$role_name] = [];

    // For all element set admin permission.
    if ($role
      ->isAdmin()) {
      foreach (array_keys($field_permission_perm) as $perm_name) {
        $field_field_permissions[$role_name][] = $perm_name;
      }
    }
    else {
      foreach ($role_permissions as $key => $role_permission) {
        if (in_array($role_permission, array_keys($field_permission_perm))) {
          $field_field_permissions[$role_name][] = $role_permission;
        }
      }
    }
  }
  return $field_field_permissions;
}