You are here

public static function CalendarSystemsDate::processDate in Calendar Systems 8.3

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/CalendarSystemsDate.php, line 22

Class

CalendarSystemsDate
Plugin annotation @FormElement("date");

Namespace

Drupal\calendar_systems\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'] = _calendar_systems_tmp_fix_string($fs['date']);
    }

    // Attach js date picker.
    $lib = [];
    foreach ($element['#attached']['library'] as $item) {
      if ($item !== 'core/drupal.date' && $item !== 'calendar_systems/picker') {
        $lib[] = $item;
      }
    }
    $lib[] = 'calendar_systems/picker';
    $element['#attached']['library'] = $lib;
    $cs = _calendar_systems_factory();
    if ($cs) {
      $element['#attached']['drupalSettings']['calendar_systems'] = [
        'calendar' => $cs
          ->getCalendarName(),
        'lang' => $cs
          ->getLangcode(),
      ];
    }

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