View source
<?php
namespace Drupal\bat_booking\Entity\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\bat_booking\BookingInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BookingForm extends ContentEntityForm {
protected $dateFormatter;
public function __construct(EntityRepositoryInterface $entity_repository, DateFormatterInterface $date_formatter, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->dateFormatter = $date_formatter;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('date.formatter'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$entity = $this->entity;
$form['changed'] = [
'#type' => 'hidden',
'#default_value' => $entity
->getChangedTime(),
];
$form['#theme'] = [
'bat_entity_edit_form',
];
$form['#attached']['library'][] = 'bat/bat_ui';
$form['advanced'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'entity-meta',
],
],
'#weight' => 99,
];
$is_new = !$entity
->isNew() ? $this->dateFormatter
->format($entity
->getChangedTime(), 'short') : t('Not saved yet');
$form['meta'] = [
'#attributes' => [
'class' => [
'entity-meta__header',
],
],
'#type' => 'container',
'#group' => 'advanced',
'#weight' => -100,
'published' => [
'#type' => 'html_tag',
'#tag' => 'h3',
'#value' => $entity
->getStatus() ? t('Published') : t('Not published'),
'#access' => !$entity
->isNew(),
'#attributes' => [
'class' => 'entity-meta__title',
],
],
'changed' => [
'#type' => 'item',
'#wrapper_attributes' => [
'class' => [
'entity-meta__last-saved',
'container-inline',
],
],
'#markup' => '<h4 class="label inline">' . t('Last saved') . '</h4> ' . $is_new,
],
'author' => [
'#type' => 'item',
'#wrapper_attributes' => [
'class' => [
'author',
'container-inline',
],
],
'#markup' => '<h4 class="label inline">' . t('Author') . '</h4> ' . $entity
->getOwner()
->getDisplayName(),
],
];
$form['author'] = [
'#type' => 'details',
'#title' => t('Authoring information'),
'#group' => 'advanced',
'#attributes' => [
'class' => [
'type-form-author',
],
],
'#weight' => 90,
'#optional' => TRUE,
'#open' => TRUE,
];
if (isset($form['uid'])) {
$form['uid']['#group'] = 'author';
}
if (isset($form['created'])) {
$form['created']['#group'] = 'author';
}
$form['#entity_builders']['update_status'] = [
$this,
'updateStatus',
];
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$status = $entity
->save();
switch ($status) {
case SAVED_NEW:
$this
->messenger()
->addMessage($this
->t('Created the %label Booking.', [
'%label' => $entity
->label(),
]));
break;
default:
$this
->messenger()
->addMessage($this
->t('Saved the %label Booking.', [
'%label' => $entity
->label(),
]));
}
$form_state
->setRedirect('entity.bat_booking.edit_form', [
'bat_booking' => $entity
->id(),
]);
}
protected function actions(array $form, FormStateInterface $form_state) {
$element = parent::actions($form, $form_state);
$entity = $this->entity;
$element['publish'] = $element['submit'];
$element['publish']['#published_status'] = TRUE;
$element['publish']['#dropbutton'] = 'save';
if ($entity
->isNew()) {
$element['publish']['#value'] = t('Save and publish');
}
else {
$element['publish']['#value'] = $entity
->getStatus() ? t('Save and keep published') : t('Save and publish');
}
$element['publish']['#weight'] = 0;
$element['unpublish'] = $element['submit'];
$element['unpublish']['#published_status'] = FALSE;
$element['unpublish']['#dropbutton'] = 'save';
if ($entity
->isNew()) {
$element['unpublish']['#value'] = t('Save as unpublished');
}
else {
$element['unpublish']['#value'] = !$entity
->getStatus() ? t('Save and keep unpublished') : t('Save and unpublish');
}
$element['unpublish']['#weight'] = 10;
if ($entity
->getStatus()) {
unset($element['unpublish']['#button_type']);
}
else {
unset($element['publish']['#button_type']);
$element['unpublish']['#weight'] = -10;
}
$element['submit']['#access'] = FALSE;
return $element;
}
public function updateStatus($entity_type_id, BookingInterface $booking_type, array $form, FormStateInterface $form_state) {
$element = $form_state
->getTriggeringElement();
if (isset($element['#published_status'])) {
$booking_type
->setStatus($element['#published_status']);
}
}
}