You are here

public function AddSingleOgMembershipRole::execute in Organic groups 8

Executes the plugin.

Overrides ExecutableInterface::execute

File

src/Plugin/Action/AddSingleOgMembershipRole.php, line 24

Class

AddSingleOgMembershipRole
Adds a role to 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,
  ]);

  // Only add the role if it is valid and doesn't exist yet.
  $role = OgRole::load($role_id);
  if ($membership
    ->isRoleValid($role) && !$membership
    ->hasRole($role_id)) {
    $membership
      ->addRole($role)
      ->save();
  }
}