Problem/Motivation

The datetime_timestamp field widget does not respect the field's default value or default value callback. Instead, the entity form's date + time widgets are left empty and the current time is saved to the field if one is not provided.

This change was introduced in 10.2.1 in #2791693: Remove sample date from date field error message and title attribute with a check in TimestampDatetimeWidget::formElement() to only use the field item delta value if the entity is "not new": https://git.drupalcode.org/project/drupal/-/commit/5404ebfb11155b53dee0e...

Steps to reproduce

  1. Add a timestamp field eg: timestamp_test, to an entity.
  2. Set a default value for the timestamp_test field, eg: 01/01/2024 - 12:00:00 AM
  3. Configure the entity form display to use the datetime_timestamp "Datetime Timestamp" widget.
  4. Go to create a new entity. Observe that that the timestamp_test field does not use the configured default value.
  5. Save the entity. Observe that the current time is saved to the timestamp_test field.

Proposed resolution

Do not check if the entity is new. Instead use the previous logic: If a field item delta value is set, use it to create a new drupal date time object.

Remaining tasks

Implement.
Add tests
Review

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

datetime_timestamp widget is fixed to respect field default values.

CommentFileSizeAuthor
#11 3414883-11.patch1.43 KBpaul121

Issue fork drupal-3414883

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

paul121 created an issue. See original summary.

paul121’s picture

Status: Active » Needs review
smustgrave’s picture

Version: 10.2.x-dev » 11.x-dev
Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

Thank you for reporting.

Moving to 11.x as the current development branch, MR should be updated also.

This will also require test coverage.

acbramley changed the visibility of the branch temp to hidden.

acbramley changed the visibility of the branch 3414883-datetimetimestamp-widget-does to hidden.

acbramley’s picture

Recreated the MR against 11.x, please close your one @paul121

acbramley’s picture

Apologies @paul121 I just saw you were fixing that branch at the same time.

paul121’s picture

StatusFileSize
new1.43 KB

No worries, I'll close that one. I won't have time to work on tests anytime soon.

Attaching a patch we can use in farmOS.

acbramley’s picture

Assigned: Unassigned » acbramley

I'll work on some tests

acbramley’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

Added test coverage, fails with the following without the fix:

The field "field_timestamp[0][value][date]" value is "", but "2024-01-16" expected.
acbramley’s picture

Assigned: acbramley » Unassigned
acbramley’s picture

Title: Datetime_timestamp widget does not use default field value » [regression] datetime_timestamp widget does not use default field value
smustgrave’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

As a regression this seemed higher to me.

Running the tests get the same as @acbramley does in #13

Was able to confirm the issue and that the MR fixes it.

Again as a regression believe it's good to mark now vs waiting a week.

quietone’s picture

Issue summary: View changes

As a regression this seemed higher to me.

Can you elaborate? Higher than what?

Again as a regression believe it's good to mark now vs waiting a week.
I do not understand this. What happens in a week?

---
Thanks for the complete issue summary!

I compared the change here to the commit for the other issue and confirmed that it doing as stating in the proposed resolution.

I tested and confirmed the problem. Then, I applied the diff and ran the test, which does fail without the fix. The test could be added to \Drupal\FunctionalTests\Datetime\TimestampTest::testWidget() and would not interfere with that test. I am in two minds about that but will leave that for now.

Leaving at RTBC

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

catch’s picture

The test could be added to \Drupal\FunctionalTests\Datetime\TimestampTest::testWidget() and would not interfere with that test. I am in two minds about that but will leave that for now.

I agree with this. If we add assertions to the test method, we save an entire Drupal install, which can make a meaningful difference to gitlab ci runtimes, so when it's easy to do that, it's worth doing.

Went ahead and made the change locally, ran the test, and pushed. Assuming the whole pipeline comes green I'll go ahead and commit this, since it's only deleting three lines or so.

  • catch committed c59d7a75 on 10.3.x
    Issue #3414883 by paul121, acbramley, catch, smustgrave, quietone: [...

  • catch committed 2fe69efe on 11.x
    Issue #3414883 by paul121, acbramley, catch, smustgrave, quietone: [...

  • catch committed 68712737 on 10.2.x
    Issue #3414883 by paul121, acbramley, catch, smustgrave, quietone: [...
catch’s picture

Version: 11.x-dev » 10.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x and cherry-picked to 10.3.x and 10.2.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.