Chris@0: entity; Chris@0: $form['label'] = [ Chris@0: '#type' => 'textfield', Chris@0: '#title' => $this->t('Role name'), Chris@0: '#default_value' => $entity->label(), Chris@0: '#size' => 30, Chris@0: '#required' => TRUE, Chris@0: '#maxlength' => 64, Chris@0: '#description' => $this->t('The name for this role. Example: "Moderator", "Editorial board", "Site architect".'), Chris@0: ]; Chris@0: $form['id'] = [ Chris@0: '#type' => 'machine_name', Chris@0: '#default_value' => $entity->id(), Chris@0: '#required' => TRUE, Chris@0: '#disabled' => !$entity->isNew(), Chris@0: '#size' => 30, Chris@0: '#maxlength' => 64, Chris@0: '#machine_name' => [ Chris@0: 'exists' => ['\Drupal\user\Entity\Role', 'load'], Chris@0: ], Chris@0: ]; Chris@0: $form['weight'] = [ Chris@0: '#type' => 'value', Chris@0: '#value' => $entity->getWeight(), Chris@0: ]; Chris@0: Chris@0: return parent::form($form, $form_state, $entity); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function save(array $form, FormStateInterface $form_state) { Chris@0: $entity = $this->entity; Chris@0: Chris@0: // Prevent leading and trailing spaces in role names. Chris@0: $entity->set('label', trim($entity->label())); Chris@0: $status = $entity->save(); Chris@0: Chris@18: $edit_link = $this->entity->toLink($this->t('Edit'), 'edit-form')->toString(); Chris@0: if ($status == SAVED_UPDATED) { Chris@17: $this->messenger()->addStatus($this->t('Role %label has been updated.', ['%label' => $entity->label()])); Chris@0: $this->logger('user')->notice('Role %label has been updated.', ['%label' => $entity->label(), 'link' => $edit_link]); Chris@0: } Chris@0: else { Chris@17: $this->messenger()->addStatus($this->t('Role %label has been added.', ['%label' => $entity->label()])); Chris@0: $this->logger('user')->notice('Role %label has been added.', ['%label' => $entity->label(), 'link' => $edit_link]); Chris@0: } Chris@0: $form_state->setRedirect('entity.user_role.collection'); Chris@0: } Chris@0: Chris@0: }