【发布时间】:2021-06-01 17:10:10
【问题描述】:
我正在尝试在 Drupal 8 (8.9.11) 中创建一个模块,该模块使用函数 hook_entity_presave 以编程方式更新节点/实体。我已经尝试了https://drupal.stackexchange.com/questions/223346/hook-entity-presave-doesnt-work 的答案,我能够在我的代码中添加这些答案。 这是我的 routing.yml (sno.routing.yml):
sno.content:
path: /node/add/issuances
defaults:
_controller: Drupal\sno\Controller\SnoController::sno_entity_presave
requirements:
_permission: 'access content'
这是我的控制器(src/Controller/SnoController.php):
namespace Drupal\sno\Controller;
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\NodeInterface;
class SnoController {
public function sno_entity_presave(\Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->getEntityType()->id() == 'issuances') {
$entity->set('field_s', ', s. ');
//CAUTION : Do not save here, because it's automatic.
}
}
}
当我开始添加内容类型发布 (/node/add/issues) 的内容时,我收到以下错误:
The website encountered an unexpected error. Please try again later.
RuntimeException: Controller "Drupal\sno\Controller\SnoController::sno_entity_presave()" requires that you provide a value for the "$entity" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one. in Symfony\Component\HttpKernel\Controller\ArgumentResolver->getArguments() (line 78 of /var/www/senate-library/vendor/symfony/http-kernel/Controller/ArgumentResolver.php).
非常感谢!
【问题讨论】: