public function DateTimeDatelistWidget::formElement

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

Overrides WidgetInterface::formElement

File

drupal/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php, line 60
Contains \Drupal\datetime\Plugin\field\widget\DateTimeDatelistWidget.

Class

DateTimeDatelistWidget
Plugin implementation of the 'datetime_datelist' widget.

Namespace

Drupal\datetime\Plugin\field\widget

Code

public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
  $field = $this->field;
  $instance = $this->instance;
  $date_order = $this
    ->getSetting('date_order');
  $time_type = $this
    ->getSetting('time_type');
  $increment = $this
    ->getSetting('increment');

  // We're nesting some sub-elements inside the parent, so we
  // need a wrapper. We also need to add another #title attribute
  // at the top level for ease in identifying this item in error
  // messages. We don't want to display this title because the
  // actual title display is handled at a higher level by the Field
  // module.
  $element['#theme_wrappers'][] = 'datetime_wrapper';
  $element['#attributes']['class'][] = 'container-inline';
  $element['#element_validate'][] = 'datetime_datelist_widget_validate';

  // Identify the type of date and time elements to use.
  switch ($field['settings']['datetime_type']) {
    case 'date':
      $storage_format = DATETIME_DATE_STORAGE_FORMAT;
      $type_type = 'none';
      break;
    default:
      $storage_format = DATETIME_DATETIME_STORAGE_FORMAT;
      break;
  }

  // Set up the date part order array.
  switch ($date_order) {
    case 'YMD':
      $date_part_order = array(
        'year',
        'month',
        'day',
      );
      break;
    case 'MDY':
      $date_part_order = array(
        'month',
        'day',
        'year',
      );
      break;
    case 'DMY':
      $date_part_order = array(
        'day',
        'month',
        'year',
      );
      break;
  }
  switch ($time_type) {
    case '24':
      $date_part_order = array_merge($date_part_order, array(
        'hour',
        'minute',
      ));
      break;
    case '12':
      $date_part_order = array_merge($date_part_order, array(
        'hour',
        'minute',
        'ampm',
      ));
      break;
    case 'none':
      break;
  }
  $element['value'] = array(
    '#type' => 'datelist',
    '#default_value' => NULL,
    '#date_increment' => $increment,
    '#date_part_order' => $date_part_order,
    '#date_timezone' => drupal_get_user_timezone(),
    '#required' => $element['#required'],
  );

  // Set the storage and widget options so the validation can use them. The
  // validator will not have access to field or instance settings.
  $element['value']['#date_storage_format'] = $storage_format;
  if (!empty($items[$delta]['date'])) {
    $date = $items[$delta]['date'];

    // The date was created and verified during field_load(), so it is safe to
    // use without further inspection.
    $date
      ->setTimezone(new \DateTimeZone($element['value']['#date_timezone']));
    if ($field['settings']['datetime_type'] == 'date') {

      // A date without time will pick up the current time, use the default
      // time.
      datetime_date_default_time($date);
    }
    $element['value']['#default_value'] = $date;
  }
  return $element;
}