public function AddUserToGroupAction::execute in Business Rules 2.x
Same name and namespace in other branches
- 8 modules/br_group/src/Plugin/BusinessRulesAction/AddUserToGroupAction.php \Drupal\br_group\Plugin\BusinessRulesAction\AddUserToGroupAction::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/ AddUserToGroupAction.php, line 111
Class
- AddUserToGroupAction
- Class AddUserToGroupAction.
Namespace
Drupal\br_group\Plugin\BusinessRulesActionCode
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
->addMember($user);
$result = [
'#type' => 'markup',
'#markup' => t('User %user has been added on group %group.', [
'%user' => $user
->getAccountName(),
'%group' => $group
->label(),
]),
];
}
else {
$result = [
'#type' => 'markup',
'#markup' => t('User %user is already member of group %group.', [
'%user' => $user
->getAccountName(),
'%group' => $group
->label(),
]),
];
}
return $result;
}