13.11.2025 | Michael Ebert

Warm your Drupal caches after a cache clear

my_module.services.yml
tags:
  - { name: cache_prewarmable }

MyPlugin.php
/**
  * Implements \Drupal\Core\PreWarm\PreWarmableInterface.
  */
public function preWarm(): void {
  $this->getDefinitions();
}

Issue:

We all know the problem that a Drupal site may be loading slowly after a cache clear.

Prerequisites:

  • Drush ≥ 13.5
  • Drupal ≥ 11.2

Solution:

There is a new prewarm API introduced in Drupal 11.2. This allows you to add a `cache_prewarmable` tag to your services and implement a preWarm method. If you are using Drush version 13.5 or newer, it will automatically call a `cache:warm` as last step when doing a `drush deploy`.

This will only warm cache cache items that are used to build most pages, plugins and registries, but not render caching or anywhere like that. 

Furter informations can be found here: 

Weitere DevBits

13.11.2025 | Peter Majmesku

Autowire services with the Autowire attribute in Drupal

Example.php
<?php

declare(strict_types=1);

namespace Drupal\my_module;

use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

class Example {

  public function __construct(
    #[Autowire(service: 'current_user')]
    private readonly AccountInterface $currentUser,
  ) {
  }

  public function getCurrentUser(): AccountInterface {
    return $this->currentUser;
  }

}

my_module.services.yml
services:
  my_module.example:
    class: Drupal\my_module\Service\Example
    autowire: true
  Drupal\my_module\Service\Example: '@my_module.example'

php
services
yml / yaml
30.07.2026 | Michael Ebert

Formatting Dates programmatically with the Drupal date formatter service

Example for a single datetime field

my_single_datetime_field.php
use Drupal\Core\Datetime\DrupalDateTime;

/** @var Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = \Drupal::service('date.formatter');

// Get DateTime object from stored date from a DateTimeFieldItemList field.
$my_date = $node->field_my_date->date;

// Get unix timestamp.
$my_date_timestamp = $my_date->getTimestamp();

// For formatting your DateTime object all other arguments are optional:
// See DateFormatterInterface for built-in options, or use machine name of a date format in config.
$type = 'medium';
// Custom PHP date format if $type="custom".
$format = '';

$formatted = $date_formatter->format($my_date_timestamp, $type, $format, $timezone = NULL, $langcode = NULL);

 

Example for a daterange field

my_datetime_range_field.php
use Drupal\Core\Datetime\DrupalDateTime;

/** @var Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = \Drupal::service('date.formatter');

// Get DateTime objects from stored date from a DateTimeFieldItemList field.
$start_date = $node->field_my_date->start_date;
$end_date = $node->field_my_date->end_date;

// For formatting your DateTime object all other arguments are optional:
// See DateFormatterInterface for built-in options, or use machine name of a date format in config.
$type = 'medium';
// Custom PHP date format if $type="custom".
$format = '';

$start_date_formatted = $date_formatter->format($start_date, $type, $format, $timezone = NULL, $langcode = NULL);
$end_date_formatted = $date_formatter->format($end_date, $type, $format, $timezone = NULL, $langcode = NULL);
php
time and date
services