SingleDateTimeWidget.php in Single DateTimePicker 8
File
src/Plugin/Field/FieldWidget/SingleDateTimeWidget.php
View source
<?php
namespace Drupal\single_datetime\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
class SingleDateTimeWidget extends SingleDateTimeBase {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$date_type = $this
->getFieldSetting('datetime_type');
$element['value'] = [
'#title' => $element['#title'],
'#type' => 'single_date_time',
'#date_timezone' => date_default_timezone_get(),
'#default_value' => NULL,
'#date_type' => $date_type,
'#required' => $element['#required'],
'#description' => $element['#description'],
];
if ($date_type === DateTimeItem::DATETIME_TYPE_DATE) {
$element['value']['#date_timezone'] = DateTimeItemInterface::STORAGE_TIMEZONE;
$format = DateTimeItemInterface::DATE_STORAGE_FORMAT;
}
else {
$format = $this
->getSetting('hour_format') === '12h' ? 'Y-m-d h:i:s A' : 'Y-m-d H:i:s';
}
$element['value'] = array_merge($element['value'], $this
->getCommonElementSettings());
if ($items[$delta]->date) {
$date = $items[$delta]->date;
$element['value']['#default_value'] = $this
->formatDefaultValue($date, $element['value']['#date_timezone'], $format);
}
return $element;
}
}