You are here

public function BookAccessHelper::getRoleBookAccessDefaults in Book access 1.x

Get the saved defaults for every role sorted by role weight.

File

src/BookAccessHelper.php, line 174

Class

BookAccessHelper
Helper functions for book_access.

Namespace

Drupal\book_access

Code

public function getRoleBookAccessDefaults() : array {
  $results = [];

  // Get the roles by weight.
  $query = $this->entityTypeManager
    ->getStorage('user_role')
    ->getQuery();
  $role_results = $query
    ->sort('weight', 'ASC')
    ->execute();
  $roles = $this->entityTypeManager
    ->getStorage('user_role')
    ->loadMultiple(array_keys($role_results));

  // Get all the book access defaults entities.
  $query = $this->entityTypeManager
    ->getStorage('book_access_defaults')
    ->getQuery();
  $defaults_ids = $query
    ->execute();
  $defaults = $this->entityTypeManager
    ->getStorage('book_access_defaults')
    ->loadMultiple(array_keys($defaults_ids));
  foreach ($roles as $role) {
    $results[$role
      ->id()] = [
      'label' => $role
        ->label(),
      'default_grants' => $defaults[$role
        ->id()] ?? $this
        ->setDefaultForRole($role),
    ];
  }
  return $results;
}