【发布时间】:2018-05-04 10:23:43
【问题描述】:
我目前正在使用 Drupal 8 API,我需要将我的 menu_link_content 类型存储在数据库中。为此,我对 entity_presave 和 entity_predelete 有一个钩子。因为我不想放松数据库和 Drupal 之间的同步,所以当我的代码出错时,我会阻止 Drupal 保存实体。这就是为什么我迷上了presave而不是保存。反正...
function modulename_entity_presave(\Drupal\Core\Entity\EntityInterface $entity) : void
{
$class = get_class($entity);
if ($entity->isNew()) {
switch ($class) {
case 'Drupal\menu_link_content\Entity\MenuLinkContent':
//dispatch event menu link content insert
//listener on that event that insert the entity in DB
break;
}
}
if (isset($entity->original)) {
switch ($class) {
case 'Drupal\menu_link_content\Entity\MenuLinkContent':
//dispatch event menu link content update <- this one triggered
//listener on that event that update the entity in DB
break;
}
}
}
我的问题是,当我删除我的 menu_link_content(或任何类型)的翻译时,会触发“presave”事件并且实体语言代码从翻译的语言代码(例如“fr”)更改为默认语言代码(例如“en”),然后触发 entity_translation_delete 钩子。
function modulename_entity_translation_delete(\Drupal\Core\Entity\EntityInterface $translation)
{
$class = get_class($translation);
switch ($class) {
case 'Drupal\menu_link_content\Entity\MenuLinkContent':
// get the langcode && the uuid of the $translation entity
// deleting the row from database using langcode && uuid as key
break;
}
}
因此,当我使用翻译后的语言代码(例如“fr”)查询我的数据库时,我什么也得不到……因为该行已经更新。所以我不能从我的数据库中删除它
我想了一种方法来捕捉实体的语言代码是否在更新过程中发生更改以触发删除功能(因为我认为实体的语言代码无法更改,我唯一看到的是当我删除了翻译)。但我认为这不是最好的方法。是否有某种hook_entity_translation_predelete?
Drupal hook list 没有显示任何内容
如果我不更新数据库中的行(通过注释更新行),来自 modulename_entity_translation_delete 的代码运行良好
感谢您的帮助!
【问题讨论】: