public function DateTimeDayWidgetBase::validateStartEnd in Date time day 8
Validation callback to ensure that the start_time <= the end_time.
Parameters
array $element: An associative array containing the properties and children of the generic form element.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
array $complete_form: The complete form structure.
File
- src/
Plugin/ Field/ FieldWidget/ DateTimeDayWidgetBase.php, line 150
Class
- DateTimeDayWidgetBase
- Base class for the 'datetimeday_*' widgets.
Namespace
Drupal\date_time_day\Plugin\Field\FieldWidgetCode
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'],
]));
}
}
}
}
}