Problem/Motivation
Currently DateRangeCustomFormatter has a logic block:
if ($start_date->getTimestamp() !== $end_date->getTimestamp()) {
$elements[$delta] = [
'start_date' => $this->buildDate($start_date),
'separator' => ['#plain_text' => ' ' . $separator . ' '],
'end_date' => $this->buildDate($end_date),
];
}
else {
$elements[$delta] = $this->buildDate($start_date);
}
This is to prevent a date string being duplicated w/ a separator. Since this formatter accepts custom format strings, this can result in weird output like
2016 - 2016
when the format string is 'Y' or anything other than the full granularity of the type.
The DateRangeDefaultFormatter has the same problem, when used with a date format that doesn't display all date components, e.g. `html_year`.
Proposed resolution
Instead of comparing using ->getTimestamp(), use the ->formatDate() method to check the actual rendered string (adjusted for timezone).
Steps to reproduce
- Install Drupal 10.1.x-dev (Standard profile) in English language
- Enable "Datetime Range" module
- Create a new date range field for articles:
- Go to Structure > Content types > Article > Create new field
- In "Add a new field" select "Date range"
- As "Label" set "Date range"
- Click "Save and continue"
- Click "Save field settings"
- Configure either the default or custom date range formatter with a date format that doesn't show all date components:
- Go to Structure > Content types > Article > Manage display
- Verify that the selected formatter for the "Date range" field is set to "Default".
- Expand the formatter settings by clicking on the gear icon to the right of the select field.
- As "Date format" select a date format that doesn't show all date components, e.g. "Olivero Medium" or "HTML year".
- Click "Update"
- Click "Save"
- Create a new article with a date time range with two dates where the date components displayed by your selected date formats are equal, but the rest of the components not displayed are not equal:
- Go to Content
- Click "Add content"
- Click "Article"
- Enter a title, e.g. "123".
- In the "Date range" field, enter 2023-05-08 17:00:00 as "Start date".
- In the "Date range" field, enter 2023-05-08 19:30:00 as "End date".
- Click "Save"
- Verify that on the canonical view of the article, the date range is shown as "8 May, 2023 - 8 May, 2023".
- Apply patch.
- Clear all caches.
- Verify that on the canonical view of the article, the date range is shown as "8 May, 2023".
Actual result
The date range field displays as START DATE SEPRATOR END DATE even though both start date and end date are the same.
Expected result
The date range field displays as START DATE since both dates are equal.
Remaining tasks
User interface changes
API changes
Data model changes
Merge request to commit
| Comment | File | Size | Author |
|---|
Issue fork drupal-2823847
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
elakiyasamuel commentedComment #3
jungleComment #4
jungleComment #5
junglefixed a typo.
Comment #7
jungleComment #8
mpdonadioThanks for working on this.
I think we need some additional test coverage here. For all three field flavors, we should check a format with everything for that flavor (the ones that are there should be fine), and then also test 'Y'. This way we can make sure we test both paths through the if statement that we adjusted.
Comment #9
jungleComment #10
jungleTests added for all three field flavors.
Comment #11
mpdonadioI am getting local fails on line 177 of DateRangeFieldTest.
This is where I am getting the fail locally, but we need to test this behavior when different dates in the same year get folded down to a single year on output.
We need to test this behavior when different dates in the same year get folded down to a single year on output. The dates are the same here, so this doesn't really cover the code change.
We need to test this behavior when different dates in the same year get folded down to a single year on output. The dates are the in different years here, so this doesn't really cover the code change.
We need to test this behavior when different dates in the same year get folded down to a single year on output. The dates are the same here, so this doesn't really cover the code change.
We need to test this behavior when different dates in the same year get folded down to a single year on output. The dates are the same here, so this doesn't really cover the code change.
Why was this needed?
Nit, exrta blank line.
Comment #13
mpdonadioAnd this one is on me. `$this->formatDate` is incorrect here. It doesn't take timezone into account, and it doesn't apply the date-only logic properly. We need to use buildDate(). I think we can use the render arrays from buildDate, and compare the '#plain_text' entries, and then just reused the build arrays in the final version we return.
Comment #26
feyp commentedI don't think this is a feature request, I think this is a bug. Since the module already compares the time stamps, clearly the intended functionality is to only display a single date, if the dates don't differ.
I'd also like to rescope the issue to also include the default date formatter. It has the same problem and I think it makes sense to fix both in one go. Updating title and IS.
W/r/t #13, I think the remarks are outdated. buildDate() now just calls formatDate() and looking at the code, it takes the time zone into account and works with the date-only logic. So I think instead of buildDate() we should stick to using formatDate(). Please correct me, if I'm wrong. Updated the proposed resolution in the IS.
I updated the patch for Drupal 10.1. For the test coverage, I added data providers to cover more date ranges and additional code to test the different formatters with the html_year date format. This should address #11.
Comment #27
feyp commentedHopefully fixing the cspell errors...
Comment #29
smustgrave commentedThis was much easier to review applying locally vs the diff haha.
Bulk of the changes are just indent moves (for others who review).
Taking a look at the changes and conceptually makes sense to me.
But could we had explicit steps for how this could appear? Then can add some before/after screenshots to show the problem and now that it's fixed.
Will keep an eye out for this one.
Comment #30
feyp commentedAdded screenshots for before/after.
Comment #31
feyp commentedThanks for your review @smustgrave, it is really appreciated! I updated the issue summary with steps to reproduce for the default formatter and actual result and expected result including before/after screenshots.
Comment #32
smustgrave commentedKudos for the best steps to reproduce I may have seen.
I think this is good for committer review now.
Comment #37
feyp commentedRe-uploading patch #27 so that the correct patch is re-tested every 2 days. RTBC per #32.
Comment #39
feyp commentedRandom test failure, see #3361121: [random test failure] InstallerExistingConfig[SyncDirectory]MultilingualTest::testConfigSync, retest was green. Back to RTBC per #32.
Comment #41
lauriiiThe new behavior makes sense. Not going to ask for subsystem maintainer review because I see that @mpdonadio filed this issue and has been active here.
I didn't review all of the test changes in detail yet because I'm wondering if we need to convert this to a data provider? This is a browser based test meaning that every individual data set requires a new installation of Drupal. The test currently takes 43 seconds to run, and the new test takes almost 9 minutes.
Comment #42
feyp commentedThanks @lauriii for the review. Fair enough, I'll change this to foreach loops. Not sure yet if I have time left this weekend, might have to wait until next weekend.
Should we do the same in #2904899: Invalid argument exception when changing language of node with menu link to und or zxx?
Comment #43
lauriiiThat other one has just 2 iterations at the moment so it isn't as critical. Usually it's not a big deal, and it's fine to use providers for their improved DX but on this issue the difference was significant enough to warrant the DX hit.
Comment #44
feyp commentedThanks @lauriii for the clarification in #43, very helpful.
I replaced the data providers with foreach loops, this should address #41. Patch and interdiff without indentation changes provided for convenience of reviewers.
Comment #46
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #47
feyp commentedSorry, bot, but you tested the wrong patch...
Comment #48
nod_He does need a little help to know which patch is relevant, hiding the extra patches lets him know what to ignore :)
Comment #49
kpoornima commentedMay be patch #44.Patch #44(2823847-44.patch) is worked for me. After applied patch date range view is
21 August, 2023
Comment #50
jonathanshawBack to RTBC
Comment #51
quietone commentedI'm triaging RTBC issues. I read the IS and the comments. There are new tests and a fail test. I didn't find any unanswered questions or other work to do.
Leaving at RTBC.
Comment #54
feyp commentedI wasn't able to reproduce the test fail locally, so I used the opportunity to switch to an MR based on patch #44. Let's see what the pipeline says.
Comment #55
smustgrave commentedPosting test-only results here
Restoring RTBC status from before.
Comment #56
longwaveMR needs rebasing.