diff --git a/core/lib/Drupal/Core/TypedData/Primitive.php b/core/lib/Drupal/Core/TypedData/Primitive.php index a2c8125..02fc6b4 100644 --- a/core/lib/Drupal/Core/TypedData/Primitive.php +++ b/core/lib/Drupal/Core/TypedData/Primitive.php @@ -12,7 +12,7 @@ /** * The data value. * - * @var boolean + * @var mixed */ protected $value; diff --git a/core/lib/Drupal/Core/TypedData/Type/Date.php b/core/lib/Drupal/Core/TypedData/Type/Date.php deleted file mode 100644 index 078b8a0..0000000 --- a/core/lib/Drupal/Core/TypedData/Type/Date.php +++ /dev/null @@ -1,49 +0,0 @@ -parent)) { - $this->parent->onChange($this->name); - } - // Don't try to create a date from an empty value. - // It would default to the current time. - if (!isset($value)) { - $this->value = $value; - } - else { - $this->value = $value instanceOf DrupalDateTime ? $value : new DrupalDateTime($value); - } - } -} diff --git a/core/lib/Drupal/Core/TypedData/Type/DateInterface.php b/core/lib/Drupal/Core/TypedData/Type/DateInterface.php deleted file mode 100644 index c68454f..0000000 --- a/core/lib/Drupal/Core/TypedData/Type/DateInterface.php +++ /dev/null @@ -1,9 +0,0 @@ -parent)) { + $this->parent->onChange($this->name); + } + if ($value instanceof DrupalDateTime) { + $this->value = $value->format('c'); + } + // Treat integer values as a UNIX Timestamp. + elseif ((string) (int) $value === (string) $value) { + $date_time = new DrupalDateTime($value); + $this->value = $date_time->format('c'); + } + else { + $this->value = $value; + } + } + + /** + * {@inheritdoc} + */ + public function getDateTime() { + if ($this->value) { + return new DrupalDateTime($this->value); + } + } + + /** + * {@inheritdoc} + */ + public function setDateTime(DrupalDateTime $dateTime) { + $this->value = $dateTime->format('c'); + } +} diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php index d890720..603b08b 100644 --- a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php +++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php @@ -10,7 +10,7 @@ use DateInterval; use Drupal\Core\TypedData\Type\BinaryInterface; use Drupal\Core\TypedData\Type\BooleanInterface; -use Drupal\Core\TypedData\Type\DateInterface; +use Drupal\Core\TypedData\Type\DateTimeInterface; use Drupal\Core\TypedData\Type\DurationInterface; use Drupal\Core\TypedData\Type\FloatInterface; use Drupal\Core\TypedData\Type\IntegerInterface; @@ -40,8 +40,8 @@ public function validate($typed_data, Constraint $constraint) { elseif ($typed_data instanceof BooleanInterface) { $valid = is_bool($value) || $value === 0 || $value === '0' || $value === 1 || $value == '1'; } - elseif ($typed_data instanceof DateInterface) { - $valid = $value instanceOf DrupalDateTime && !$value->hasErrors(); + elseif ($typed_data instanceof DateTimeInterface) { + $valid = $typed_data->getDateTime() instanceOf DrupalDateTime && !$typed_data->getDateTime()->hasErrors(); } elseif ($typed_data instanceof DurationInterface) { $valid = $value instanceof DateInterval; diff --git a/core/modules/datetime/lib/Drupal/datetime/Type/DateTimeItem.php b/core/modules/datetime/lib/Drupal/datetime/Type/DateTimeItem.php index 2d14e5a..86f96bc 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Type/DateTimeItem.php +++ b/core/modules/datetime/lib/Drupal/datetime/Type/DateTimeItem.php @@ -30,7 +30,7 @@ public function getPropertyDefinitions() { if (!isset(self::$propertyDefinitions)) { self::$propertyDefinitions['value'] = array( - 'type' => 'date', + 'type' => 'datetime', 'label' => t('Date value'), ); } diff --git a/core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php b/core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php index fca6f88..79f3521 100644 --- a/core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php @@ -134,18 +134,19 @@ public function testGetAndSet() { // Date type. $value = new DrupalDateTime(); $typed_data = $this->createTypedData(array('type' => 'date'), $value); - $this->assertTrue($typed_data->getValue() === $value, 'Date value was fetched.'); + $this->assertTrue($typed_data->getDateTime()->getTimestamp() == $value->getTimestamp(), 'Date value was fetched.'); + $this->assertEqual($typed_data->getValue(), $typed_data->getDateTime()->format('c'), 'Value representation of a date is ISO 8061'); $this->assertEqual($typed_data->validate()->count(), 0); $new_value = REQUEST_TIME + 1; $typed_data->setValue($new_value); - $this->assertTrue($typed_data->getValue()->getTimestamp() === $new_value, 'Date value was changed and set by timestamp.'); + $this->assertTrue($typed_data->getDateTime()->getTimestamp() === $new_value, 'Date value was changed and set by timestamp.'); $this->assertEqual($typed_data->validate()->count(), 0); $typed_data->setValue('2000-01-01'); - $this->assertTrue($typed_data->getValue()->format('Y-m-d') == '2000-01-01', 'Date value was changed and set by date string.'); + $this->assertTrue($typed_data->getDateTime()->format('Y-m-d') == '2000-01-01', 'Date value was changed and set by date string.'); $this->assertTrue(is_string($typed_data->getString()), 'Date value was converted to string'); $this->assertEqual($typed_data->validate()->count(), 0); $typed_data->setValue(NULL); - $this->assertNull($typed_data->getValue(), 'Date wrapper is null-able.'); + $this->assertNull($typed_data->getDateTime(), 'Date wrapper is null-able.'); $this->assertEqual($typed_data->validate()->count(), 0); $typed_data->setValue('invalid'); $this->assertEqual($typed_data->validate()->count(), 1, 'Validation detected invalid value.'); diff --git a/core/modules/system/system.module b/core/modules/system/system.module index eea7303..a212737 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -2197,7 +2197,7 @@ function system_data_type_info() { ), 'date' => array( 'label' => t('Date'), - 'class' => '\Drupal\Core\TypedData\Type\Date', + 'class' => '\Drupal\Core\TypedData\Type\DateTimeIso8601', ), 'duration' => array( 'label' => t('Duration'),