Calling date formatter ->format() with empty $type causes many extra database queries

Created on 29 October 2024, about 2 months ago

Problem/Motivation

As mentioned in πŸ› Drupal\Core\Datetime::dateFormat() does not properly cache results Active , there is a bug in core that is being triggered by SmartDateTrait. The result is tons of extra queries to the database looking for a non-existent date format, like this:

SELECT "cid", "data", "created", "expire", "serialized", "tags", "checksum" FROM "cache_config" WHERE "cid" IN ( 'core.date_format.' ) ORDER BY "cid"

Proposed resolution

The reason for this is the $type (second) parameter to \Drupal::service('date.formatter')->format(), as used in SmartDateTrait. For instance, this line:

$datetime = \Drupal::service('date.formatter')->format($times[$part], '', $format);

should be:

$datetime = \Drupal::service('date.formatter')->format($times[$part], 'custom', $format);

So, even if the parent issue does get fixed, this patch avoids even the first attempt to load the incorrect format.

πŸ› Bug report
Status

Active

Version

4.2

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States gribnif

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

Comments & Activities

Production build 0.71.5 2024