Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
The core ChangedItem, CreatedItem, and TimestampItem fields have an implicit dependency on datetime.module because they define their `default_widget` as "datetime_default", which is provided by the datetime.module:
/**
* Defines the 'timestamp' entity field type.
*
* @FieldType(
* id = "timestamp",
* label = @Translation("Timestamp"),
* description = @Translation("An entity field containing a UNIX timestamp value."),
* default_widget = "datetime_default", // <-- provided by datetime.module
* default_formatter = "timestamp",
* constraints = {
* "ComplexData" = {
* "value" = {
* "Range" = {
* "min" = "-2147483648",
* "max" = "2147483648",
* }
* }
* }
* }
* )
* )
*/
Proposed resolution
Update the annotation on these classes to use "datetime_timestamp" as the default widget.
Add a functional test to make sure the widget works.
Remaining tasks
None.
User interface changes
None.
API changes
None.
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#29 | interdiff-18-28.txt | 688 bytes | mpdonadio |
#29 | 2802663-28.patch | 7.7 KB | mpdonadio |
#18 | interdiff-16-18.txt | 786 bytes | mpdonadio |
#18 | 2802663-18.patch | 7.03 KB | mpdonadio |
#16 | 2802663-16.patch | 7.03 KB | mpdonadio |
Comments
Comment #2
BerdirComment #3
BerdirI was wrong, we do have one. \Drupal\Core\Datetime\Plugin\Field\FieldWidget\TimestampDatetimeWidget/datetime_timestamp is what they should use.
That makes this a novice issue I think, just switch to the right type. Since they are not UI addable, I'm not sure we can write any useful tests, though.
Comment #4
BerdirAnd I'm wrong again: #2707057: Remove no_ui from timestamp field type
Comment #5
mpdonadioComment #6
mpdonadioNot sure this is really a novice issue.
#2552799: FieldType with no available widget causes Fatal error added the widget to this field.
#2707057: Remove no_ui from timestamp field type made Timestamp available from the UI.
With the attached, adding this field to an entity and then trying to add the entity via the UI fatals when just loading the page:
The timestamps on nodes, etc, use this widget for base fields. I suspect those are never empty, so they never barf here. I think I see what is happening, but want to wrap my head around this first before posting more work.
I'll see if I can work this into a failing test tonight or tomorrow.
Comment #7
mpdonadioWidget fix and my work-in-progress on the test.
Comment #9
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedWhich field type are you adding? With the patch from #6 applied, I added a Timestamp field to a node bundle, it used the 'datetime_timestamp' widget by default and I see no error when adding a node of that type :/
Comment #10
mpdonadio#9, it was a timestamp w/o a default value.
IIRC correctly from debugging this the other night, $items[$delta]->value was coming in as an empty string, which DrupalDateTime::createFromTimestamp() didn't like b/c DateTimePlus::createFromTimestamp() has an explicit is_numeric() check, which led to adding this hunk.
Comment #11
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedWeird, the timestamp field that I created also didn't have a default value..
Comment #12
mpdonadioI'll check this again tonight; I was working on a few issues, but I am nearly sure I was doing a clean install between all of them. My dev machine MAMP is also set for PHP7 (so tests run faster...), so I will double check with 5.5 and 5.6, too. It probably doesn't make a difference, but my clean install script defaults to adding a dev-oriented settings.local.php. I may also switch the test to WTB so I can finish it quicker, and then change it to BTB once we know it is good (I'm finding BTB much harder to write from scratch b/c debugging output is less convenient).
Comment #13
mpdonadioI can't make #6 happen any more. No clue why I saw that error other than maybe my database had cruft in it from another issue and I wasn't actually developing from a clean install.
Here is basic coverage of the widget. Probably can be refined. Tried to simplify some of the patterns existing tests use, but may have gone a little too far with assumptions / hardcoded values.
Comment #14
jhedstromThis is looking good--tagging for an IS update to address the proposed resolution from 'no clue' :)
Some tiny nits:
Needs a real group? :)
Missing period.
Class is using a mix of traditional array syntax and bracket, should probably be all brackets.
Comment #15
mpdonadioNits will be picked shortly.
Comment #16
mpdonadioAddressed #14. The "field" @group seems most appropriate; this really isn't datetime proper. Also added an assertion to make sure the "datetime_timestamp" widget is actually used.
Comment #17
jhedstromExtra line break here, I missed this before.
I think this is RTBC once that is fixed.
Comment #18
mpdonadioAddressed #17 and two other minor things phpcs noticed.
Comment #19
jhedstromI think this is good to go!
Comment #20
alexpottI'm pondering if there are API or BC implications of changing this. Existing fields won't be affected right?
But should the datetime module alter the defaults to use it's formatter when it is installed?
Comment #21
mpdonadio#20, I don't think there are any API implications here, which is why I added a pretty explicit test. I don't think there are any BC problems either. Bottom line is that the timestamp and datetime widgets are formatters aren't compatible because the typed data is different. Timestamps are ints, and datetime are DrupalDateTime (fancy \DateTime). If the datetime widgets or formatters get used with a timestampitem, then you get a fatal (maybe a nasty warning, would have to force a way to reproduce this again). I think we also have pretty decent coverage on created and changed on nodes, which use the timestamp stuff.
Down the road, we could consider whether we want the datetime widgets/formatters could extend the timestamp ones, but haven't put much thought into whether it is worthwhile.
Comment #23
charginghawk CreditAttribution: charginghawk at Genuine commentedI've tested this and it works for me. Please merge! Right now, timestamp fields just aren't working on default installs.
(Arrived here via #2836053: Datetime timestamp field)
Comment #24
jibranDon't we need an upgrade path for existing fields?
I think this much safer approach also datetime_install can update existing fields widgets.
Comment #25
jibranNW for #24.
Comment #26
BerdirThose are plugin definitions, the only update it would need is a cache clear.
Comment #27
mpdonadio#24, I don't see how datetime.module can do this. The typed data is different. Timestamps, as provided by core, are signed integers. Datetime (and daterange) are ISO strings. We could probably support this, but it is decent change to the widgets/formatters and we are trying to simplify the internals of them right now. There are also field setting differences. We would also have to have an uninstall hook to switch them back if datetime.module gets uninstalled. Also not 100% sure we can warn people about this before uninstall with the way dependencies work.
?
What is the current consensus on empty update hooks / post update hooks? I can add one, but a decent amount are being stripped on commit because we have rebuild already from other ones.
Comment #28
jibranEmpty post-update hooks should be fine.
Comment #29
mpdonadioOK, here's an empty post-update hook to force plugin definitions to be re-read.
Comment #30
jibranOver to @alexpott then.
Comment #31
xjmDrupal 8 currently has seven active committers; let's not make assumptions.
I would not normally backport this to a patch release, but since having correct dependencies has implications for dependency management, data integrity, etc., I'm considering whether to add it to 8.3.x. It also sounds like it is major if this is causing the feature to not work at all on default installations.
Meanwhile, committed to 8.4.x. Leaving RTBC against 8.3.x while I get a second opinion on the backport.
Comment #32
xjmActually, sorry. I just noticed this is adding a dependency in core/lib on datetime.module, no? We're not supposed to do that. Reverted and setting back to NR to discuss that.
Comment #33
xjmComment #34
BerdirNope, the opposite, this removes that dependency.
datetime_timestamp, despite the misleading name is \Drupal\Core\Datetime\Plugin\Field\FieldWidget\TimestampDatetimeWidget. datetime_default is \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeDefaultFormatter.
Comment #35
xjmOh, hah. Thanks @Berdir. Okay, recommitting to 8.4.x based on that and also backporting to 8.3.x, because yeah, that's definitely a problem. Thanks!
Comment #36
xjmComment #37
jibranI was not making assumptions. I just wanted to be sure that his concerns in #20 are addressed properly. If you were fine with answering those then I have no problem with it. :)
Thank you for committing this and thank you for all the awesome work with RTBC queue.
Comment #38
charginghawk CreditAttribution: charginghawk at Genuine commentedThanks!