You are here

public function GroupRole::changePermissions in Group 2.0.x

Same name and namespace in other branches
  1. 8 src/Entity/GroupRole.php \Drupal\group\Entity\GroupRole::changePermissions()

Changes permissions for the 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

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 group' => 0,
  // Revoke 'administer group'
  'edit group' => FALSE,
  // Revoke 'edit group'
  'administer members' => 1,
  // Grant 'administer members'
  'leave group' => TRUE,
  // Grant 'leave group'
  'join group' => 'join group',
];

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

Return value

\Drupal\group\Entity\GroupRoleInterface The group role this was called on.

Overrides GroupRoleInterface::changePermissions

File

src/Entity/GroupRole.php, line 265

Class

GroupRole
Defines the Group role configuration entity.

Namespace

Drupal\group\Entity

Code

public function changePermissions(array $permissions = []) {

  // Grant new permissions to the role.
  $grant = array_filter($permissions);
  if (!empty($grant)) {
    $this
      ->grantPermissions(array_keys($grant));
  }

  // Revoke permissions from the role.
  $revoke = array_diff_assoc($permissions, $grant);
  if (!empty($revoke)) {
    $this
      ->revokePermissions(array_keys($revoke));
  }
  return $this;
}