You are here

BadgeForm.php in User Badges 8

File

src/Entity/Form/BadgeForm.php
View source
<?php

/**
 * @file
 * Contains \Drupal\user_badges\Entity\Form\BadgeForm.
 */
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;

/**
 * Form controller for Badge edit forms.
 *
 * @ingroup user_badges
 */
class BadgeForm extends ContentEntityForm {

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    /* @var $entity \Drupal\user_badges\Entity\Badge */
    $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;
  }

  /**
   * {@inheritdoc}
   */
  public function submit(array $form, FormStateInterface $form_state) {

    // Build the entity object from the submitted values.
    $entity = parent::submit($form, $form_state);
    return $entity;
  }

  /**
   * {@inheritdoc}
   */
  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(),
    ]);
  }

}

Classes

Namesort descending Description
BadgeForm Form controller for Badge edit forms.