function user_role_change_permissions

Same name and namespace in other branches
  1. 10 core/modules/user/user.module \user_role_change_permissions()
  2. 9 core/modules/user/user.module \user_role_change_permissions()
  3. 8.9.x core/modules/user/user.module \user_role_change_permissions()
  4. 7.x modules/user/user.module \user_role_change_permissions()
  5. main core/modules/user/user.module \user_role_change_permissions()

Change permissions for a user role.

This function may be used to grant and revoke multiple permissions at once. For example, when a form exposes checkboxes to configure permissions for a role, the form submit handler may directly pass the submitted values for the checkboxes form element to this function.

Parameters

mixed $rid: The ID of a user role to alter.

array $permissions: (optional) An associative array, where the key holds the permission name and the value determines whether to grant or revoke that permission. Any value that evaluates to TRUE will cause the permission to be granted. Any value that evaluates to FALSE will cause the permission to be revoked.

[
  'administer nodes' => 0,
  // Revoke 'administer nodes'
'administer blocks' => FALSE,
  // Revoke 'administer blocks'
'access user profiles' => 1,
  // Grant 'access user profiles'
'access content' => TRUE,
  // Grant 'access content'
'access comments' => 'access comments',
];

Existing permissions are not changed, unless specified in $permissions.

Deprecated

in drupal:11.5.0 and is removed from drupal:13.0.0. Use \Drupal\user\RoleInterface::changePermissions() instead.

See also

RoleInterface::grantPermissions()

RoleInterface::revokePermissions()

https://www.drupal.org/node/3348027

File

core/modules/user/user.module, line 402

Code

function user_role_change_permissions($rid, array $permissions = []) : void {
  @trigger_error(__METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use \\Drupal\\user\\RoleInterface::changePermissions() instead. See https://www.drupal.org/node/3348027', E_USER_DEPRECATED);
  $role_storage = \Drupal::entityTypeManager()->getStorage('user_role');
  $role = $role_storage->loadOverrideFree($rid);
  $role?->changePermissions($permissions);
  $role?->save();
}

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