You are here

public function EventSeriesDeleteEventsForm::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/EventSeriesDeleteEventsForm.php, line 81
Contains \Drupal\bat_event_series\Entity\Form\EventSeriesDeleteEventsForm.

Class

EventSeriesDeleteEventsForm
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();
  $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($future_events)) {
    $form['delete_events']['future_events'] = [
      '#markup' => '<h3>' . $this
        ->t('There are no upcoming events to delete!') . '</h3>',
    ];
    $form['description']['#access'] = FALSE;
    $form['actions']['submit']['#disabled'] = TRUE;
  }
  else {
    $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['events'] = [
      '#type' => 'hidden',
      '#value' => $future_events,
    ];
  }
  $form['description']['#weight'] = 1;
  return $form;
}