TimestampDatetimeWidget.php in Drupal 10
File
core/lib/Drupal/Core/Datetime/Plugin/Field/FieldWidget/TimestampDatetimeWidget.php
View source
<?php
namespace Drupal\Core\Datetime\Plugin\Field\FieldWidget;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Datetime\Element\Datetime;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class TimestampDatetimeWidget extends WidgetBase {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$date_format = DateFormat::load('html_date')
->getPattern();
$time_format = DateFormat::load('html_time')
->getPattern();
$default_value = isset($items[$delta]->value) ? DrupalDateTime::createFromTimestamp($items[$delta]->value) : '';
$element['value'] = $element + [
'#type' => 'datetime',
'#default_value' => $default_value,
'#date_year_range' => '1902:2037',
];
$element['value']['#description'] = $element['#description'] !== '' ? $element['#description'] : $this
->t('Format: %format. Leave blank to use the time of form submission.', [
'%format' => Datetime::formatExample($date_format . ' ' . $time_format),
]);
return $element;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as &$item) {
if (isset($item['value']) && $item['value'] instanceof DrupalDateTime) {
$date = $item['value'];
}
elseif (isset($item['value']['object']) && $item['value']['object'] instanceof DrupalDateTime) {
$date = $item['value']['object'];
}
else {
$date = new DrupalDateTime();
}
$item['value'] = $date
->getTimestamp();
}
return $values;
}
}