DateTime Element set

Created on 28 August 2025, 21 days ago

Problem/Motivation

The following DateTime form element was working but since Issue #3505318 it is not working:

  $form['test_form']['important_time'] = [
    '#type' => 'datetime',
    '#title' => t('Important time'),
    '#size' => 20,
    '#date_date_element' => 'none',
    '#date_time_element' => 'time',
    '#date_time_format' => 'H:i',
    '#description' => t('Enter a value in 24h format (HH:MM:SS) to set the important time.'),
    '#default_value' => $form,
  ];

Since this date we need to set the format to 'none':

  $form['test_form']['important_time'] = [
    '#type' => 'datetime',
    '#title' => t('Important time'),
    '#size' => 20,
    '#date_date_element' => 'none',
    '#date_date_format' => 'none',
    '#date_time_element' => 'time',
    '#date_time_format' => 'H:i',
    '#description' => t('Enter a value in 24h format (HH:MM:SS) to set the important time.'),
    '#default_value' => $form,
  ];

The previous behaviour was correct from my reading of the doc block, we should not have to set an form display format to 'none' if we are already saying we do not want to display an element.

Steps to reproduce

1. Create the first form element in a form
2. Attempt to submit the form
3. Note the error on submit

Proposed resolution

1. Update logic in DateTime to set the format to 'none' if the element is set to 'none'

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component

datetime.module

Created by

πŸ‡¦πŸ‡ΊAustralia RichardGaunt Melbourne

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @RichardGaunt
  • πŸ‡¦πŸ‡ΊAustralia RichardGaunt Melbourne
  • Merge request !13102Resolve #3543555 "Datetime element set" β†’ (Open) created by RichardGaunt
  • πŸ‡¦πŸ‡ΊAustralia RichardGaunt Melbourne
  • Pipeline finished with Failed
    21 days ago
    Total: 173s
    #584240
  • πŸ‡¦πŸ‡ΊAustralia RichardGaunt Melbourne
  • Pipeline finished with Failed
    21 days ago
    #584242
  • First commit to issue fork.
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Ran the test-only feature here https://git.drupalcode.org/issue/drupal-3543555/-/jobs/6347958

    1) Drupal\KernelTests\Core\Datetime\Element\DatetimeFormElementTest::testNoErrorMetOnFormSubmit
    Failed asserting that an array is empty.
    /builds/issue/drupal-3543555/core/tests/Drupal/KernelTests/Core/Datetime/Element/DatetimeFormElementTest.php:113
    --
    1 test triggered 2 PHP warnings:
    1) /builds/issue/drupal-3543555/core/lib/Drupal/Core/Datetime/Element/Datetime.php:93
    Undefined array key "#date_date_format"
    Triggered by:
    * Drupal\KernelTests\Core\Datetime\Element\DatetimeFormElementTest::testDatetimeTimeOnlyValueCallback
      /builds/issue/drupal-3543555/core/tests/Drupal/KernelTests/Core/Datetime/Element/DatetimeFormElementTest.php:199
    2) /builds/issue/drupal-3543555/core/lib/Drupal/Core/Datetime/Element/Datetime.php:426
    Undefined array key "#date_date_format"
    Triggered by:
    * Drupal\KernelTests\Core\Datetime\Element\DatetimeFormElementTest::testDatetimeTimeOnlyValueCallback
      /builds/issue/drupal-3543555/core/tests/Drupal/KernelTests/Core/Datetime/Element/DatetimeFormElementTest.php:199
    FAILURES!
    Tests: 4, Assertions: 43, Failures: 1, Warnings: 2, PHPUnit Deprecations: 25.
    

    Did apply a small suggestion for the new @legacy-covers

    Change makes sense and summary lines up. Believe this one is good to go.

Production build 0.71.5 2024