You are here

public function ChangeGroupMembershipRole::execute in Open Social 10.2.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/src/Plugin/Action/ChangeGroupMembershipRole.php \Drupal\social_group\Plugin\Action\ChangeGroupMembershipRole::execute()
  2. 8.5 modules/social_features/social_group/src/Plugin/Action/ChangeGroupMembershipRole.php \Drupal\social_group\Plugin\Action\ChangeGroupMembershipRole::execute()
  3. 8.6 modules/social_features/social_group/src/Plugin/Action/ChangeGroupMembershipRole.php \Drupal\social_group\Plugin\Action\ChangeGroupMembershipRole::execute()
  4. 8.7 modules/social_features/social_group/src/Plugin/Action/ChangeGroupMembershipRole.php \Drupal\social_group\Plugin\Action\ChangeGroupMembershipRole::execute()
  5. 8.8 modules/social_features/social_group/src/Plugin/Action/ChangeGroupMembershipRole.php \Drupal\social_group\Plugin\Action\ChangeGroupMembershipRole::execute()
  6. 10.3.x modules/social_features/social_group/src/Plugin/Action/ChangeGroupMembershipRole.php \Drupal\social_group\Plugin\Action\ChangeGroupMembershipRole::execute()
  7. 10.0.x modules/social_features/social_group/src/Plugin/Action/ChangeGroupMembershipRole.php \Drupal\social_group\Plugin\Action\ChangeGroupMembershipRole::execute()
  8. 10.1.x modules/social_features/social_group/src/Plugin/Action/ChangeGroupMembershipRole.php \Drupal\social_group\Plugin\Action\ChangeGroupMembershipRole::execute()

Executes the plugin.

Overrides ExecutableInterface::execute

File

modules/social_features/social_group/src/Plugin/Action/ChangeGroupMembershipRole.php, line 85

Class

ChangeGroupMembershipRole
Change group membership role.

Namespace

Drupal\social_group\Plugin\Action

Code

public function execute($entity = NULL) {
  $role = $this->configuration['role'];
  $is_member = $this->configuration['is_member'];
  $update = TRUE;
  $value = [];

  /** @var \Drupal\group\Entity\GroupContentInterface $entity */

  /** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $roles */
  $roles =& $entity
    ->get('group_roles');
  if ($roles
    ->isEmpty() && $is_member) {
    $update = FALSE;
  }
  elseif (!$roles
    ->isEmpty() && !$is_member) {
    $value = $roles
      ->getValue();
    foreach ($value as $item) {
      if ($item['target_id'] === $role) {
        $update = FALSE;
        break;
      }
    }
  }
  if ($update) {
    if (!$is_member) {
      $value[] = [
        'target_id' => $role,
      ];
    }
    $entity
      ->set('group_roles', $value)
      ->save();
  }
  return $this
    ->t('Change roles');
}