You are here

public function RoleDelegationSettingsForm::submitForm in Role Delegation 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/RoleDelegationSettingsForm.php, line 91

Class

RoleDelegationSettingsForm
Configure book settings for this site.

Namespace

Drupal\role_delegation\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\user\UserInterface $account */
  $account = $form_state
    ->getBuildInfo()['args'][0];

  // Make sure this functionality works when single_user_role is enabled.
  // This module can change the role_change form element to a select or
  // radio buttons, which will return an single value instead of the default
  // checkboxes.
  $assigned_roles = is_array($form_state
    ->getValue('role_change')) ? $form_state
    ->getValue('role_change') : [
    $form_state
      ->getValue('role_change') => $form_state
      ->getValue('role_change'),
  ];
  $assignable_roles = $this->delegatableRoles
    ->getAssignableRoles($this->currentUser);
  $roles = [];
  foreach ($assignable_roles as $rid => $assignable_role) {
    $roles[$rid] = isset($assigned_roles[$rid]) && !empty($assigned_roles[$rid]) ? $rid : 0;
  }
  foreach ($roles as $rid => $value) {
    empty($value) === TRUE ? $account
      ->removeRole($rid) : $account
      ->addRole($rid);
  }
  $account
    ->save();
  $this
    ->messenger()
    ->addStatus($this
    ->t('The roles have been updated.'));
}