By godotislate on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
11.4.x
Introduced in version:
11.4.0
Issue links:
Description:
Container parameters can be passed as autowired parameters to the constructors of classes using AutowireTrait and AutowiredInstanceTrait. These include classes implementing ContainerInjectionInterface, such as controllers and forms, and classes implementing ContainerFactoryPluginInterface, such as plugins. Previously, AutowireTrait and AutowiredInstanceTrait only supported autowiring services.
Controller example:
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
class SampleController extends ControllerBase {
public function __construct(
#[Autowire(param: 'container.namespaces')]
protected readonly array $namespaces,
) {}
}
Alternative:
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
class SampleController extends ControllerBase {
public function __construct(
#[Autowire('%container.namespaces%')]
protected readonly array $namespaces,
) {}
}
Impacts:
Module developers