Problem/Motivation
The Datetime Timestamp widget sets the value of the submitted form element to the current time, if left empty. This essentially forces the form element to always be required.
One case where this might cause difficulties is that a timestamp widget may be intended to be used for Views sorting, with a fallback to the initial entity creation date. When the entity is saved, the field using the Datetime Timestamp widget is populated with the current timestamp, and the entity is always reordered to the start of the view.
Steps to reproduce
- Add a Datetime field to an entity
- Set the new Datetime field to use the Datetime Timestamp widget
- Create a new entity of the aforementioned type
- Edit the entity without editing the Datetime field
- Save the entity
Proposed resolution
There should be a setting for the Datetime Timestamp field widget that allows site builders to choose whether update on save is the desired behavior or not so that empty fields using this form element are possible.
Remaining tasks
- Implement a code and config change for the Datetime Timestamp widget
- Implement tests
User interface changes
None for the end user. Administrative users will be shown a dropdown to allow them to select the intended behaviour for the Datetime Timestamp widget, which will default to the current behaviour, populating the field with the current timestamp on save.
Introduced terminology
None.
API changes
None.
Data model changes
A new use_current_time setting for Datetime Timestamp widgets will be introduced, which will default to the current on submission behaviour.
Release notes snippet
Datetime Timestamp widgets now allow site builders to specify whether fields using this widget are populated on every submission, or when the entity is first created.
| Comment | File | Size | Author |
|---|---|---|---|
| #51 | 2978727-51.patch | 9.91 KB | lawxen |
| #47 | use-current-time-setting-2978727-47.patch | 8.91 KB | cantrellnm |
| #43 | error.png | 157 KB | djsagar |
| #44 | 2024-02-06 18 48 09.png | 101.9 KB | joey91133 |
| #46 | use_current_time_setting_10.2.x.patch | 8.77 KB | nexusnovaz |
Issue fork drupal-2978727
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
krystalcode commentedPatch attached.
Comment #3
krystalcode commentedComment #4
idebr commentedComment #7
tea.time commentedI noticed this same thing. Note the scenario here happens if the field is not required.
+1 for the idea of adding a field setting. I came to the same conclusion in #2978467: Timestamp field widget - "Leave blank to use the time of form submission." is inaccurate if field is required.
Comment #8
mpdonadio@tea.time :wave:
OK, moving this to datetime.module. It's not part of that module proper, but related.
Since this is a bug report, we need a test to demonstrate the bug. There are also BC issues around adding a setting. And I think we have only minimal testing on the item and widget...
A lot of this stems from making this field exposed to the UI, instead of just being a base field. It was originally used as a common point for ChangedItem and CreatedItem, and then exposed in #2707057: Remove no_ui from timestamp field type.
So, I think we need to figure out the subtleties of fixing a bug and adding a new feature (which may be the true way to fix the bug).
Comment #9
sahil432 commentedComment #10
sahil432 commentedUpdated datetime_timestamp field Widget
Comment #11
sahil432 commentedComment #12
sahil432 commentedAnother way of doing that create your own field widget and apply that widget on field.
Comment #14
sahil432 commentedComment #15
sahil432 commentedComment #16
snehi commented@sahil432 #8 demands for test cases.
Apart from it
How you write don't ?
Comment #17
auxiliaryjoel commentedI have 2 timestamps. The 2nd one is only needed if an event runs more than 1 day.
So most of the time, I don't want the 2nd timestamp to display, ONLY when it's filled in.
But both timestamps always display, because the timestamp autofills itself with the date that I created it, when it's left empty.
What can I do to stop this?
EDIT: This worked for me:
{% if content.field_date_finish[0] is empty %}
{{ node.field_date_start.value|date("d/m/Y g:i A") }}
{% endif %}
{% if content.field_date_finish[0] is not empty %}
{{ node.field_date_start.value|date("d/m/Y g:i A, e") }}
-
{{ node.field_date_finish.value|date("d/m/Y") }}
{% endif %}
Comment #18
szeidler commentedThe problem is even more drastic when using a multivalue field.
Let's say you have a Timestamp field with unlimited values.
On every entity save it creates a new item for the timestamp field, because of that behavior.
This also happens when you edit the fields settings with Field UI. There we have the "Default value" settings and on every time when saving the field settings it creates a new item for the default value.
Comment #21
krystalcode commented@sahil432 Your patches change two things:
- The behavior of the widget.
- It removes a feature (letting the system to use the current time if left empty) that many website might actually make use of.
This is not the way to go since that will break many sites that are configured relying on the current behavior. We need to a way that keeps backwards compatibility. That's why on #2 I added a setting that defaults to `TRUE` to maintain the behavior of sites.
Comment #22
krystalcode commentedAdditional thoughts, the default field widget is also used to allow admins/developers to set a default value for the field in general. The widget setting might help with allowing an empty value when the entity is created from the UI, however it is not possible to configure the field to be empty when it is created programmatically.
Currently, it is possible only with custom code where you set a default value callback for the field using `hook_entity_bundle_field_info_alter()`.
Some more thought is required on how this can be accomplished from the Field UI.
Comment #24
vakulrai commentedRerolled the patch for 9.2.x, I agree with #21, and added some logic to the patch given in #2.
I tried to fix the unwanted behaviour raised by #18.
Added a tests to verify the behaviour of "use_current_time" settings.
Please review.
Comment #25
vakulrai commentedFixed one Cspell issue.
Comment #30
merauluka commentedWhen I used the patch from #25 I noted that the "created" field on nodes was reset to the current date even though the field was already set.
Comment #33
jaime@gingerrobot.com commentedHi,
I was able to recreate this issue with a time stamp field in Drupal 10 latest dev and below. There is a ticket to sort out UX for date related fields, widgets and formatters. https://www.drupal.org/project/drupal/issues/2897397
I believe the consensus will be to allow this widget to work exactly the same as the Date time widget (can they use the same widget??).
TODO:
Patch still needs testing / tests to pass.
Comment #34
ravi.shankar commentedAdded reroll of patch #25 on Drupal 10.1.x.
Comment #35
pradhumanjain2311 commentedTry to fix PHPStan Errors.
Comment #38
dieterholvoet commentedThe config schema was in the wrong place, causing it to not be used. I started a MR based on the last patch and fixed this issue there.
Comment #41
joey91133 commentedfix conflict on 11.x
Comment #42
joey91133 commentedComment #43
djsagar commentedHi @joey91133,
Getting error while applying MR.
moving back to NW.
After MR:
Comment #44
joey91133 commented@djsagar
This is a bug on gitlab.
You should use .diff file not .patch.
I don't know why gitlab will cache patch file on MR.
Comment #45
smustgrave commentedWith a schema change going to need an upgrade path + tests around that.
Issue summary will also need to be updated to use standard issue template. With a UI change we probably need a change record.
Comment #46
nexusnovaz commentedUsed the 4681 MR to update the patch so it applies on Drupal 10.2.x.
Comment #47
cantrellnm commented#46 doesn't apply for me with Drupal 10.2.7 so here's a re-roll
Comment #48
dieterholvoet commentedI rebased the MR against 11.x and fixed an issue where the field value was always overwritten, even if the field already had a value.
Comment #50
lendudeThere was a fatal error when submitting an empty value. Fixed the error and added some test assertions to check this scenario.
Comment #51
lawxen commentedReroll (Merge request !4681) for 10.3.9
Comment #52
philyPatch #51 works for me using Drupal 10.3.9
Comment #53
chi commentedI've just released a module that implements the date/time fields in a different way.
It also supports HTML5 elements in views exposed forms
https://www.drupal.org/project/date_point
Comment #57
geoffreyr commentedI think I've fixed the TimestampTest by ensuring that
$this->fieldNamegets set. I still see a lot of tests failing but none of them relate to the timestamp widget.BTW hi @man-1982 long time no see (:
Comment #58
smustgrave commentedOpen tags still seem needed
Comment #59
smustgrave commentedActually missed one as upgrade path will need test coverage
Comment #60
geoffreyr commented@smustgrave Thanks for your checks. I've updated the issue description to try and conform to the current template. I don't know much about upgrade path tests but I'll try and figure this out while I'm working on this issue.
I might also see about adjusting the MR and tests further to add functionality. I'd like to ensure we've got an "on demand" setting that lets the field remain empty as long as we need it to.
Comment #61
geoffreyr commentedIssue summary updated as of #60.
Comment #62
markdcThe Date Point module mentioned in #53 perfectly resolves this issue and others:
Since I'm starting a new project, I will opt for this module rather than patched core fields.
Comment #63
finex commentedI've just found this alternative module which try solve the core bug: https://www.drupal.org/project/timestamp_nullable