View source
<?php
namespace Drupal\eu_cookie_compliance\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CookieCategoryForm extends EntityForm {
protected $categoryStorageManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
$this->categoryStorageManager = $entityTypeManager
->getStorage('cookie_category');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$cookie_category = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $cookie_category
->label(),
'#description' => $this
->t("The name that will be shown to the website visitor."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $cookie_category
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\eu_cookie_compliance\\Entity\\CookieCategory::load',
],
'#changeable_state' => !$cookie_category
->isNew(),
];
$form['description'] = [
'#type' => 'textarea',
'#title' => $this
->t('Description'),
'#default_value' => $cookie_category
->get('description'),
'#description' => $this
->t("The description that will be shown to the website visitor."),
'#required' => FALSE,
];
$form['checkbox_default_state'] = [
'#type' => 'radios',
'#title' => $this
->t('Checkbox default state'),
'#description' => $this
->t("Determines the default state of this category's selection checkbox on the cookie consent popup."),
'#default_value' => $cookie_category
->get('checkbox_default_state') ?: 'unchecked',
'#options' => $this->categoryStorageManager
->getCheckboxDefaultStateOptionsList(),
'#required' => TRUE,
];
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$cookie_category = $this->entity;
$status = $cookie_category
->save();
switch ($status) {
case SAVED_NEW:
$this
->messenger()
->addMessage($this
->t('Created the %label Cookie category.', [
'%label' => $cookie_category
->label(),
]));
break;
default:
$this
->messenger()
->addMessage($this
->t('Saved the %label Cookie category.', [
'%label' => $cookie_category
->label(),
]));
}
$form_state
->setRedirectUrl($cookie_category
->toUrl('collection'));
}
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
parent::copyFormValuesToEntity($entity, $form, $form_state);
if (empty($entity
->getWeight())) {
$entity
->setWeight($this->categoryStorageManager
->getCookieCategoryNextWeight());
}
}
}