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 datetime module has one function it it that isn't a hook: datetime_date_default_time(). This function should not exist.
Proposed resolution
1. @deprecate it with the proper trigger_error().
2. Create a ::setDefaultDateTime() method on DateTimePlus that objects can use for date-only representations.
Remaining tasks
Discuss.
User interface changes
None.
API changes
datetime_date_default_time($date) is @deprecated
Use $date->setDefaultDateTime() instead.
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#47 | interdiff-40-47.txt | 2.7 KB | mpdonadio |
#47 | 2830094-47.patch | 18.55 KB | mpdonadio |
#40 | deprecate_and_remove-2830094-40.patch | 20.18 KB | yogeshmpawar |
Comments
Comment #2
mpdonadioThe hunk(s) in #2829848: Random test failure in DateRangeFieldTest to DateTimeComputed should probably be sufficient for the field/widget/formatter usages.
?
Comment #3
mpdonadioPostponing on #2829848: Random test failure in DateRangeFieldTest, once we figure out the full scope of that.
Comment #4
mpdonadioComment #5
mpdonadioOK, let's remove all non-test uses and see what happens! I think we may have fails in the default date coverage in the widgets. If not, then I think we have missing test coverage.
Comment #7
arunkumarkHi,
I have re-rolled the patch to Drupal 8.4.x
Comment #8
mpdonadioNeeds to be updated for 8.4.0.
Comment #9
mpdonadioThe #5 and #7 patches appear to be identical.
Here is more work on it.
Thoughts on next steps? Thinking we need to centralize this behavior somewhere so we can update the (TBD), but not sure where? Static public method on DateTimeComputed?
Comment #11
mpdonadioThis should pass now.
Comment #12
jhedstromI love this cleanup!
Is the
TBD
to be decided here? I don't think we can deprecate w/o a clear replacement, or example of what to use instead...Comment #13
mpdonadio#12, that's what has me stumped. I did the change in three parallel places in the patch (was in item from another patch, added in default value, added to test base class). I don't know where we should centralize this. My only thought is a static method on DateTimeItem or DateTimeComputed (static so it can be called from procedural code / hooks when needed), but those don't sit well with me.
Comment #14
jhedstromIf we must replace it, then a static method makes sense...if not, just documenting what time should be set in the deprecated notice might work...
While reviewing I found one lingering mention of the function in
Drupal\datetime\DateTimeComputed::getValue()
:Comment #15
jhedstromThis needs a reroll after #2775669: Clean up redundant methods in datetime field formatters.
Comment #16
mpdonadioWorking on this, but found a buglet that crept in via #2786583: Create a base class for DateTimeFieldTest and DateRangeFieldTest and move common code into it. It will likely cause a tiny merge conflict.
Comment #17
mpdonadioOk, here is a redo of #11. It still has the TBD. I'm leaning towards something like
and then weaving this through. That will make it accessible from object oriented and procedural code that may not be dealing with fields/items directly.
Comment #18
mpdonadioRe-roll. Tiny merge conflict in the old DateTestBase.
Comment #19
mpdonadioOK, here is an option for how to handle the deprecation.
Comment #22
jhedstromThe test fails are due to fatals, which are due to the phpunit tests thinking they have this new
massageTestDate
method (which is only added on the legacy test base class). Which brings me to the question, why can't the tests just call the new\Drupal\Core\Datetime\DrupalDateTime::setDefaultDateTime()
method instead?Comment #23
mpdonadio#22, I think I had a comment get swallowed. I think that method fell out during a reroll. I think adding that back to the new BTB test base would be a good idea, and then have that call DrupalDateTime::setDefaultDateTime() is a good idea.
Comment #24
mpdonadioHere is the missing hunk. Should be all green again.
Comment #25
jhedstromCan this also use the new method?
Comment #26
mpdonadioGood idea. Adjusted a few other things. PhpStorm still says no usages left of `datetime_date_default_time()`.
Comment #27
jhedstromI think this is good to go now.
Comment #28
alexpottI would have thought this should have a type hint. Also I think think should be a static method on \Drupal\Core\Datetime\DrupalDateTime and not here because reaching into a plugin like this for common functionality is a bit odd.
Also why are we not copying
Comment #29
mpdonadioPatch; more words to follow when I can use DrEditor on it.
Comment #30
mpdonadioOk, since we are deprecating something, I'll draft a short CR about it.
Where to move this was a head scratcher. I don't disagree with putting the method on \Drupal/Core/Datetime/DrupalDateTime. My main thinking for the field item was that this is really a field-specific activity.
This part also has a tiny odor, but I don't know what to do about it. The old global function would work on a \DateTime, DateTimePlus, and DrupalDateTime. This still will allow that because the hint isn't enforced (yay PHP?), though PhpStorm will complain. However, we can't use a normal invokaction like $date->setDefaultDateTime() since we really do need this to be a static to allow usage on all three types.
After writing all that out and posting the patch, I am wondering if we should make `Drupal/Core/Datetime/DrupalDateTime::setDefaultDateTime` a normal class method (maybe on DateTimePlus?), and keep the `$date->setTime(12, 0, 0);` in `datetime_date_default_time()` to make this cleaner in the long run.
?
Comment #31
jhedstromWon't php fatal error if a type not matching the hint is passed? Testing locally I get this:
I was just looking at the 3 classes, assuming
DateTimePlus
extended\DateTime
, but it doesn't, even though it has this in the docblock:If this new method really should take all 3 types, should we add unit tests to ensure that it does? We might also want a follow-up to actually make
DateTimePlus
extend the php class it purports to (or correct the docblock).Comment #32
mpdonadio#31, apparently I am an idiot :) and PHP has enforced typehints in arguments for a while...
The docblock on DateTimePlus is essentially correct: DTP wraps \DateTime in the sense that it has a protected property, $dateTimeObject that is a \DateTime. While it doesn't extend it properly, it does behave like a \DateTime in most cases because of the __call() magic method. It just cases weird inheritance problems, and is why PhpStorm never autocompletes a lot of things for DDT and DPT.
Setting back to NW to ponder this some more, but thinking that
may be the best option.
Comment #33
mpdonadioThink this should be fully green. Still want to add some test coverage somewhere. Probably the existing DateTimePlusTest and DrupalDateTimeTest unit tests. Not 100% sure if we have a good home to add a test for datetime_date_default_time().
Will write CR when we agree on final approach (and update the IS to match).
Comment #34
mpdonadioOK, here is new approach w/ tests.
I'll update IS and CR if we think this is good.
Comment #35
jhedstromI like this approach better than the static replacement method. I think this is RTBC once the CR and IS are updated.
Comment #37
joachim CreditAttribution: joachim commentedThe deprecated version needs upping as we've missed the boat for 8.4.0.
Comment #38
mpdonadioYup. Patch still applies, so should be a good novice task for someone.
Comment #39
yogeshmpawarComment #40
yogeshmpawarChanges made as per comments in #38 & also added an interdiff
Comment #41
jhedstromThe change record stub needs to be updated (and I guess also the IS?), then I think this is good to go.
Comment #42
mpdonadioComment #43
jhedstromLooks good!
Comment #44
larowlanFeels like this could be in a trait? Or not worth it because the simpletest one has a limited life span?
I think we need a
@group legacy
here to prevent this from triggering deprecation errors in the future.Comment #45
larowlanThis needs to include a link to the change notice too
Comment #46
jhedstromComment #47
mpdonadioOne of the earlier patches accidentally added the old DateTimeTest back, so removing that file from the patch takes care of #44.
Added the CR to the deprecation message per #45.
Did another usage report and search in core for datetime_date_default_time and am not finding any new usages. Think this is good.
Comment #48
jhedstromI'd say not worth it :)
I think this is back to RTBC!
Comment #49
catchAgreed on not needing the trait.
Committed 50ab195 and pushed to 8.5.x. Thanks!
Comment #52
quietone CreditAttribution: quietone at PreviousNext commentedpublish the change record