You are here

function DateFieldDatepickerWidget::formElement in Date 8

Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().

The widget builds out a complex date element in the following way:

  • A field is pulled out of the database which is comprised of one or more collections of start/end dates.
  • The dates in this field are all converted from the UTC values stored in the database back to the local time. This is done in #process to avoid making this change to dates that are not being processed, like those hidden with #access.
  • If values are empty, the field settings rules are used to determine if the default_values should be empty, now, the same, or use strtotime.
  • Each start/end combination is created using the date_combo element type defined by the date module. If the timezone is date-specific, a timezone selector is added to the first combo element.
  • The date combo element creates two individual date elements, one each for the start and end field, using the appropriate individual Date API date elements, like selects, textfields, or popups.
  • In the individual element validation, the data supplied by the user is used to update the individual date values.
  • In the combo date validation, the timezone is updated, if necessary, then the user input date values are used with that timezone to create date objects, which are used update combo date timezone and offset values.
  • In the field's submission processing, the new date values, which are in the local timezone, are converted back to their UTC values and stored.

Overrides DateFieldWidgetBase::formElement

File

date_field/lib/Drupal/date_field/Plugin/field/widget/DateFieldDatepickerWidget.php, line 117
Definition of Drupal\datefield\Plugin\field\widget\DateFieldDatepickerWidget.

Class

DateFieldDatepickerWidget
Plugin implementation of the 'date' widget.

Namespace

Drupal\date_field\Plugin\field\widget

Code

function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
  $element = parent::formElement($items, $delta, $element, $langcode, $form, $form_state);
  $element += array(
    '#type' => 'datetime',
    '#date_increment' => $this
      ->getSetting('increment'),
    '#date_text_parts' => $this
      ->getSetting('text_parts'),
    '#date_year_range' => $this
      ->getSetting('year_range'),
    '#date_date_format' => $this
      ->getSetting('date_format'),
    '#date_date_element' => $this
      ->getSetting('date_element'),
    '#date_date_callbacks' => $this
      ->getSetting('date_callbacks'),
    '#date_time_format' => $this
      ->getSetting('time_format'),
    '#date_time_element' => $this
      ->getSetting('time_element'),
    '#date_time_callbacks' => $this
      ->getSetting('time_callbacks'),
  );
  return $element;
}