UnitBundleForm.php in Booking and Availability Management Tools for Drupal 8
Contains \Drupal\bat_unit\UnitBundleForm.
Namespace
Drupal\bat_unitFile
modules/bat_unit/src/UnitBundleForm.phpView source
<?php
/**
* @file
* Contains \Drupal\bat_unit\UnitBundleForm.
*/
namespace Drupal\bat_unit;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form handler for unit bundle forms.
*/
class UnitBundleForm extends BundleEntityFormBase {
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs the EventTypeForm object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
* The entity manager.
*/
public function __construct(EntityTypeManagerInterface $entity_manager) {
$this->entityTypeManager = $entity_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);
$type = $this->entity;
$form['name'] = [
'#title' => t('Name'),
'#type' => 'textfield',
'#default_value' => $type
->label(),
'#description' => t('The human-readable name of this type.'),
'#required' => TRUE,
'#size' => 30,
];
$form['type'] = [
'#type' => 'machine_name',
'#default_value' => $type
->id(),
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
'#disabled' => FALSE,
'#machine_name' => [
'exists' => [
'Drupal\\bat_unit\\Entity\\UnitBundle',
'load',
],
'source' => [
'name',
],
],
'#description' => t('A unique machine-readable name for this type. It must only contain lowercase letters, numbers, and underscores.'),
];
return $this
->protectBundleIdElement($form);
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = t('Save unit bundle');
$actions['delete']['#value'] = t('Delete unit bundle');
return $actions;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$id = trim($form_state
->getValue('type'));
// '0' is invalid, since elsewhere we check it using empty().
if ($id == '0') {
$form_state
->setErrorByName('type', $this
->t("Invalid machine-readable name. Enter a name other than %invalid.", [
'%invalid' => $id,
]));
}
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$type = $this->entity;
$type
->set('type', trim($type
->id()));
$type
->set('name', trim($type
->label()));
$status = $type
->save();
$t_args = [
'%name' => $type
->label(),
];
if ($status == SAVED_UPDATED) {
$this
->messenger()
->addMessage(t('The unit bundle %name has been updated.', $t_args));
}
elseif ($status == SAVED_NEW) {
$this
->messenger()
->addMessage(t('The unit bundle %name has been added.', $t_args));
}
$form_state
->setRedirectUrl($type
->toUrl('collection'));
}
}
Classes
Name | Description |
---|---|
UnitBundleForm | Form handler for unit bundle forms. |