entity_generic-8.x-3.x-dev/src/Form/GenericConfigForm.php
src/Form/GenericConfigForm.php
<?php
namespace Drupal\entity_generic\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form controller for config entity forms.
*/
class GenericConfigForm extends EntityForm {
/**
* Creates a class instance.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$entity = $this->entity;
$entity_type = $entity->getEntityType();
$entity_type_label = $entity_type->getLabel();
if ($this->operation == 'add') {
$form['#title'] = $this->t('Add %type', ['%type' => $entity_type_label]);
}
else {
$form['#title'] = $this->t('Edit %label %type', [
'%label' => $entity->label(),
'%type' => $entity_type_label,
]);
}
$form['label'] = [
'#title' => t('Label'),
'#type' => 'textfield',
'#default_value' => $entity->label(),
'#description' => $this->t('The human-readable name of this entity.'),
'#required' => TRUE,
'#size' => 30,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $entity->id(),
'#maxlength' => EntityTypeInterface::ID_MAX_LENGTH,
'#disabled' => !$entity->isNew(),
'#machine_name' => [
'exists' => [$this, 'exists'],
],
'#description' => $this->t('A unique machine-readable name for this entity. It must only contain lowercase letters, numbers, and underscores.'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$entity_type = $entity->getEntityType();
$entity_type_label = $entity_type->getLabel();
$entity->set('id', trim($entity->id()));
$entity->set('label', trim($entity->label()));
$status = $entity->save();
$messenger = \Drupal::messenger();
if ($status == SAVED_UPDATED) {
$messenger->addMessage($this->t('%type %label has been updated.', [
'%label' => $entity->label(),
'%type' => $entity_type_label,
]));
}
else {
$messenger->addMessage($this->t('%type %label has been created.', [
'%label' => $entity->label(),
'%type' => $entity_type_label,
]));
}
$form_state->setRedirect('entity.' . $entity_type->id() . '.collection');
}
/**
* Check whether the entity type exists.
*
* @param int $id
* Id if the entity to check.
*
* @return bool
* TRUE if given entity exists.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function exists($id) {
return !empty($this->entityTypeManager->getStorage($this->entity->getEntityType()->id())->load($id));
}
}
