function RoleForm::save

Same name in other branches
  1. 8.9.x core/modules/user/src/RoleForm.php \Drupal\user\RoleForm::save()
  2. 10 core/modules/user/src/RoleForm.php \Drupal\user\RoleForm::save()
  3. 11.x core/modules/user/src/RoleForm.php \Drupal\user\RoleForm::save()

Overrides EntityForm::save

File

core/modules/user/src/RoleForm.php, line 51

Class

RoleForm
Form controller for the role entity edit forms.

Namespace

Drupal\user

Code

public function save(array $form, FormStateInterface $form_state) {
    $entity = $this->entity;
    // Prevent leading and trailing spaces in role names.
    $entity->set('label', trim($entity->label()));
    $status = $entity->save();
    $edit_link = $this->entity
        ->toLink($this->t('Edit'), 'edit-form')
        ->toString();
    if ($status == SAVED_UPDATED) {
        $this->messenger()
            ->addStatus($this->t('Role %label has been updated.', [
            '%label' => $entity->label(),
        ]));
        $this->logger('user')
            ->notice('Role %label has been updated.', [
            '%label' => $entity->label(),
            'link' => $edit_link,
        ]);
    }
    else {
        $this->messenger()
            ->addStatus($this->t('Role %label has been added.', [
            '%label' => $entity->label(),
        ]));
        $this->logger('user')
            ->notice('Role %label has been added.', [
            '%label' => $entity->label(),
            'link' => $edit_link,
        ]);
    }
    $form_state->setRedirect('entity.user_role.collection');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.