You are here

public static function PersianDate::processDate in Persian Date for Drupal 8 8.4

Same name and namespace in other branches
  1. 8 src/Element/PersianDate.php \Drupal\persian_date\Element\PersianDate::processDate()

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/PersianDate.php, line 30

Class

PersianDate
Class PersianDate @package Drupal\persian_date\Element

Namespace

Drupal\persian_date\Element

Code

public static function processDate(&$element, FormStateInterface $form_state, &$complete_form) {

  // Attach JS support for the date field, if we can determine which date
  // format should be used.
  if ($element['#attributes']['type'] == 'date' && !empty($element['#date_date_format'])) {
    $element['#attributes']['type'] = 'text';

    // filter element goes to view
    if ($element['#date_date_format'] === 'Y-m-d' && $element['#value']) {
      list($year, , ) = explode('-', $element['#value']);
      if (is_georgian_year($year)) {
        $element['#value'] = jDate::forge($element['#value'])
          ->format('Y-m-d');
      }
    }
    $element['#attached']['library'][] = 'persian_date/core';
    $element['#attributes']['data-drupal-date-format'] = [
      $element['#date_date_format'],
    ];
  }
  return $element;
}