Commit 6bad26f4 authored by alexpott's avatar alexpott

Issue #2327363 by yched, andypost: Move datetime_datetime_widget_validate() to...

Issue #2327363 by yched, andypost: Move datetime_datetime_widget_validate() to DateTimeDefaultWidget::massageFormValues().
parent a8b92fe4
......@@ -45,82 +45,6 @@ function datetime_help($route_name, RouteMatchInterface $route_match) {
}
}
/**
* Validation callback for the datetime widget element.
*
* The date has already been validated by the datetime form type validator and
* transformed to an date object. We just need to convert the date back to a the
* storage timezone and format.
*
* @param array $element
* The form element whose value is being validated.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
function datetime_datetime_widget_validate(&$element, FormStateInterface $form_state) {
if (!$form_state->getErrors()) {
$input_exists = FALSE;
$input = NestedArray::getValue($form_state->getValues(), $element['#parents'], $input_exists);
if ($input_exists) {
// The date should have been returned to a date object at this point by
// datetime_validate(), which runs before this.
if (!empty($input['value'])) {
$date = $input['value'];
if ($date instanceOf DrupalDateTime && !$date->hasErrors()) {
// If this is a date-only field, set it to the default time so the
// timezone conversion can be reversed.
if ($element['value']['#date_time_element'] == 'none') {
datetime_date_default_time($date);
}
// Adjust the date for storage.
$date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
$value = $date->format($element['value']['#date_storage_format']);
form_set_value($element['value'], $value, $form_state);
}
}
}
}
}
/**
* Validation callback for the datelist widget element.
*
* The date has already been validated by the datetime form type validator and
* transformed to an date object. We just need to convert the date back to a the
* storage timezone and format.
*
* @param array $element
* The form element whose value is being validated.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
function datetime_datelist_widget_validate(&$element, FormStateInterface $form_state) {
if (!$form_state->getErrors()) {
$input_exists = FALSE;
$input = NestedArray::getValue($form_state->getValues(), $element['#parents'], $input_exists);
if ($input_exists) {
// The date should have been returned to a date object at this point by
// datetime_validate(), which runs before this.
if (!empty($input['value'])) {
$date = $input['value'];
if ($date instanceOf DrupalDateTime && !$date->hasErrors()) {
// If this is a date-only field, set it to the default time so the
// timezone conversion can be reversed.
if (!in_array('hour', $element['value']['#date_part_order'])) {
datetime_date_default_time($date);
}
// Adjust the date for storage.
$date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
$value = $date->format($element['value']['#date_storage_format']);
form_set_value($element['value'], $value, $form_state);
}
}
}
}
}
/**
* Sets a consistent time on a date without time.
*
......
......@@ -7,9 +7,7 @@
namespace Drupal\datetime\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
/**
* Plugin implementation of the 'datetime_datelist' widget.
......@@ -22,7 +20,7 @@
* }
* )
*/
class DateTimeDatelistWidget extends WidgetBase {
class DateTimeDatelistWidget extends DateTimeWidgetBase {
/**
* {@inheritdoc}
......@@ -39,32 +37,12 @@ public static function defaultSettings() {
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$date_order = $this->getSetting('date_order');
$time_type = $this->getSetting('time_type');
$increment = $this->getSetting('increment');
// We're nesting some sub-elements inside the parent, so we
// need a wrapper. We also need to add another #title attribute
// at the top level for ease in identifying this item in error
// messages. We don't want to display this title because the
// actual title display is handled at a higher level by the Field
// module.
$element['#theme_wrappers'][] = 'datetime_wrapper';
$element['#attributes']['class'][] = 'container-inline';
$element['#element_validate'][] = 'datetime_datelist_widget_validate';
// Identify the type of date and time elements to use.
switch ($this->getFieldSetting('datetime_type')) {
case DateTimeItem::DATETIME_TYPE_DATE:
$storage_format = DATETIME_DATE_STORAGE_FORMAT;
break;
default:
$storage_format = DATETIME_DATETIME_STORAGE_FORMAT;
break;
}
// Set up the date part order array.
switch ($date_order) {
case 'YMD':
......@@ -94,29 +72,10 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$element['value'] = array(
'#type' => 'datelist',
'#default_value' => NULL,
'#date_increment' => $increment,
'#date_part_order'=> $date_part_order,
'#date_timezone' => drupal_get_user_timezone(),
'#required' => $element['#required'],
);
) + $element['value'];
// Set the storage and widget options so the validation can use them. The
// validator will not have access to the field definition.
$element['value']['#date_storage_format'] = $storage_format;
if ($items[$delta]->date) {
$date = $items[$delta]->date;
// The date was created and verified during field_load(), so it is safe to
// use without further inspection.
$date->setTimezone( new \DateTimeZone($element['value']['#date_timezone']));
if ($this->getFieldSetting('datetime_type') == 'date') {
// A date without time will pick up the current time, use the default
// time.
datetime_date_default_time($date);
}
$element['value']['#default_value'] = $date;
}
return $element;
}
......
......@@ -8,7 +8,6 @@
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
......@@ -26,7 +25,7 @@
* }
* )
*/
class DateTimeDefaultWidget extends WidgetBase implements ContainerFactoryPluginInterface {
class DateTimeDefaultWidget extends DateTimeWidgetBase implements ContainerFactoryPluginInterface {
/**
* The date format storage.
......@@ -62,15 +61,7 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
// We are nesting some sub-elements inside the parent, so we need a wrapper.
// We also need to add another #title attribute at the top level for ease in
// identifying this item in error messages. We do not want to display this
// title because the actual title display is handled at a higher level by
// the Field module.
$element['#theme_wrappers'][] = 'datetime_wrapper';
$element['#attributes']['class'][] = 'container-inline';
$element['#element_validate'][] = 'datetime_datetime_widget_validate';
$element = parent::formElement($items, $delta, $element, $form, $form_state);
// Identify the type of date and time elements to use.
switch ($this->getFieldSetting('datetime_type')) {
......@@ -79,8 +70,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$time_type = 'none';
$date_format = $this->dateStorage->load('html_date')->getPattern();
$time_format = '';
$element_format = $date_format;
$storage_format = DATETIME_DATE_STORAGE_FORMAT;
break;
default:
......@@ -88,42 +77,18 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$time_type = 'time';
$date_format = $this->dateStorage->load('html_date')->getPattern();
$time_format = $this->dateStorage->load('html_time')->getPattern();
$element_format = $date_format . ' ' . $time_format;
$storage_format = DATETIME_DATETIME_STORAGE_FORMAT;
break;
}
$element['value'] = array(
'#type' => 'datetime',
'#default_value' => NULL,
'#date_increment' => 1,
$element['value'] += array(
'#date_date_format'=> $date_format,
'#date_date_element' => $date_type,
'#date_date_callbacks' => array(),
'#date_time_format' => $time_format,
'#date_time_element' => $time_type,
'#date_time_callbacks' => array(),
'#date_timezone' => drupal_get_user_timezone(),
'#required' => $element['#required'],
);
// Set the storage and widget options so the validation can use them. The
// validator will not have access to the field definition.
$element['value']['#date_element_format'] = $element_format;
$element['value']['#date_storage_format'] = $storage_format;
if ($items[$delta]->date) {
$date = $items[$delta]->date;
// The date was created and verified during field_load(), so it is safe to
// use without further inspection.
$date->setTimezone(new \DateTimeZone($element['value']['#date_timezone']));
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
// A date without time will pick up the current time, use the default
// time.
datetime_date_default_time($date);
}
$element['value']['#default_value'] = $date;
}
return $element;
}
......
<?php
/**
* @file
* Contains \Drupal\datetime\Plugin\Field\FieldWidget\DateTimeWidgetBase.
*/
namespace Drupal\datetime\Plugin\Field\FieldWidget;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
/**
* Base class for the 'datetime_*' widgets.
*/
class DateTimeWidgetBase extends WidgetBase {
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
// We are nesting some sub-elements inside the parent, so we need a wrapper.
// We also need to add another #title attribute at the top level for ease in
// identifying this item in error messages. We do not want to display this
// title because the actual title display is handled at a higher level by
// the Field module.
$element['#theme_wrappers'][] = 'datetime_wrapper';
$element['#attributes']['class'][] = 'container-inline';
$element['value'] = array(
'#type' => 'datetime',
'#default_value' => NULL,
'#date_increment' => 1,
'#date_timezone' => drupal_get_user_timezone(),
'#required' => $element['#required'],
);
if ($items[$delta]->date) {
$date = $items[$delta]->date;
// The date was created and verified during field_load(), so it is safe to
// use without further inspection.
$date->setTimezone(new \DateTimeZone($element['value']['#date_timezone']));
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
// A date without time will pick up the current time, use the default
// time.
datetime_date_default_time($date);
}
$element['value']['#default_value'] = $date;
}
return $element;
}
/**
* {@inheritdoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
// The widget form element type has transformed the value to a
// DrupalDateTime object at this point. We need to convert it back to the
// storage timezone and format.
foreach ($values as &$item) {
if (!empty($item['value']) && $item['value'] instanceof DrupalDateTime) {
$date = $item['value'];
switch ($this->getFieldSetting('datetime_type')) {
case DateTimeItem::DATETIME_TYPE_DATE:
// If this is a date-only field, set it to the default time so the
// timezone conversion can be reversed.
datetime_date_default_time($date);
$format = DATETIME_DATE_STORAGE_FORMAT;
break;
default:
$format = DATETIME_DATETIME_STORAGE_FORMAT;
break;
}
// Adjust the date for storage.
$date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
$item['value'] = $date->format($format);
}
}
return $values;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment