You are here

public function EventForm::buildForm in Booking and Availability Management Tools for Drupal 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

modules/bat_event/src/Entity/Form/EventForm.php, line 82
Contains \Drupal\bat_event\Entity\Form\EventForm.

Class

EventForm
Form controller for Event edit forms.

Namespace

Drupal\bat_event\Entity\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $entity = $this->entity;
  $event_type = bat_event_type_load($entity
    ->bundle());
  $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,
    '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';
  }
  if ($event_type
    ->getEventGranularity() == 'bat_daily') {
    $form['event_dates']['widget'][0]['value']['#date_time_element'] = 'none';
    $form['event_dates']['widget'][0]['end_value']['#date_time_element'] = 'none';
  }
  else {
    $widget_type = EntityFormDisplay::load($entity
      ->getEntityTypeId() . '.' . $entity
      ->bundle() . '.' . $form_state
      ->getStorage()['form_display']
      ->getMode())
      ->getComponent('event_dates')['type'];

    // Don't allow entering seconds with the default daterange widget.
    if ($widget_type == 'daterange_default') {
      $form['event_dates']['widget'][0]['value']['#date_increment'] = 60;
      $form['event_dates']['widget'][0]['end_value']['#date_increment'] = 60;
    }
  }
  $form['event_dates']['widget'][0]['value']['#date_timezone'] = 'UTC';
  $form['event_dates']['widget'][0]['end_value']['#date_timezone'] = 'UTC';
  if (isset($form['event_dates']['widget'][0]['value']['#default_value'])) {
    $form['event_dates']['widget'][0]['value']['#default_value']
      ->setTimezone(new \DateTimeZone('UTC'));
  }
  if (isset($form['event_dates']['widget'][0]['end_value']['#default_value'])) {
    $form['event_dates']['widget'][0]['end_value']['#default_value']
      ->setTimezone(new \DateTimeZone('UTC'));
  }
  if ($this->request->query
    ->get(MainContentViewSubscriber::WRAPPER_FORMAT) == 'drupal_ajax') {
    $form['actions']['submit']['#attributes']['class'][] = 'use-ajax-submit';
    $form['actions']['delete']['#access'] = FALSE;
  }
  return $form;
}