View source
<?php
namespace Drupal\smart_date\Plugin\Field\FieldWidget;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\datetime\Plugin\Field\FieldWidget\DateTimeWidgetBase;
use Drupal\smart_date\Plugin\Field\FieldType\SmartDateListItemBase;
class SmartDateWidgetBase 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['#theme_wrappers'][] = 'fieldset';
$element['#element_validate'][] = [
$this,
'validateStartEnd',
];
$element['value']['#title'] = $this
->t('Start');
$element['value']['#date_year_range'] = '1902:2037';
$default_value = isset($items[$delta]->value) ? DrupalDateTime::createFromTimestamp($items[$delta]->value) : '';
$default_end_value = isset($items[$delta]->end_value) ? DrupalDateTime::createFromTimestamp($items[$delta]->end_value) : '';
$element['value']['#default_value'] = $default_value;
$element['end_value'] = [
'#title' => $this
->t('End'),
'#default_value' => $default_end_value,
] + $element['value'];
$element['value']['#attributes']['class'] = [
'time-start',
];
$element['end_value']['#attributes']['class'] = [
'time-end',
];
$defaults = $this->fieldDefinition
->getDefaultValueLiteral()[0];
$default_duration = isset($items[$delta]->duration) ? $items[$delta]->duration : $defaults['default_duration'];
$increments = SmartDateListItemBase::parseValues($defaults['default_duration_increments']);
foreach ($increments as $key => $label) {
if (strcmp($key, $label) !== 0) {
continue;
}
if (is_numeric($key)) {
$num = (int) $key;
$increments[$key] = t('@count minutes', [
'@count' => $num,
]);
}
elseif ($key == 'custom') {
$increments[$key] = t('Custom');
}
else {
$increments[$key] = t('@key (unrecognized format)', [
'@key' => $key,
]);
}
}
if (!array_key_exists($default_duration, $increments)) {
if (array_key_exists('custom', $increments)) {
$default_duration = 'custom';
}
else {
$default_duration = '';
}
}
$element['duration'] = [
'#title' => $this
->t('Duration'),
'#type' => 'select',
'#options' => $increments,
'#default_value' => $default_duration,
'#attributes' => [
'data-default' => $defaults['default_duration'],
],
];
$form['#attached']['library'][] = 'smart_date/smart_date';
if ($items[$delta]->start_time) {
$start_time = $items[$delta]->start_time;
$element['value']['#default_value'] = $this
->createDefaultValue($start_time, $element['value']['#date_timezone']);
}
if ($items[$delta]->end_time) {
$end_time = $items[$delta]->end_time;
$element['end_value']['#default_value'] = $this
->createDefaultValue($end_time, $element['end_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) {
$start_time = $item['value'];
$item['value'] = $start_time
->getTimestamp();
}
if (!empty($item['end_value']) && $item['end_value'] instanceof DrupalDateTime) {
$end_time = $item['end_value'];
$item['end_value'] = $end_time
->getTimestamp();
}
if ($item['duration'] == 'custom') {
if (isset($start_time) && isset($end_time) && $start_time instanceof DrupalDateTime && $end_time instanceof DrupalDateTime) {
$item['duration'] = (int) ($item['end_value'] - $item['value']) / 60;
}
}
}
return $values;
}
public function validateStartEnd(array &$element, FormStateInterface $form_state, array &$complete_form) {
$start_time = $element['value']['#value']['object'];
$end_time = $element['end_value']['#value']['object'];
if ($start_time instanceof DrupalDateTime && $end_time instanceof DrupalDateTime) {
if ($start_time
->getTimestamp() !== $end_time
->getTimestamp()) {
$interval = $start_time
->diff($end_time);
if ($interval->invert === 1) {
$form_state
->setError($element, $this
->t('The @title end date cannot be before the start date', [
'@title' => $element['#title'],
]));
}
}
}
}
}