You are here

public function AssignRoleToUserAction::execute in Business Rules 8

Same name and namespace in other branches
  1. 2.x modules/br_group/src/Plugin/BusinessRulesAction/AssignRoleToUserAction.php \Drupal\br_group\Plugin\BusinessRulesAction\AssignRoleToUserAction::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/AssignRoleToUserAction.php, line 120

Class

AssignRoleToUserAction
Class AssignRoleToUserAction.

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);
  $role_id = $action
    ->getSettings('role_id');
  $role_id = $this
    ->processVariables($role_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) {
    $roles = $member
      ->getRoles();
    $group_type_id = $group
      ->getGroupType()
      ->id();
    $role_id = substr($role_id, 0, strlen("{$group_type_id}-")) == "{$group_type_id}-" ? $role_id : "{$group_type_id}-{$role_id}";
    $new_role = GroupRole::load($role_id);
    unset($roles["{$group_type_id}-member"]);
    $roles_keys = array_keys($roles);
    if (!in_array($role_id, $roles_keys) && $new_role instanceof GroupRole) {
      $roles_keys[] = $role_id;
    }
    foreach ($roles_keys as $key => $value) {
      $roles_keys[$key] = [
        'target_id' => $value,
      ];
    }
    $content = $member
      ->getGroupContent();
    $content
      ->set('group_roles', $roles_keys);
    $content
      ->save();
    $result = [
      '#type' => 'markup',
      '#markup' => t('Role %role has been assigned to user %user on group %group.', [
        '%role' => $new_role
          ->label(),
        '%user' => $user
          ->getUsername(),
        '%group' => $group
          ->label(),
      ]),
    ];
  }
  else {
    $result = [
      '#type' => 'markup',
      '#markup' => t('Role %role could not be assigned to user %user because he is not member of group %group.', [
        '%role' => $role_id,
        '%user' => $user
          ->getUsername(),
        '%group' => $group
          ->label(),
      ]),
    ];
  }
  return $result;
}