Problem/Motivation
I have a date range field in a node using the Smart Date, date-only range widget. Whenever I save the node, the start date is decremented by one day and the end date is incremented by one day. The issue only occurs with that one widget - the "Smart Date and time" widget doesn't change the field values on save.
Steps to reproduce
Create a node with a date range field, set default start value and end value as Current date. Configure the Form Display to use the Smart Date, date-only range widget. Add a new node and save it. The start date will; be yesterday and the end date will be tomorrow. Each time the node is saved the dates get farther apart. (You can change the date values, but when saved they will be one day different from what you set them to.
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | smart_date-date_only_expands-3303444-9.patch | 1.79 KB | mandclu |
| #6 | 3303444-6.patch | 1.19 KB | narendra.rajwar27 |
| #5 | 3303444-5.patch | 1.22 KB | peter pulsifer |
| #4 | 3303444-4.patch | 1.22 KB | peter pulsifer |
Issue fork smart_date-3303444
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
peter pulsifer commentedThe problem is in smart_date/src/Plugin/Field/FieldWidget/SmartDateOnlyWidget.php, lines 53 to 59:
If I comment out that section the dates are not changed.
However, I don't know why this causes the problem. It appears that before being reset, the time part of the field is set to the current time, so the duration is an exact number of days. "Force to all day" increases that duration. Maybe the duration is being remembered somewhere and the
parent::massageFormValues()that follows adjusts the beginning date to be (original end date) minus duration, the adjusts the end date to (original beginning date) plus duration? The interval always increases by two days.Comment #3
mandclu commentedI suspect that the problem has something to do with timezones, but I haven't had an opportunity to test it thoroughly.
Comment #4
peter pulsifer commentedYes, I think you're right. Setting the tine zone to the system default (as is done in SmartDateWidgetBase.php) seems to solve the problem. It might be simpler to eliminate the "force to all day" code, but that might have other consequences. I found that the time was initially set to the current time; that seems to happen both with field setting default as "current date" and no default value.
Comment #5
peter pulsifer commentedSorry, careless copy/past in the previous patch - the second test should be on 'end_value', not 'value'. (I noticed that when I entered an invalid date and it threw an error.) See the attached revision (3303444-5).
Comment #6
narendra.rajwar27Adding re-rolled patch, Since patch from comment #5 is not getting applied.
Comment #7
peter pulsifer commentedJust as a note related to this issue, the code makes an interpretation that might not be expected. If the start and end dates are the same, the start value and end value in the date field will NOT be equal, but will rather be just under 24 hours apart. (And stripping the time might not result in equal dates, depending on time zone considerations.) Making the interval between start and end be a full day often makes sense, but it could be confusing programmatically that the values are not the same, when the widget displays the same dates.
Comment #8
mandclu commentedReviewing this again, I think it would make sense to incorporate the proposed changes, as well as omit the block of code identified in #2 for core date range fields. Not 100% sure if this might be a contributor to the problems reported here, but if so worth testing. I'll try to create a new patch shortly.
Comment #9
mandclu commentedHere's a new patch based on the ideas in the previous comment.
Comment #10
marksmith commentedThis is still an issue in SmartDate 4.2.1. Patch #9 appears to solve the problem.
Comment #11
mandclu commentedAdded an MR that targets the 4.2.x branch.