You are here

protected function PermissionHandler::buildPermissionsYaml in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/src/PermissionHandler.php \Drupal\user\PermissionHandler::buildPermissionsYaml()
  2. 9 core/modules/user/src/PermissionHandler.php \Drupal\user\PermissionHandler::buildPermissionsYaml()

Builds all permissions provided by .permissions.yml files.

Return value

array[] An array with the same structure as PermissionHandlerInterface::getPermissions().

See also

\Drupal\user\PermissionHandlerInterface::getPermissions()

File

core/modules/user/src/PermissionHandler.php, line 142

Class

PermissionHandler
Provides the available permissions based on yml files.

Namespace

Drupal\user

Code

protected function buildPermissionsYaml() {
  $all_permissions = [];
  $all_callback_permissions = [];
  foreach ($this
    ->getYamlDiscovery()
    ->findAll() as $provider => $permissions) {

    // The top-level 'permissions_callback' is a list of methods in controller
    // syntax, see \Drupal\Core\Controller\ControllerResolver. These methods
    // should return an array of permissions in the same structure.
    if (isset($permissions['permission_callbacks'])) {
      foreach ($permissions['permission_callbacks'] as $permission_callback) {
        $callback = $this->controllerResolver
          ->getControllerFromDefinition($permission_callback);
        if ($callback_permissions = call_user_func($callback)) {

          // Add any callback permissions to the array of permissions. Any
          // defaults can then get processed below.
          foreach ($callback_permissions as $name => $callback_permission) {
            if (!is_array($callback_permission)) {
              $callback_permission = [
                'title' => $callback_permission,
              ];
            }
            $callback_permission += [
              'description' => NULL,
              'provider' => $provider,
            ];
            $all_callback_permissions[$name] = $callback_permission;
          }
        }
      }
      unset($permissions['permission_callbacks']);
    }
    foreach ($permissions as &$permission) {
      if (!is_array($permission)) {
        $permission = [
          'title' => $permission,
        ];
      }
      $permission['title'] = $this
        ->t($permission['title']);
      $permission['description'] = isset($permission['description']) ? $this
        ->t($permission['description']) : NULL;
      $permission['provider'] = !empty($permission['provider']) ? $permission['provider'] : $provider;
    }
    $all_permissions += $permissions;
  }
  return $all_permissions + $all_callback_permissions;
}