You are here

public function EditRepeatingRuleModalForm::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 FormInterface::buildForm

File

modules/bat_event_series/src/Form/EditRepeatingRuleModalForm.php, line 82
Contains \Drupal\bat_event_series\Form\EditRepeatingRuleModalForm.

Class

EditRepeatingRuleModalForm

Namespace

Drupal\bat_event_series\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, EventSeries $bat_event_series = NULL) {
  $this->event_series = $bat_event_series;
  $event_series_type = bat_event_series_type_load($bat_event_series
    ->bundle());
  $start_date = new \DateTime($bat_event_series
    ->get('event_dates')->value);
  $end_date = new \DateTime($bat_event_series
    ->get('event_dates')->end_value);
  $rrule = RfcParser::parseRRule($bat_event_series
    ->getRRule());
  $event_type = $event_series_type
    ->getEventGranularity();
  $form['errors'] = [
    '#markup' => '<div class="form-validation-errors"></div>',
  ];
  $form['start_date'] = [
    '#type' => $event_type == 'bat_daily' ? 'date' : 'datetime',
    '#title' => t('Start date'),
    '#default_value' => $event_type == 'bat_daily' ? $start_date
      ->format('Y-m-d') : new DrupalDateTime($start_date
      ->format('Y-m-d H:00')),
    '#date_increment' => 60,
    '#required' => TRUE,
  ];
  $form['end_date'] = [
    '#type' => $event_series_type
      ->getEventGranularity() == 'bat_daily' ? 'date' : 'datetime',
    '#title' => t('End date'),
    '#default_value' => $event_type == 'bat_daily' ? $end_date
      ->format('Y-m-d') : new DrupalDateTime($end_date
      ->format('Y-m-d H:00')),
    '#date_increment' => 60,
    '#required' => TRUE,
  ];
  $form['repeat_frequency'] = [
    '#type' => 'select',
    '#title' => t('Repeat frequency'),
    '#options' => [
      'daily' => t('Daily'),
      'weekly' => t('Weekly'),
      'monthly' => t('Monthly'),
    ],
    '#default_value' => isset($rrule['FREQ']) ? strtolower($rrule['FREQ']) : '',
    '#required' => TRUE,
  ];
  $form['repeat_until'] = [
    '#type' => 'date',
    '#title' => t('Repeat until'),
    '#default_value' => isset($rrule['UNTIL']) ? $rrule['UNTIL']
      ->format('Y-m-d') : '',
    '#required' => TRUE,
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Update this event series'),
    '#attributes' => [
      'class' => [
        'button--primary',
      ],
    ],
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxSubmit',
      ],
      'url' => Url::fromRoute('entity.bat_event_series.edit_form_modal', [
        'bat_event_series' => $bat_event_series
          ->id(),
      ]),
      'options' => [
        'query' => [
          FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
        ],
      ],
    ],
  ];
  $form['actions']['cancel'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Cancel'),
    '#attributes' => [
      'class' => [
        'button--danger',
        'dialog-cancel',
      ],
    ],
  ];
  $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
  return $form;
}