DateTimeDayWidgetBase.php in Date time day 8
File
src/Plugin/Field/FieldWidget/DateTimeDayWidgetBase.php
View source
<?php
namespace Drupal\date_time_day\Plugin\Field\FieldWidget;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\datetime\Plugin\Field\FieldWidget\DateTimeWidgetBase;
use Drupal\date_time_day\Plugin\Field\FieldType\DateTimeDayItem;
class DateTimeDayWidgetBase extends DateTimeWidgetBase {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['#element_validate'][] = [
$this,
'validateStartEnd',
];
$element['#theme_wrappers'][] = 'fieldset';
$element['value']['#title'] = $this
->t('Date');
$element['start_time_value'] = [
'#title' => $this
->t('Start time'),
] + $element['value'];
$element['end_time_value'] = [
'#title' => $this
->t('End time'),
] + $element['value'];
if ($items[$delta]->date) {
$value = $items[$delta]->date;
$element['value']['#default_value'] = $this
->createDateTimeDayDefaultValue($value, $element['value']['#date_timezone']);
}
if ($items[$delta]->start_time) {
$start_time = $items[$delta]->start_time;
$element['start_time_value']['#default_value'] = $this
->createDateTimeDayDefaultValue($start_time, $element['start_time_value']['#date_timezone']);
}
if ($items[$delta]->end_time) {
$end_time = $items[$delta]->end_time;
$element['end_time_value']['#default_value'] = $this
->createDateTimeDayDefaultValue($end_time, $element['end_time_value']['#date_timezone']);
}
return $element;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as &$item) {
if (!empty($item['value']) && $item['value'] instanceof DrupalDateTime) {
$value_date = $item['value'];
$value_format = DateTimeItemInterface::DATE_STORAGE_FORMAT;
$value_date
->setTimezone(new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE));
$item['value'] = $value_date
->format($value_format);
}
if (!empty($item['start_time_value']) && $item['start_time_value'] instanceof DrupalDateTime) {
$start_time_date = $item['start_time_value'];
$start_time_format = '';
switch ($this
->getFieldSetting('time_type')) {
case DateTimeDayItem::DATEDAY_TIME_DEFAULT_TYPE_FORMAT:
$start_time_format = DateTimeDayItem::DATE_TIME_DAY_H_I_FORMAT_STORAGE_FORMAT;
break;
case DateTimeDayItem::DATEDAY_TIME_TYPE_SECONDS_FORMAT:
$start_time_format = DateTimeDayItem::DATE_TIME_DAY_H_I_S_FORMAT_STORAGE_FORMAT;
break;
default:
$start_time_format = DateTimeDayItem::DATE_TIME_DAY_H_I_S_FORMAT_STORAGE_FORMAT;
break;
}
$start_time_date
->setTimezone(new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE));
$item['start_time_value'] = $start_time_date
->format($start_time_format);
}
if (!empty($item['end_time_value']) && $item['end_time_value'] instanceof DrupalDateTime) {
$end_time_date = $item['end_time_value'];
$end_time_format = '';
switch ($this
->getFieldSetting('time_type')) {
case DateTimeDayItem::DATEDAY_TIME_DEFAULT_TYPE_FORMAT:
$end_time_format = DateTimeDayItem::DATE_TIME_DAY_H_I_FORMAT_STORAGE_FORMAT;
break;
case DateTimeDayItem::DATEDAY_TIME_TYPE_SECONDS_FORMAT:
$end_time_format = DateTimeDayItem::DATE_TIME_DAY_H_I_S_FORMAT_STORAGE_FORMAT;
break;
default:
$end_time_format = DateTimeDayItem::DATE_TIME_DAY_H_I_S_FORMAT_STORAGE_FORMAT;
break;
}
$end_time_date
->setTimezone(new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE));
$item['end_time_value'] = $end_time_date
->format($end_time_format);
}
}
return $values;
}
protected function createDateTimeDayDefaultValue(DrupalDateTime $date, $timezone) {
$date
->setTimezone(new \DateTimeZone($timezone));
return $date;
}
public function validateStartEnd(array &$element, FormStateInterface $form_state, array &$complete_form) {
$type = $this
->getFieldSetting('time_type');
$start_date = isset($element['start_time_value']['#value']['time']) ? $element['start_time_value']['#value']['time'] : $element['start_time_value']['#value'];
$storage_format = $type === DateTimeDayItem::DATEDAY_TIME_DEFAULT_TYPE_FORMAT ? DateTimeDayItem::DATE_TIME_DAY_H_I_FORMAT_STORAGE_FORMAT : DateTimeDayItem::DATE_TIME_DAY_H_I_S_FORMAT_STORAGE_FORMAT;
if ($type === DateTimeDayItem::DATEDAY_TIME_TYPE_SECONDS_FORMAT && strlen($start_date) === 5) {
$start_date = "{$start_date}:00";
}
$end_date = isset($element['end_time_value']['#value']['time']) ? $element['end_time_value']['#value']['time'] : $element['end_time_value']['#value'];
if ($type === DateTimeDayItem::DATEDAY_TIME_TYPE_SECONDS_FORMAT && strlen($end_date) === 5) {
$end_date = "{$end_date}:00";
}
if (!empty($start_date) && !empty($end_date)) {
$start_date = DrupalDateTime::createFromFormat($storage_format, $start_date);
$end_date = DrupalDateTime::createFromFormat($storage_format, $end_date);
if ($start_date instanceof DrupalDateTime && $end_date instanceof DrupalDateTime) {
if ($start_date
->getTimestamp() !== $end_date
->getTimestamp()) {
$interval = $start_date
->diff($end_date);
if ($interval->invert === 1) {
$form_state
->setError($element, $this
->t('The @title end date cannot be before the start date', [
'@title' => $element['#title'],
]));
}
}
}
}
}
}