SpamicideForm.php in Spamicide 8
File
src/Form/SpamicideForm.php
View source
<?php
namespace Drupal\spamicide\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class SpamicideForm extends EntityForm {
protected $requestStack;
public function __construct(RequestStack $request_stack) {
$this->requestStack = $request_stack;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('request_stack'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$spamicide = $this->entity;
$request = $this->requestStack
->getCurrentRequest();
if ($spamicide
->isNew() && !$spamicide
->id() && $request->query
->has('form_id')) {
$spamicide
->set('formId', $request->query
->get('form_id'));
$spamicide
->set('label', $request->query
->get('form_id'));
}
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Form ID name'),
'#maxlength' => 255,
'#default_value' => $spamicide
->label(),
'#description' => $this
->t('Label for the spamicide.'),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $spamicide
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\spamicide\\Entity\\Spamicide::load',
],
'#disabled' => !$spamicide
->isNew(),
];
$form['status'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enabled'),
'#default_value' => $spamicide
->status(),
];
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$result = parent::save($form, $form_state);
$message_args = [
'%label' => $this->entity
->label(),
];
$message = $result == SAVED_NEW ? $this
->t('Created new spamicide %label.', $message_args) : $this
->t('Updated spamicide %label.', $message_args);
$this
->messenger()
->addStatus($message);
$form_state
->setRedirectUrl($this->entity
->toUrl('collection'));
return $result;
}
}