You are here

public function AppointmentCalendarForm::buildForm in Appointment Calendar 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

src/Form/AppointmentCalendarForm.php, line 21

Class

AppointmentCalendarForm

Namespace

Drupal\appointment_calendar\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Default year.
  $default_year = date('Y', time());
  $form['appointment_from_date'] = [
    '#type' => 'datetime',
    '#title' => $this
      ->t('Appointment From date'),
    '#default_value' => new DrupalDateTime('now'),
    '#date_date_element' => 'date',
    '#date_time_element' => 'none',
    '#date_year_range' => $default_year . ':+3',
    '#datepicker_options' => [
      'minDate' => 0,
    ],
    '#required' => TRUE,
  ];
  $form['appointment_to_date'] = [
    '#type' => 'datetime',
    '#title' => $this
      ->t('Appointment To date'),
    '#default_value' => new DrupalDateTime('now'),
    '#date_date_element' => 'date',
    '#date_time_element' => 'none',
    '#date_year_range' => $default_year . ':+3',
    '#datepicker_options' => [
      'minDate' => 0,
    ],
    '#required' => TRUE,
  ];
  $form['appointment_slot'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('No of Slots:'),
    '#size' => 10,
    '#maxlength' => 10,
    '#required' => TRUE,
  ];
  $form['appointment_fill'] = [
    '#type' => 'button',
    '#value' => $this
      ->t('Fill Slots'),
    '#weight' => 36,
    '#ajax' => [
      'callback' => '::appointment_calendar_filltime_slots_callback_form',
      'wrapper' => 'time-slot-check',
      'method' => 'replace',
      'effect' => 'fade',
    ],
  ];
  $no_slots = isset($form_state
    ->getValues()['appointment_slot']) ? $form_state
    ->getValues()['appointment_slot'] : 0;
  $form['slots']['#prefix'] = '<div id="time-slot-check">';
  for ($i = 1; $i <= $no_slots; $i++) {
    $form['slots']['time_slot_' . $i] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Time Slot ' . $i . ' :'),
      '#description' => $this
        ->t('Ex: 10:00-11:00, 13:00-14:00, etc.,'),
    ];
    $form['slots']['time_slot_' . $i . '_capacity'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Slot ' . $i . ' Capacity'),
      '#description' => $this
        ->t('Only Numeric'),
    ];
  }
  $form['slots']['#suffix'] = '</div>';
  $form['slots']['#weight'] = 39;
  if ($no_slots != 0) {
    $form['slots']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Submit'),
    ];
    $form['slots']['reset'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Reset'),
    ];
  }
  return $form;
}