updated core from 8.4 to 8.5 : bug with login_destination
This commit is contained in:
@@ -7,6 +7,7 @@ use Drupal\Core\Datetime\DrupalDateTime;
|
||||
use Drupal\Core\TypedData\DataDefinitionInterface;
|
||||
use Drupal\Core\TypedData\TypedDataInterface;
|
||||
use Drupal\Core\TypedData\TypedData;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
|
||||
/**
|
||||
* A computed property for dates of date time field items.
|
||||
@@ -36,7 +37,7 @@ class DateTimeComputed extends TypedData {
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getValue($langcode = NULL) {
|
||||
public function getValue() {
|
||||
if ($this->date !== NULL) {
|
||||
return $this->date;
|
||||
}
|
||||
@@ -46,9 +47,9 @@ class DateTimeComputed extends TypedData {
|
||||
$value = $item->{($this->definition->getSetting('date source'))};
|
||||
|
||||
$datetime_type = $item->getFieldDefinition()->getSetting('datetime_type');
|
||||
$storage_format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$storage_format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
|
||||
try {
|
||||
$date = DrupalDateTime::createFromFormat($storage_format, $value, DATETIME_STORAGE_TIMEZONE);
|
||||
$date = DrupalDateTime::createFromFormat($storage_format, $value, DateTimeItemInterface::STORAGE_TIMEZONE);
|
||||
if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
|
||||
$this->date = $date;
|
||||
// If the format did not include an explicit time portion, then the
|
||||
@@ -56,12 +57,10 @@ class DateTimeComputed extends TypedData {
|
||||
// set the time to 12:00:00 UTC for date-only fields. This is used so
|
||||
// that the local date portion is the same, across nearly all time
|
||||
// zones.
|
||||
// @see datetime_date_default_time()
|
||||
// @see http://php.net/manual/en/datetime.createfromformat.php
|
||||
// @todo Update comment and/or code per the chosen solution in
|
||||
// https://www.drupal.org/node/2830094
|
||||
// @see \Drupal\Component\Datetime\DateTimePlus::setDefaultDateTime()
|
||||
// @see http://php.net/manual/datetime.createfromformat.php
|
||||
if ($datetime_type === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
$this->date->setTime(12, 0, 0);
|
||||
$this->date->setDefaultDateTime();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Drupal\datetime\Plugin\Field\FieldFormatter;
|
||||
use Drupal\Core\Datetime\DrupalDateTime;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'Custom' formatter for 'datetime' fields.
|
||||
@@ -24,7 +25,7 @@ class DateTimeCustomFormatter extends DateTimeFormatterBase {
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return [
|
||||
'date_format' => DATETIME_DATETIME_STORAGE_FORMAT,
|
||||
'date_format' => DateTimeItemInterface::DATETIME_STORAGE_FORMAT,
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
@@ -167,7 +168,7 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF
|
||||
protected function setTimeZone(DrupalDateTime $date) {
|
||||
if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// A date without time has no timezone conversion.
|
||||
$timezone = DATETIME_STORAGE_TIMEZONE;
|
||||
$timezone = DateTimeItemInterface::STORAGE_TIMEZONE;
|
||||
}
|
||||
else {
|
||||
$timezone = drupal_get_user_timezone();
|
||||
@@ -201,10 +202,6 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF
|
||||
* A render array.
|
||||
*/
|
||||
protected function buildDate(DrupalDateTime $date) {
|
||||
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// A date without time will pick up the current time, use the default.
|
||||
datetime_date_default_time($date);
|
||||
}
|
||||
$this->setTimeZone($date);
|
||||
|
||||
$build = [
|
||||
@@ -229,11 +226,6 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF
|
||||
* A render array.
|
||||
*/
|
||||
protected function buildDateWithIsoAttribute(DrupalDateTime $date) {
|
||||
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// A date without time will pick up the current time, use the default.
|
||||
datetime_date_default_time($date);
|
||||
}
|
||||
|
||||
// Create the ISO date in Universal Time.
|
||||
$iso_date = $date->format("Y-m-d\TH:i:s") . 'Z';
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Drupal\datetime\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'Plain' formatter for 'datetime' fields.
|
||||
@@ -40,7 +41,7 @@ class DateTimePlainFormatter extends DateTimeFormatterBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function formatDate($date) {
|
||||
$format = $this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$format = $this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
|
||||
$timezone = $this->getSetting('timezone_override');
|
||||
return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $timezone != '' ? $timezone : NULL);
|
||||
}
|
||||
|
||||
@@ -110,10 +110,6 @@ class DateTimeTimeAgoFormatter extends FormatterBase implements ContainerFactory
|
||||
$date = $item->date;
|
||||
$output = [];
|
||||
if (!empty($item->date)) {
|
||||
if ($this->getFieldSetting('datetime_type') == 'date') {
|
||||
// A date without time will pick up the current time, use the default.
|
||||
datetime_date_default_time($date);
|
||||
}
|
||||
$output = $this->formatDate($date);
|
||||
}
|
||||
$elements[$delta] = $output;
|
||||
|
||||
@@ -96,13 +96,13 @@ class DateTimeFieldItemList extends FieldItemList {
|
||||
// A default date only value should be in the format used for date
|
||||
// storage but in the user's local timezone.
|
||||
$date = new DrupalDateTime($default_value[0]['default_date'], drupal_get_user_timezone());
|
||||
$format = DATETIME_DATE_STORAGE_FORMAT;
|
||||
$format = DateTimeItemInterface::DATE_STORAGE_FORMAT;
|
||||
}
|
||||
else {
|
||||
// A default date+time value should be in the format and timezone used
|
||||
// for date storage.
|
||||
$date = new DrupalDateTime($default_value[0]['default_date'], DATETIME_STORAGE_TIMEZONE);
|
||||
$format = DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$date = new DrupalDateTime($default_value[0]['default_date'], DateTimeItemInterface::STORAGE_TIMEZONE);
|
||||
$format = DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
|
||||
}
|
||||
$value = $date->format($format);
|
||||
// We only provide a default value for the first item, as do all fields.
|
||||
|
||||
@@ -21,7 +21,7 @@ use Drupal\Core\Field\FieldItemBase;
|
||||
* constraints = {"DateTimeFormat" = {}}
|
||||
* )
|
||||
*/
|
||||
class DateTimeItem extends FieldItemBase {
|
||||
class DateTimeItem extends FieldItemBase implements DateTimeItemInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
@@ -109,10 +109,10 @@ class DateTimeItem extends FieldItemBase {
|
||||
// type.
|
||||
$timestamp = REQUEST_TIME - mt_rand(0, 86400 * 365);
|
||||
if ($type == DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
$values['value'] = gmdate(DATETIME_DATE_STORAGE_FORMAT, $timestamp);
|
||||
$values['value'] = gmdate(static::DATE_STORAGE_FORMAT, $timestamp);
|
||||
}
|
||||
else {
|
||||
$values['value'] = gmdate(DATETIME_DATETIME_STORAGE_FORMAT, $timestamp);
|
||||
$values['value'] = gmdate(static::DATETIME_STORAGE_FORMAT, $timestamp);
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\datetime\Plugin\Field\FieldType;
|
||||
|
||||
/**
|
||||
* Interface definition for Datetime items.
|
||||
*/
|
||||
interface DateTimeItemInterface {
|
||||
|
||||
/**
|
||||
* Defines the timezone that dates should be stored in.
|
||||
*/
|
||||
const STORAGE_TIMEZONE = 'UTC';
|
||||
|
||||
/**
|
||||
* Defines the format that date and time should be stored in.
|
||||
*/
|
||||
const DATETIME_STORAGE_FORMAT = 'Y-m-d\TH:i:s';
|
||||
|
||||
/**
|
||||
* Defines the format that dates should be stored in.
|
||||
*/
|
||||
const DATE_STORAGE_FORMAT = 'Y-m-d';
|
||||
|
||||
}
|
||||
@@ -7,6 +7,7 @@ use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\WidgetBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
|
||||
/**
|
||||
* Base class for the 'datetime_*' widgets.
|
||||
@@ -28,20 +29,15 @@ class DateTimeWidgetBase extends WidgetBase {
|
||||
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// A date-only field should have no timezone conversion performed, so
|
||||
// use the same timezone as for storage.
|
||||
$element['value']['#date_timezone'] = DATETIME_STORAGE_TIMEZONE;
|
||||
$element['value']['#date_timezone'] = DateTimeItemInterface::STORAGE_TIMEZONE;
|
||||
}
|
||||
|
||||
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.
|
||||
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);
|
||||
}
|
||||
$date->setTimezone(new \DateTimeZone($element['value']['#date_timezone']));
|
||||
$element['value']['#default_value'] = $date;
|
||||
$element['value']['#default_value'] = $this->createDefaultValue($date, $element['value']['#date_timezone']);
|
||||
}
|
||||
|
||||
return $element;
|
||||
@@ -59,22 +55,43 @@ class DateTimeWidgetBase extends WidgetBase {
|
||||
$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;
|
||||
$format = DateTimeItemInterface::DATE_STORAGE_FORMAT;
|
||||
break;
|
||||
|
||||
default:
|
||||
$format = DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$format = DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
|
||||
break;
|
||||
}
|
||||
// Adjust the date for storage.
|
||||
$date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
|
||||
$date->setTimezone(new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE));
|
||||
$item['value'] = $date->format($format);
|
||||
}
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a date object for use as a default value.
|
||||
*
|
||||
* This will take a default value, apply the proper timezone for display in
|
||||
* a widget, and set the default time for date-only fields.
|
||||
*
|
||||
* @param \Drupal\Core\Datetime\DrupalDateTime $date
|
||||
* The UTC default date.
|
||||
* @param string $timezone
|
||||
* The timezone to apply.
|
||||
*
|
||||
* @return \Drupal\Core\Datetime\DrupalDateTime
|
||||
* A date object for use as a default value in a field widget.
|
||||
*/
|
||||
protected function createDefaultValue($date, $timezone) {
|
||||
// The date was created and verified during field_load(), so it is safe to
|
||||
// use without further inspection.
|
||||
if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
$date->setDefaultDateTime();
|
||||
}
|
||||
$date->setTimezone(new \DateTimeZone($timezone));
|
||||
return $date;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-2
@@ -4,6 +4,7 @@ namespace Drupal\datetime\Plugin\Validation\Constraint;
|
||||
|
||||
use Drupal\Component\Datetime\DateTimePlus;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
||||
@@ -24,10 +25,10 @@ class DateTimeFormatConstraintValidator extends ConstraintValidator {
|
||||
}
|
||||
else {
|
||||
$datetime_type = $item->getFieldDefinition()->getSetting('datetime_type');
|
||||
$format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
|
||||
$date = NULL;
|
||||
try {
|
||||
$date = DateTimePlus::createFromFormat($format, $value, new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
|
||||
$date = DateTimePlus::createFromFormat($format, $value, new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
|
||||
}
|
||||
catch (\InvalidArgumentException $e) {
|
||||
$this->context->addViolation($constraint->badFormat, [
|
||||
|
||||
@@ -14,11 +14,22 @@ use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
|
||||
* "datestamp" = "timestamp",
|
||||
* "datetime" = "datetime",
|
||||
* },
|
||||
* core = {6,7}
|
||||
* core = {6,7},
|
||||
* source_module = "date",
|
||||
* destination_module = "datetime"
|
||||
* )
|
||||
*/
|
||||
class DateField extends FieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFieldFormatterMap() {
|
||||
return [
|
||||
'date_default' => 'datetime_default',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
||||
@@ -16,7 +16,9 @@ use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
|
||||
* "datestamp" = "timestamp",
|
||||
* "datetime" = "datetime",
|
||||
* },
|
||||
* core = {6}
|
||||
* core = {6},
|
||||
* source_module = "date",
|
||||
* destination_module = "datetime"
|
||||
* )
|
||||
*
|
||||
* @deprecated in Drupal 8.4.x, to be removed before Drupal 9.0.x. Use
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
namespace Drupal\datetime\Plugin\views\argument;
|
||||
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\views\FieldAPIHandlerTrait;
|
||||
use Drupal\views\Plugin\views\argument\Date as NumericDate;
|
||||
|
||||
/**
|
||||
@@ -22,12 +25,36 @@ use Drupal\views\Plugin\views\argument\Date as NumericDate;
|
||||
*/
|
||||
class Date extends NumericDate {
|
||||
|
||||
use FieldAPIHandlerTrait;
|
||||
|
||||
/**
|
||||
* Determines if the timezone offset is calculated.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $calculateOffset = TRUE;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition, $route_match);
|
||||
|
||||
$definition = $this->getFieldStorageDefinition();
|
||||
if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// Timezone offset calculation is not applicable to dates that are stored
|
||||
// as date-only.
|
||||
$this->calculateOffset = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDateField() {
|
||||
// Return the real field, since it is already in string format.
|
||||
return "$this->tableAlias.$this->realField";
|
||||
// Use string date storage/formatting since datetime fields are stored as
|
||||
// strings rather than UNIX timestamps.
|
||||
return $this->query->getDateField("$this->tableAlias.$this->realField", TRUE, $this->calculateOffset);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace Drupal\datetime\Plugin\views\filter;
|
||||
|
||||
use Drupal\Component\Datetime\DateTimePlus;
|
||||
use Drupal\Core\Datetime\DateFormatterInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
use Drupal\views\FieldAPIHandlerTrait;
|
||||
use Drupal\views\Plugin\views\filter\Date as NumericDate;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
@@ -38,7 +40,14 @@ class Date extends NumericDate implements ContainerFactoryPluginInterface {
|
||||
*
|
||||
* @see \Drupal\views\Plugin\views\query\Sql::getDateFormat()
|
||||
*/
|
||||
protected $dateFormat = DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
protected $dateFormat = DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
|
||||
|
||||
/**
|
||||
* Determines if the timezone offset is calculated.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $calculateOffset = TRUE;
|
||||
|
||||
/**
|
||||
* The request stack used to determin current time.
|
||||
@@ -66,10 +75,13 @@ class Date extends NumericDate implements ContainerFactoryPluginInterface {
|
||||
$this->dateFormatter = $date_formatter;
|
||||
$this->requestStack = $request_stack;
|
||||
|
||||
// Date format depends on field storage format.
|
||||
$definition = $this->getFieldStorageDefinition();
|
||||
if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
$this->dateFormat = DATETIME_DATE_STORAGE_FORMAT;
|
||||
// Date format depends on field storage format.
|
||||
$this->dateFormat = DateTimeItemInterface::DATE_STORAGE_FORMAT;
|
||||
// Timezone offset calculation is not applicable to dates that are stored
|
||||
// as date-only.
|
||||
$this->calculateOffset = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,20 +102,23 @@ class Date extends NumericDate implements ContainerFactoryPluginInterface {
|
||||
* Override parent method, which deals with dates as integers.
|
||||
*/
|
||||
protected function opBetween($field) {
|
||||
$origin = ($this->value['type'] == 'offset') ? $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME') : 0;
|
||||
$a = intval(strtotime($this->value['min'], $origin));
|
||||
$b = intval(strtotime($this->value['max'], $origin));
|
||||
$timezone = $this->getTimezone();
|
||||
$origin_offset = $this->getOffset($this->value['min'], $timezone);
|
||||
|
||||
// Formatting will vary on date storage.
|
||||
// Although both 'min' and 'max' values are required, default empty 'min'
|
||||
// value as UNIX timestamp 0.
|
||||
$min = (!empty($this->value['min'])) ? $this->value['min'] : '@0';
|
||||
|
||||
// Convert to ISO format and format for query. UTC timezone is used since
|
||||
// dates are stored in UTC.
|
||||
$a = $this->query->getDateFormat("'" . $this->dateFormatter->format($a, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE);
|
||||
$b = $this->query->getDateFormat("'" . $this->dateFormatter->format($b, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE);
|
||||
$a = new DateTimePlus($min, new \DateTimeZone($timezone));
|
||||
$a = $this->query->getDateFormat($this->query->getDateField("'" . $this->dateFormatter->format($a->getTimestamp() + $origin_offset, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE) . "'", TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
|
||||
$b = new DateTimePlus($this->value['max'], new \DateTimeZone($timezone));
|
||||
$b = $this->query->getDateFormat($this->query->getDateField("'" . $this->dateFormatter->format($b->getTimestamp() + $origin_offset, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE) . "'", TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
|
||||
|
||||
// This is safe because we are manually scrubbing the values.
|
||||
$operator = strtoupper($this->operator);
|
||||
$field = $this->query->getDateFormat($field, $this->dateFormat, TRUE);
|
||||
$field = $this->query->getDateFormat($this->query->getDateField($field, TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
|
||||
$this->query->addWhereExpression($this->options['group'], "$field $operator $a AND $b");
|
||||
}
|
||||
|
||||
@@ -111,15 +126,57 @@ class Date extends NumericDate implements ContainerFactoryPluginInterface {
|
||||
* Override parent method, which deals with dates as integers.
|
||||
*/
|
||||
protected function opSimple($field) {
|
||||
$origin = (!empty($this->value['type']) && $this->value['type'] == 'offset') ? $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME') : 0;
|
||||
$value = intval(strtotime($this->value['value'], $origin));
|
||||
$timezone = $this->getTimezone();
|
||||
$origin_offset = $this->getOffset($this->value['value'], $timezone);
|
||||
|
||||
// Convert to ISO. UTC is used since dates are stored in UTC.
|
||||
$value = $this->query->getDateFormat("'" . $this->dateFormatter->format($value, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE);
|
||||
// Convert to ISO. UTC timezone is used since dates are stored in UTC.
|
||||
$value = new DateTimePlus($this->value['value'], new \DateTimeZone($timezone));
|
||||
$value = $this->query->getDateFormat($this->query->getDateField("'" . $this->dateFormatter->format($value->getTimestamp() + $origin_offset, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE) . "'", TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
|
||||
|
||||
// This is safe because we are manually scrubbing the value.
|
||||
$field = $this->query->getDateFormat($field, $this->dateFormat, TRUE);
|
||||
$field = $this->query->getDateFormat($this->query->getDateField($field, TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
|
||||
$this->query->addWhereExpression($this->options['group'], "$field $this->operator $value");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the proper time zone to use in computations.
|
||||
*
|
||||
* Date-only fields do not have a time zone associated with them, so the
|
||||
* filter input needs to use UTC for reference. Otherwise, use the time zone
|
||||
* for the current user.
|
||||
*
|
||||
* @return string
|
||||
* The time zone name.
|
||||
*/
|
||||
protected function getTimezone() {
|
||||
return $this->dateFormat === DateTimeItemInterface::DATE_STORAGE_FORMAT
|
||||
? DateTimeItemInterface::STORAGE_TIMEZONE
|
||||
: drupal_get_user_timezone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the proper offset from UTC to use in computations.
|
||||
*
|
||||
* @param string $time
|
||||
* A date/time string compatible with \DateTime. It is used as the
|
||||
* reference for computing the offset, which can vary based on the time
|
||||
* zone rules.
|
||||
* @param string $timezone
|
||||
* The time zone that $time is in.
|
||||
*
|
||||
* @return int
|
||||
* The computed offset in seconds.
|
||||
*/
|
||||
protected function getOffset($time, $timezone) {
|
||||
// Date-only fields do not have a time zone or offset from UTC associated
|
||||
// with them. For relative (i.e. 'offset') comparisons, we need to compute
|
||||
// the user's offset from UTC for use in the query.
|
||||
$origin_offset = 0;
|
||||
if ($this->dateFormat === DateTimeItemInterface::DATE_STORAGE_FORMAT && $this->value['type'] === 'offset') {
|
||||
$origin_offset = $origin_offset + timezone_offset_get(new \DateTimeZone(drupal_get_user_timezone()), new \DateTime($time, new \DateTimeZone($timezone)));
|
||||
}
|
||||
|
||||
return $origin_offset;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Drupal\datetime\Plugin\views\sort;
|
||||
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\views\FieldAPIHandlerTrait;
|
||||
use Drupal\views\Plugin\views\sort\Date as NumericDate;
|
||||
|
||||
/**
|
||||
@@ -14,12 +16,38 @@ use Drupal\views\Plugin\views\sort\Date as NumericDate;
|
||||
*/
|
||||
class Date extends NumericDate {
|
||||
|
||||
use FieldAPIHandlerTrait;
|
||||
|
||||
/**
|
||||
* Determines if the timezone offset is calculated.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $calculateOffset = TRUE;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
|
||||
$definition = $this->getFieldStorageDefinition();
|
||||
if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// Timezone offset calculation is not applicable to dates that are stored
|
||||
// as date-only.
|
||||
$this->calculateOffset = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Override to account for dates stored as strings.
|
||||
*/
|
||||
public function getDateField() {
|
||||
// Return the real field, since it is already in string format.
|
||||
return "$this->tableAlias.$this->realField";
|
||||
// Use string date storage/formatting since datetime fields are stored as
|
||||
// strings rather than UNIX timestamps.
|
||||
return $this->query->getDateField("$this->tableAlias.$this->realField", TRUE, $this->calculateOffset);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -185,4 +185,20 @@ abstract class DateTestBase extends WebTestBase {
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Massages test date values.
|
||||
*
|
||||
* If a date object is generated directly by a test, then it needs to be
|
||||
* adjusted to behave like the computed date from the item.
|
||||
*
|
||||
* @param \Drupal\Core\Datetime\DrupalDateTime $date
|
||||
* A date object directly generated by the test.
|
||||
*/
|
||||
protected function massageTestDate($date) {
|
||||
if ($this->field->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// Set the default time for date-only items.
|
||||
$date->setDefaultDateTime();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,8 +43,8 @@ abstract class DateTimeHandlerTestBase extends HandlerTestBase {
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
protected function setUp($import_test_views = TRUE) {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
// Add a date field to page nodes.
|
||||
$node_type = NodeType::create([
|
||||
|
||||
Reference in New Issue
Block a user