View source
<?php
namespace Drupal\custom_pub\Form;
use Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CustomPublishingOptionForm extends EntityForm {
protected $entityDefinitionManager;
protected $entityTypeManager;
public function __construct(EntityDefinitionUpdateManagerInterface $entity_definition_manager, EntityTypeManagerInterface $entity_type_manager) {
$this->entityDefinitionManager = $entity_definition_manager;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
$entity_definition_manager = $container
->get('entity.definition_update_manager');
$entity_type_manager = $container
->get('entity_type.manager');
return new static($entity_definition_manager, $entity_type_manager);
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $this->entity
->label(),
'#description' => $this
->t("The label for this publishing option."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $this->entity
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\custom_pub\\Entity\\CustomPublishingOption::load',
],
'#disabled' => !$this->entity
->isNew(),
];
$form['publish_under_promote_options'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Place under "Promote options"'),
'#size' => 10,
'#maxlength' => 255,
'#default_value' => $this->entity
->isPublishUnderPromoteOptions(),
];
$form['description'] = [
'#type' => 'textarea',
'#title' => $this
->t('Description'),
'#maxlength' => 255,
'#default_value' => $this->entity
->getDescription(),
'#description' => $this
->t("Add a description for this publishing option."),
'#required' => TRUE,
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$entity = $this->entity;
$storage_definition = $this->entityDefinitionManager
->getFieldStorageDefinition($entity
->id(), 'node');
if ($entity
->isNew() && isset($storage_definition)) {
$form_state
->setError($form['id'], $this
->t('Cannot use machine name %name - field definition already exists.', [
'%name' => $entity
->id(),
]));
}
}
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$label = $entity
->label();
$status = $entity
->save();
switch ($status) {
case SAVED_NEW:
$this
->messenger()
->addMessage($this
->t('Created the %label custom publishing option.', [
'%label' => $label,
]));
break;
default:
$this
->messenger()
->addMessage($this
->t('Saved the %label custom publishing option.', [
'%label' => $label,
]));
}
$form_state
->setRedirectUrl($entity
->toUrl('collection'));
}
}