BadgeForm.php in User Badges 8
File
src/Entity/Form/BadgeForm.php
View source
<?php
namespace Drupal\user_badges\Entity\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Messenger\MessengerTrait;
class BadgeForm extends ContentEntityForm {
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$entity = $this->entity;
$form['langcode'] = array(
'#title' => $this
->t('Language'),
'#type' => 'language_select',
'#default_value' => $entity
->language()
->getId(),
'#languages' => LanguageInterface::STATE_ALL,
);
return $form;
}
public function submit(array $form, FormStateInterface $form_state) {
$entity = parent::submit($form, $form_state);
return $entity;
}
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$status = $entity
->save();
switch ($status) {
case SAVED_NEW:
$this
->messenger()
->addMessage($this
->t('Created the %label Badge.', [
'%label' => $entity
->label(),
]), 'status');
break;
default:
$this
->messenger()
->addMessage($this
->t('Saved the %label Badge.', [
'%label' => $entity
->label(),
]), 'status');
}
$form_state
->setRedirect('entity.badge.edit_form', [
'badge' => $entity
->id(),
]);
}
}