You are here

StatusTypeForm.php in Heartbeat 8

File

modules/statusmessage/src/Form/StatusTypeForm.php
View source
<?php

namespace Drupal\statusmessage\Form;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\statusmessage\StatusService;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class StatusTypeForm.
 *
 * @package Drupal\statusmessage\Form
 */
class StatusTypeForm extends EntityForm {
  protected $mimeTypes;
  protected $statusService;

  /**
   * {@inheritdoc}
   * @throws \Exception
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('statusservice'));
  }
  public function __construct(StatusService $statusService) {
    $this->statusService = $statusService;
  }
  public function buildForm(array $form, FormStateInterface $form_state) {
    $this->mimeTypes = $this->statusService
      ->getMimeTypes();
    return parent::buildForm($form, $form_state);

    // TODO: Change the autogenerated stub
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $status_type = $this->entity;
    $form['label'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#maxlength' => 255,
      '#default_value' => $status_type
        ->label(),
      '#description' => $this
        ->t("Label for the Status type."),
      '#required' => TRUE,
    );
    $form['id'] = array(
      '#type' => 'machine_name',
      '#default_value' => $status_type
        ->id(),
      '#machine_name' => array(
        'exists' => '\\Drupal\\statusmessage\\Entity\\StatusType::load',
      ),
      '#disabled' => !$status_type
        ->isNew(),
    );
    $form['media'] = array(
      '#type' => 'checkbox',
      '#description' => 'This status message contains media',
      '#default' => $status_type
        ->getMedia(),
    );
    $form['mime'] = array(
      '#type' => 'select',
      '#description' => 'The MIME Type of the media contained in this status message',
      '#options' => array(
        $this->mimeTypes,
      ),
      '#default' => $status_type
        ->getMime(),
    );

    /* You will need additional form elements for your custom properties. */
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $status_type = $this->entity;
    $status_type
      ->setMedia($form_state
      ->getValue('media'));
    if ($status_type
      ->getMedia()) {
      $status_type
        ->setMime($this->mimeTypes[$form_state
        ->getValue('mime')]);
    }
    $status = $status_type
      ->save();
    switch ($status) {
      case SAVED_NEW:
        drupal_set_message($this
          ->t('Created the %label Status type.', [
          '%label' => $status_type
            ->label(),
        ]));
        break;
      default:
        drupal_set_message($this
          ->t('Saved the %label Status type.', [
          '%label' => $status_type
            ->label(),
        ]));
    }
    $form_state
      ->setRedirectUrl($status_type
      ->urlInfo('collection'));
  }

}

Classes

Namesort descending Description
StatusTypeForm Class StatusTypeForm.