You are here

public function EventSeriesDeleteForm::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 ContentEntityConfirmFormBase::buildForm

File

modules/bat_event_series/src/Entity/Form/EventSeriesDeleteForm.php, line 81
Contains \Drupal\bat_event_series\Entity\Form\EventSeriesDeleteForm.

Class

EventSeriesDeleteForm
Provides a form for deleting Event series entities.

Namespace

Drupal\bat_event_series\Entity\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $entity = $this
    ->getEntity();
  $query = $this->entityTypeManager
    ->getStorage('bat_event')
    ->getQuery()
    ->condition('event_series.target_id', $entity
    ->id())
    ->condition('event_dates.value', date('Y-m-d\\TH:i:s'), '>');
  $future_events = $query
    ->execute();
  $query = $this->entityTypeManager
    ->getStorage('bat_event')
    ->getQuery()
    ->condition('event_series.target_id', $entity
    ->id())
    ->condition('event_dates.value', date('Y-m-d\\TH:i:s'), '<=');
  $past_events = $query
    ->execute();
  $form['delete_events'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Events'),
    '#open' => TRUE,
  ];
  $date_format = $this
    ->configFactory()
    ->get('bat.settings')
    ->get('bat_date_format') ?: 'Y-m-d H:i';
  if (!empty($past_events)) {
    $form['delete_events']['past_events'] = [
      '#theme' => 'item_list',
      '#title' => $this
        ->t('The following events will no longer be connected:'),
      '#items' => [],
    ];
    foreach (bat_event_load_multiple($past_events) as $event) {
      $form['delete_events']['past_events']['#items'][$event
        ->id()] = t('from @start to @end', [
        '@start' => $event
          ->getStartDate()
          ->format($date_format),
        '@end' => $event
          ->getEndDate()
          ->format($date_format),
      ]);
    }
  }
  if (!empty($future_events)) {
    if (isset($form['delete_events']['past_events'])) {
      $form['delete_events']['past_events']['#suffix'] = '<br>';
    }
    $form['delete_events']['future_events'] = [
      '#theme' => 'item_list',
      '#title' => $this
        ->t('The following events will be deleted:'),
      '#items' => [],
    ];
    foreach (bat_event_load_multiple($future_events) as $event) {
      $form['delete_events']['future_events']['#items'][$event
        ->id()] = t('from @start to @end', [
        '@start' => $event
          ->getStartDate()
          ->format($date_format),
        '@end' => $event
          ->getEndDate()
          ->format($date_format),
      ]);
    }
    $form['future_events'] = [
      '#type' => 'hidden',
      '#value' => $future_events,
    ];
    $form['past_events'] = [
      '#type' => 'hidden',
      '#value' => $past_events,
    ];
  }
  $form['description']['#weight'] = 1;
  return $form;
}