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.

Issue fork drupal-2978727

Command icon 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

krystalcode created an issue. See original summary.

krystalcode’s picture

Patch attached.

krystalcode’s picture

Title: Use current time setting the timestamp widget » Use current time setting for the timestamp widget
idebr’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: drupal-use-current-time-setting-2978727-2.patch, failed testing. View results

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

tea.time’s picture

I 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.

mpdonadio’s picture

Component: forms system » datetime.module
Issue tags: +Needs tests, +DrupalCampNJ2019

@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).

sahil432’s picture

Assigned: Unassigned » sahil432
sahil432’s picture

StatusFileSize
new1.45 KB

Updated datetime_timestamp field Widget

sahil432’s picture

Status: Needs work » Needs review
sahil432’s picture

Another way of doing that create your own field widget and apply that widget on field.

Status: Needs review » Needs work

The last submitted patch, 10: timpstamp-widget-update-2978727-10.patch, failed testing. View results

sahil432’s picture

StatusFileSize
new1.86 KB
sahil432’s picture

Status: Needs work » Needs review
snehi’s picture

Status: Needs review » Needs work

@sahil432 #8 demands for test cases.
Apart from it

+++ b/core/lib/Drupal/Core/Datetime/Plugin/Field/FieldWidget/TimestampDatetimeWidget.php
@@ -35,7 +35,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
+    $element['value']['#description'] = $this->t('Format: %format. Leave blank if you don\'t want to set any value.', ['%format' => Datetime::formatExample($date_format . ' ' . $time_format)]);

How you write don't ?

auxiliaryjoel’s picture

I 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 %}

szeidler’s picture

Version: 8.6.x-dev » 8.8.x-dev
Assigned: sahil432 » Unassigned
StatusFileSize
new3.24 MB

The 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.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

krystalcode’s picture

@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.

krystalcode’s picture

Additional 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.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

vakulrai’s picture

Status: Needs work » Needs review
StatusFileSize
new8.55 KB
new7.33 KB

Rerolled 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.

vakulrai’s picture

StatusFileSize
new8.55 KB

Fixed one Cspell issue.

Status: Needs review » Needs work

The last submitted patch, 25: current_time_timestamp-2978727-25.patch, failed testing. View results

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

merauluka made their first commit to this issue’s fork.

merauluka’s picture

When 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.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jaime@gingerrobot.com’s picture

Issue tags: +Usability, +widget, +behaviors, +DrupalSouth

Hi,

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.

ravi.shankar’s picture

StatusFileSize
new8.4 KB
new5.05 KB

Added reroll of patch #25 on Drupal 10.1.x.

pradhumanjain2311’s picture

StatusFileSize
new8.99 KB
new1.16 KB

Try to fix PHPStan Errors.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

DieterHolvoet made their first commit to this issue’s fork.

dieterholvoet’s picture

The 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.

joey91133 made their first commit to this issue’s fork.

joey91133’s picture

fix conflict on 11.x

joey91133’s picture

Status: Needs work » Needs review
djsagar’s picture

Status: Needs review » Needs work
StatusFileSize
new157 KB

Hi @joey91133,

Getting error while applying MR.

moving back to NW.

After MR:

error

joey91133’s picture

Status: Needs work » Needs review
StatusFileSize
new101.9 KB

@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.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests, -DrupalCampNJ2019, -widget +Needs issue summary update, +Needs upgrade path

With 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.

nexusnovaz’s picture

StatusFileSize
new8.77 KB

Used the 4681 MR to update the patch so it applies on Drupal 10.2.x.

cantrellnm’s picture

StatusFileSize
new8.91 KB

#46 doesn't apply for me with Drupal 10.2.7 so here's a re-roll

dieterholvoet’s picture

I 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.

lendude made their first commit to this issue’s fork.

lendude’s picture

There was a fatal error when submitting an empty value. Fixed the error and added some test assertions to check this scenario.

lawxen’s picture

StatusFileSize
new9.91 KB

Reroll (Merge request !4681) for 10.3.9

phily’s picture

Patch #51 works for me using Drupal 10.3.9

chi’s picture

I'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

man-1982 made their first commit to this issue’s fork.

geoffreyr made their first commit to this issue’s fork.

geoffreyr’s picture

Status: Needs work » Needs review

I think I've fixed the TimestampTest by ensuring that $this->fieldName gets 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 (:

smustgrave’s picture

Status: Needs review » Needs work

Open tags still seem needed

smustgrave’s picture

Actually missed one as upgrade path will need test coverage

geoffreyr’s picture

Issue summary: View changes

@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.

geoffreyr’s picture

Issue summary updated as of #60.

markdc’s picture

The Date Point module mentioned in #53 perfectly resolves this issue and others:

  • Blank values are possible for non-required fields
  • Views filters compares against current time using semantically friendlier "now"
  • Seconds can be disabled for the user
  • Date and time are combined into a single input field

Since I'm starting a new project, I will opt for this module rather than patched core fields.

finex’s picture

I've just found this alternative module which try solve the core bug: https://www.drupal.org/project/timestamp_nullable

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.