services
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'
13.11.2025 | Peter Majmesku
Autowire services with the Autowire attribute in Drupal
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);
30.07.2026 | Michael Ebert
Formatting Dates programmatically with the Drupal date formatter service
DomainRouteProvider.php
<?php
namespace Drupal\poormans_domain\Routing;
use Drupal\Core\Routing\RouteProvider;
use Symfony\Component\HttpFoundation\Request;
/**
* Custom router.route_provider service to make it domain context sensitive.
*/
class DomainRouteProvider extends RouteProvider {
/**
* {@inheritdoc}
*/
protected function getRouteCollectionCacheId(Request $request) {
// Add domain sensitive cache information to the cid.
$this->addExtraCacheKeyPart('poormans_domain', $request->getHost());
return parent::getRouteCollectionCacheId($request);
}
}
poormans_domain.services.yml
services:
poormans_domain.route_provider:
class: Drupal\poormans_domain\Routing\DomainRouteProvider
decorates: router.route_provider
decoration_priority: 10
arguments: ['@database', '@state', '@path.current', '@cache.data', '@path_processor_manager', '@cache_tags.invalidator', 'router', '@language_manager']
27.01.2023