You are here

public function RemoveUserFromGroupAction::execute in Business Rules 8

Same name and namespace in other branches
  1. 2.x modules/br_group/src/Plugin/BusinessRulesAction/RemoveUserFromGroupAction.php \Drupal\br_group\Plugin\BusinessRulesAction\RemoveUserFromGroupAction::execute()

Execute the action.

Parameters

\Drupal\business_rules\ActionInterface $action: The configured action.

\Drupal\business_rules\Events\BusinessRulesEvent $event: The event that has triggered the action.

Return value

array The render array to be showed on debug block.

Overrides BusinessRulesActionPlugin::execute

File

modules/br_group/src/Plugin/BusinessRulesAction/RemoveUserFromGroupAction.php, line 111

Class

RemoveUserFromGroupAction
Class RemoveUserFromGroupAction.

Namespace

Drupal\br_group\Plugin\BusinessRulesAction

Code

public function execute(ActionInterface $action, BusinessRulesEvent $event) {
  $variables = $event
    ->getArgument('variables');
  $group_id = $action
    ->getSettings('group_id');
  $group_id = $this
    ->processVariables($group_id, $variables);
  $user_key = $action
    ->getSettings('user_key');
  $user_name = $action
    ->getSettings('user_name');
  $user_name = $this
    ->processVariables($user_name, $variables);
  $user_id = $action
    ->getSettings('user_id');
  $user_id = $this
    ->processVariables($user_id, $variables);
  if ($user_key == 'userid') {
    $user = User::load($user_id);
  }
  else {
    $id = $this->util->container
      ->get('entity_type.manager')
      ->getStorage('user')
      ->getQuery()
      ->condition('name', $user_name)
      ->execute();
    $user = User::load(array_values($id)[0]);
  }
  $group = Group::load($group_id);
  $member = $group
    ->getMember($user);
  if ($member instanceof GroupMembership) {
    $group
      ->removeMember($user);
    $result = [
      '#type' => 'markup',
      '#markup' => t('User %user has been removed from group %group.', [
        '%user' => $user
          ->getUsername(),
        '%group' => $group
          ->label(),
      ]),
    ];
  }
  else {
    $result = [
      '#type' => 'markup',
      '#markup' => t('User %user is not member of group %group.', [
        '%user' => $user
          ->getUsername(),
        '%group' => $group
          ->label(),
      ]),
    ];
  }
  return $result;
}