Problem/Motivation
#2768651: Let TimestampItem (de)normalize to/from RFC3339 timestamps, not UNIX timestamps, for better DX fixed the normalization of "Time fields" aka the TimestampItem
. But it did so by adding a normalizer for the @FieldType
level. It could have been implemented just as well at the @DataType
level, but this simply didn't cross anybody's mind AFAICT. At least it wasn't discussed on the issue. (And I was very active on it too, so my bad!)
IOW: not a normalizer with
use Drupal\Core\Field\Plugin\Field\FieldType\TimestampItem;
…
protected $supportedInterfaceOrClass = TimestampItem::class;
but with
\Drupal\Core\TypedData\Plugin\DataType\Timestamp
…
protected $supportedInterfaceOrClass = Timestamp::class;
The benefit is that the normalizers are then no longer format-specific: we'll need only one and it'll work for both the default normalization (serialization
module: json
+ xml
formats) and the HAL normalization (hal
module: hal_json
format). But also for contrib's jsonapi
normalization/format!
Note: this will also fix #2870609: [PP-1] Core Date field is serialized as String instead of timestamp/long.
Proposed resolution
- Add
serializer.normalizer.timestamp
- Add
\Drupal\serialization\Normalizer\TimestampNormalizer
- Deprecate
\Drupal\serialization\Normalizer\TimeStampItemNormalizerTrait
See #72 for a clear overview of the patch.
Remaining tasks
None.
User interface changes
None.
API changes
@FieldType=timestamp
: no changes!@FieldType=datetime
fields configured to store date + time:- Before
'2017-03-01T20:02:00'
Note: timezone information is absent!
- After
'2017-03-01T20:02:00+11:00'
Note: the site's timezone is present! This is now a valid RFC3339
@FieldType=datetime
fields configured to store date only:- Before
'2017-03-01T20:02:00'
Note: time information is present despite this being a date-only field!
- After
'2017-03-01'
RFC3339 only covers combined date and time representations. For date-only representations, we need to use ISO 8601. There isn't a particular name for this date-only format, so we have to hardcode the format. See https://en.wikipedia.org/wiki/ISO_8601#Calendar_dates.
Data model changes
None.