You are here

public static function DatexDateTime::valueCallback in Datex 8

Determines how user input is mapped to an element's #value property.

Parameters

array $element: An associative array containing the properties of the element.

mixed $input: The incoming input to populate the form element. If this is FALSE, the element's default value should be returned.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

mixed The value to assign to the element.

Overrides Datetime::valueCallback

1 call to DatexDateTime::valueCallback()
DatexTimestampDatetimeNoDefaultWidget::valueCallback in src/Plugin/Field/FieldWidget/DatexTimestampDatetimeNoDefaultWidget.php
Callback function to add default time to the input date if needed.

File

src/Element/DatexDateTime.php, line 25

Class

DatexDateTime
Plugin annotation @FormElement("datetime");

Namespace

Drupal\datex\Element

Code

public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  $e_cal = datex_factory();
  if (!$e_cal) {
    return parent::valueCallback($element, $input, $form_state);
  }
  if ($input !== FALSE) {
    $date_input = $element['#date_date_element'] != 'none' && !empty($input['date']) ? $input['date'] : '';
    $time_input = $element['#date_time_element'] != 'none' && !empty($input['time']) ? $input['time'] : '';
    $date_format = $element['#date_date_element'] != 'none' ? static::getHtml5DateFormat($element) : '';
    $time_format = $element['#date_time_element'] != 'none' ? static::getHtml5TimeFormat($element) : '';
    $timezone = !empty($element['#date_timezone']) ? $element['#date_timezone'] : NULL;
    $e_cal = datex_factory($timezone, 'en');

    // Seconds will be omitted in a post in case there's no entry.
    $date = NULL;
    if (!empty($time_input) && strlen($time_input) == 5) {
      $time_input .= ':00';
    }
    try {
      $date_time_format = trim($date_format . ' ' . $time_format);
      $date_time_input = trim($date_input . ' ' . $time_input);
      if (empty($date_time_input)) {

        // pass
      }
      elseif ($e_cal
        ->parse($date_time_input, $date_time_format)) {
        $date = DrupalDateTime::createFromTimestamp($e_cal
          ->getTimestamp(), $timezone);
      }
      else {
        $form_state
          ->setError($element, t('Date is not valid.'));
      }
    } catch (\Exception $e) {
      $date = NULL;
    }
    $input = [
      'date' => $date_input,
      'time' => $time_input,
      'object' => $date,
    ];
  }
  else {
    $date = $element['#default_value'];
    if ($date) {
      $date = DatexDrupalDateTime::convert($date);
    }
    if ($date instanceof DrupalDateTime && !$date
      ->hasErrors()) {
      $input = [
        'date' => $date
          ->format($element['#date_date_format']),
        'time' => $date
          ->format($element['#date_time_format']),
        'object' => $date,
      ];
    }
    else {
      $input = [
        'date' => '',
        'time' => '',
        'object' => NULL,
      ];
    }
  }
  return $input;
}