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 extends EntityForm {
protected $mimeTypes;
protected $statusService;
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);
}
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(),
);
return $form;
}
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'));
}
}