TimestampItemNormalizer.php in Drupal 8
File
core/modules/hal/src/Normalizer/TimestampItemNormalizer.php
View source
<?php
namespace Drupal\hal\Normalizer;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\TimestampItem;
use Drupal\Core\TypedData\Plugin\DataType\Timestamp;
class TimestampItemNormalizer extends FieldItemNormalizer {
protected $supportedInterfaceOrClass = TimestampItem::class;
protected function normalizedFieldValues(FieldItemInterface $field_item, $format, array $context) {
return parent::normalizedFieldValues($field_item, $format, $context) + [
'format' => \DateTime::RFC3339,
];
}
protected function constructValue($data, $context) {
if (!empty($data['format'])) {
$context['datetime_allowed_formats'] = [
$data['format'],
];
}
return [
'value' => $this->serializer
->denormalize($data['value'], Timestamp::class, NULL, $context),
];
}
}