关键词: autowire: true
更新日志: https://www.drupal.org/node/3218156
在默认的服务器中如果想要传入参数都需要在服务中使用arguments. autowire可以自动加载服务
比如以下:
services:
Drupal\mymodule\Helper:
autowire: true
服务在初始化的时候会有参数EntityTypeManagerInterface.
<?php
namespace Drupal\mymodule;
use Drupal\Core\Entity\EntityTypeManagerInterface;
final class Helper {
public function __construct(
private readonly EntityTypeManagerInterface $entityTypeManager
) {}
public function getHello() {
return 'hello';
}
}
但是这并不会生效。自动装载装载的是当前服务中有EntityTypeManagerInterface这个服务才会自动装载。现在来改一个原来的services.yml
services:
Drupal\mymodule\Helper:
autowire: true
Drupal\Core\Entity\EntityTypeManagerInterface: '@entity_type.manager'