You are here

public function AppointmentCalendarEditForm::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/AppointmentCalendarEditForm.php, line 22

Class

AppointmentCalendarEditForm

Namespace

Drupal\appointment_calendar\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $from_date = \Drupal::request()->query
    ->get('date');

  // Date edit page.
  if ($from_date != '') {
    $form['appointment_slot_date'] = [
      '#type' => 'datetime',
      '#title' => $this
        ->t('Date'),
      '#date_date_element' => 'date',
      '#date_time_element' => 'none',
      '#default_value' => DrupalDateTime::createFromTimestamp($from_date),
      '#disabled' => TRUE,
    ];

    // Fetching Slot previous capacity filled.
    $capacity = appointment_calendar_slot_capacity($from_date);
    if ($capacity) {
      $i = 1;

      // Show slots and capacity.
      foreach (json_decode($capacity) as $key => $value) {

        // Check if any appointment booked.
        $slot_check = appointment_calendar_slot_capacity_value($from_date, $key);
        $form['time_slot_' . $i] = [
          '#type' => 'textfield',
          '#title' => Html::escape('Time Slot ' . $i . ' :'),
          '#description' => t('Ex: 10:00-11:00, 13:00-14:00, etc.,'),
          '#default_value' => $key,
          '#prefix' => '<div class="time-slot-field-form">',
        ];
        if ($slot_check > 0) {
          $form['time_slot_' . $i]['#disabled'] = TRUE;
          $form['time_slot_' . $i]['#description'] = t('<b>Slot :i </b>booked atleast once', [
            ':i' => $i,
          ]);
        }
        $form['time_slot_' . $i . '_capacity'] = [
          '#type' => 'textfield',
          '#title' => Html::escape('Slot ' . $i . ' Capacity'),
          '#description' => t('Only Numeric'),
          '#default_value' => $value,
          '#suffix' => '</div>',
        ];
        $i++;
      }
    }
    $form['appointment_slot'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('No of Extra Slots:'),
    ];
    $values = $form_state
      ->getValues();

    // Display Extra slots.
    if (!empty($values)) {
      $extra_slots = $values['appointment_slot'];
      $extra_slots += $i - 1;
      for ($j = $i; $j <= $extra_slots; $j++) {
        $form['slots']['time_slot_' . $j] = [
          '#type' => 'textfield',
          '#title' => Html::escape('Time Slot ' . $j . ' :'),
          '#description' => t('Ex: 10:00-11:00, 13:00-14:00, etc.,'),
          '#default_value' => '',
          '#prefix' => '<div class="time-slot-field-form">',
        ];
        $form['slots']['time_slot_' . $j . '_capacity'] = [
          '#type' => 'textfield',
          '#title' => Html::escape('Slot ' . $j . ' Capacity'),
          '#description' => t('Only Numeric'),
          '#default_value' => '',
          '#suffix' => '</div>',
        ];
      }
      $j++;
    }
    $form['add_more'] = [
      '#type' => 'submit',
      '#value' => t('Add More Slots'),
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => t('Submit'),
    ];
    return $form;
  }
}