You are here

public static function DatexDate::processDate in Datex 8

Processes a date form element.

Parameters

array $element: The form element to process. Properties used:

  • #attributes: An associative array containing:

    • type: The type of date field rendered.
  • #date_date_format: The date format used in PHP formats.

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

array $complete_form: The complete form structure.

Return value

array The processed element.

Overrides Date::processDate

File

src/Element/DatexDate.php, line 25

Class

DatexDate
Plugin annotation @FormElement("date");

Namespace

Drupal\datex\Element

Code

public static function processDate(&$element, FormStateInterface $form_state, &$complete_form) {
  $element = parent::processDate($element, $form_state, $complete_form);
  $type = $element['#attributes']['type'];
  if (($type === 'date' || $type === 'text') && !empty($element['#date_date_format'])) {

    // Element jumps back to gregorian on form submit with a form with errors.
    if (!empty($element['#value'])) {
      $parents = $element['#parents'];
      array_pop($parents);
      $fs = $form_state
        ->getValue($parents);
      $element['#value'] = $fs['date'];
    }

    // Attach js date picker.
    $lib = [];
    foreach ($element['#attached']['library'] as $item) {
      if ($item !== 'core/drupal.date' && $item !== 'datex/picker') {
        $lib[] = $item;
      }
    }
    $lib[] = 'datex/picker';
    $element['#attached']['library'] = $lib;

    // Js date picker works on text fields, date field interferes with us.
    $element['#attributes']['type'] = 'text';
  }
  return $element;
}