Hi core developers,
please provide us with a solution for the datetimeRangeYears() like in webform;
Problem/Motivation
Deprecated function: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated in Drupal\Core\Datetime\Element\DateElementBase::datetimeRangeYears()
(line 49 of core/lib/Drupal/Core/Datetime/Element/DateElementBase.php).
(line 50 of core/lib/Drupal/Core/Datetime/Element/DateElementBase.php).
The deprecated function under php 8.3 is used in core as well as in webform (#3556560) .
protected static function datetimeRangeYears($string, $date = NULL) {...
Steps to reproduce
in a fresh installed drupal 10.5.4 with php 8.3.23
Proposed resolution
look into https://www.drupal.org/project/webform/issues/3556560#comment-16339057
[$min_year, $max_year] = explode(':', $string);
The problem with this is there is no $min_year at the start of my form; $min_year will be selected and is undefined at the beginning;
so the solution from ritarshi_chakraborty is to build the array in parts like this:
// Ensure we always have two elements, even if $string is malformed.
$parts = explode(':', $string ?? '');
$min_year = $parts[0] ?? '';
$max_year = $parts[1] ?? '';
thanks, Erich
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | Screenshot 2025-11-15 at 11.46.51 PM.png | 886.64 KB | harivansh |
Issue fork drupal-3556851
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
ritarshi_chakraborty commentedWorking on it.
Comment #4
ritarshi_chakraborty commentedThe remaining test failures don't appear to be related to the datetimeRangeYears() fix.
Comment #5
enorm commentedyes, you are wright; we will proof $parts[0] and $parts[1] for existent and not null - and we don't want to check the entire $string before explode();
pertect! thank you!
greetings, Erich
Comment #6
smustgrave commentedFixes need to land in 11.x first. But what's the scenario where these values are missing, may require test coverage.
Comment #8
ritarshi_chakraborty commentedHey @smustgrave, please let me know if you want any other changes.
Comment #9
smustgrave commentedStill need concrete steps as I’m not seeing this
Comment #10
harivansh commentedComment #11
harivansh commentedSteps to Reproduce
Create a custom form that includes a datetime field.
Set both default_value and date_year_range to an empty string.
Use the following example code:
Visit the form page in the browser.
Observe the URL — the malformed date_year_range value causes the issue.

Comment #12
harivansh commentedComment #13
smustgrave commentedOkay why would you do that though?
Comment #14
smustgrave commentedLet me elaborate more. Not knocking the code snippet at all. But is it a valid scenario?
I could write faulty code that could break things and that would be on me not the system to handle.
If a valid scenario a test case should be added
Comment #16
oily commentedFor testing I found this: core/tests/Drupal/KernelTests/Core/Datetime/Element/DatetimeFormElementTest.php. Lines 84 to 89 seem useful.
As I understand it the error in this issue occurs when one of the datetime fields is left blank? And the error occurs on form submission?
Comment #17
oily commentedWould be helpful to get 'steps to reproduce' in the IS.
Comment #18
harivansh commentedAgree with #14
I am not sure it counts as a valid scenario, but I can think of something like this. Honestly, I can't think of any other valid scenario.
Comment #20
anybodyComment #21
anybody