You are here

function appointment_calendar_form_alter in Appointment Calendar 8

Same name and namespace in other branches
  1. 7 appointment_calendar.module \appointment_calendar_form_alter()

implements hook_form_alter().

File

./appointment_calendar.module, line 8

Code

function appointment_calendar_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'node_appointment_calendar_form' || $form_id == 'node_appointment_calendar_edit_form') {
    if (isset($form_state
      ->getValue('field_appointment_date')[0])) {
      $selected_date = strtotime(date('Y-m-d', $form_state
        ->getValue('field_appointment_date')[0]['value']
        ->getTimestamp()));
    }
    else {
      $selected_date = strtotime(date('Y-m-d', $form['field_appointment_date']['widget'][0]['value']['#default_value']
        ->getTimestamp()));
    }
    $current_path = \Drupal::service('path.current')
      ->getPath();
    $path_args = explode('/', $current_path);

    // Check for edit page.
    if ($path_args[1] == 'node' && is_numeric($path_args[2])) {
      $nid = $path_args[2];
      $default_value = appointment_calendar_slot_value($nid);
    }
    $options = [];

    // Get slot values.
    $slot_result = get_slot_values($selected_date);
    foreach ((array) json_decode($slot_result) as $key => $value) {
      $options[$key] = $key;
    }
    $form['field_appointment_date']['widget'][0]['value']['#ajax'] = [
      'callback' => 'appointment_calendar_appointment_calendar_timeslot_callback_form',
      'wrapper' => 'slot-check',
      'method' => 'replace',
      'effect' => 'fade',
    ];
    $form['data']['field_appointment_slot'] = [
      '#type' => 'select',
      '#title' => t('Appointment Slot'),
      '#weight' => $form['field_appointment_date']['#weight'],
      '#options' => $options,
      '#default_value' => isset($default_value) ? $default_value : '',
    ];
    $form['appointment_fill'] = [
      '#type' => 'button',
      '#value' => t('Check Slot'),
      '#weight' => $form['field_appointment_date']['#weight'],
      '#ajax' => [
        'callback' => 'appointment_calendar_appointment_calendar_timeslot_callback_form',
        'wrapper' => 'slot-check',
        'method' => 'replace',
        'effect' => 'fade',
      ],
    ];
    $values = $form_state
      ->getValues();
    if (!empty($values)) {
      $selected_date = strtotime(date('Y-m-d', $values['field_appointment_date'][0]['value']
        ->getTimestamp()));
      $slot_result = get_slot_values($selected_date);
      foreach ((array) json_decode($slot_result) as $key => $value) {
        $options[$key] = $key;
      }
      if (!empty($slot_result)) {
        $form['data']['field_appointment_slot']['#options'] = $options;
      }
      else {
        $form['data']['field_appointment_slot']['#options'] = '';
      }
    }
    $form['data']['#prefix'] = '<div id="slot-check">';
    $form['data']['#suffix'] = '</div>';
    $form['data']['calendar'] = [
      '#theme' => 'appointment_availability',
      '#selected_date' => date("jS F, Y ", $selected_date),
      '#headers' => $options,
      '#values' => check_slot_availability($selected_date),
    ];

    // Alter submit and validate functions.
    foreach (array_keys($form['actions']) as $action) {
      if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'appointment_calendar_submit';
      }
    }
    $form['#validate'][] = 'appointment_calendar_validate';
  }
}