You are here

public function EventSeriesForm::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_series/src/Entity/Form/EventSeriesForm.php, line 83
Contains \Drupal\bat_event_series\Entity\Form\EventSeriesForm.

Class

EventSeriesForm
Form controller for Event edit forms.

Namespace

Drupal\bat_event_series\Entity\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $entity = $this->entity;
  if (!$entity
    ->isNew() && isset($form['rrule'])) {
    $form['rrule']['edit'] = [
      '#type' => 'button',
      '#value' => $this
        ->t('Edit repeating rule'),
      '#limit_validation_errors' => [],
      '#ajax' => [
        'callback' => '::editRepeatingRuleFormSubmitAjax',
        'event' => 'click',
      ],
      '#prefix' => '<div>',
      '#suffix' => '</div>',
    ];
  }
  $event_series_type = bat_event_series_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_series_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 (isset($form['actions']['delete'])) {
    $form['actions']['delete']['#title'] = $this
      ->t('Delete Event Series');
    $form['actions']['delete_events'] = [
      '#type' => 'link',
      '#title' => t('Delete remaining events in this series'),
      '#url' => Url::fromRoute('entity.bat_event_series.delete_events_form', [
        'bat_event_series' => $entity
          ->id(),
      ]),
      '#attributes' => [
        'class' => [
          'button',
          'button--danger',
        ],
      ],
      '#weight' => 999,
    ];
  }
  $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
  return $form;
}