13.11.2025 | Peter Majmesku
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'