You are here

BookingBundleForm.php in Booking and Availability Management Tools for Drupal 8

Namespace

Drupal\bat_booking

File

modules/bat_booking/src/BookingBundleForm.php
View source
<?php

/**
 * @file
 * Contains \Drupal\bat_booking\BookingBundleForm.
 */
namespace Drupal\bat_booking;

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 type bundle forms.
 */
class BookingBundleForm 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_booking\\Entity\\BookingBundle',
          'load',
        ],
        'source' => [
          'name',
        ],
      ],
      '#description' => t('A unique machine-readable name for this type. It must only contain lowercase letters, numbers, and underscores.'),
    ];
    $form['advanced'] = [
      '#type' => 'vertical_tabs',
      '#attributes' => [
        'class' => [
          'entity-meta',
        ],
      ],
      '#weight' => 99,
    ];
    return $this
      ->protectBundleIdElement($form);
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);
    $actions['submit']['#value'] = t('Save type bundle');
    $actions['delete']['#value'] = t('Delete type 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 type bundle %name has been updated.', $t_args));
    }
    elseif ($status == SAVED_NEW) {
      $this
        ->messenger()
        ->addMessage(t('The type bundle %name has been added.', $t_args));
    }
    $form_state
      ->setRedirectUrl($type
      ->toUrl('collection'));
  }

}

Classes

Namesort descending Description
BookingBundleForm Form handler for type bundle forms.