You are here

public function RemoveSingleOgMembershipRole::execute in Organic groups 8

Executes the plugin.

Overrides ExecutableInterface::execute

File

src/Plugin/Action/RemoveSingleOgMembershipRole.php, line 23

Class

RemoveSingleOgMembershipRole
Removes a role from a group membership.

Namespace

Drupal\og\Plugin\Action

Code

public function execute(?OgMembership $membership = NULL) {
  if (!$membership) {
    return;
  }
  $role_name = $this->configuration['role_name'];
  $role_id = implode('-', [
    $membership
      ->getGroupEntityType(),
    $membership
      ->getGroupBundle(),
    $role_name,
  ]);

  // Skip removing the role from the membership if it doesn't have it.
  if (in_array($role_id, $membership
    ->getRolesIds())) {
    $membership
      ->revokeRoleById($role_id)
      ->save();
  }
}