Drupal——钩子翻译——删除
Drupal - hook translation - deletion
我目前正在使用 Drupal 8 API,我需要将我的 menu_link_content 类型。为此,我在 entity_presave & entity_predelete 上设置了一个钩子。因为我不想失去数据库和 Drupal 之间的同步,所以当我的代码出现错误时,我会停止 Drupal 保存实体。这就是为什么我迷上了 presave 而不是 save。无论如何...
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')查询我的数据库时,我什么也得不到...因为该行已经更新。所以我无法将其从我的数据库中删除
我想到了一种方法,如果实体的langcode在更新过程中被更改以触发remove函数(因为我认为实体的lancode不能更改,我唯一一次看到是我删除翻译的时候)。但我不认为这是最好的方法.. 有某种 hook_entity_translation_predelete 吗?
Drupal hook list 没有显示任何内容
如果我不更新数据库中的行(通过注释更新行),来自 modulename_entity_translation_delete 的代码运行良好
感谢您的帮助!
如果我删除了实体的翻译(即使我之前捕获了更新),我发现捕获的唯一方法是检查:
- 我更新数据的语言代码是默认语言
- 我有一个以前的注册数据(所以这个实体不是新的)
- 之前注册的语言代码不再存在于可用的翻译中
对我来说,代码如下所示:
if (\Drupal::languageManager()->getDefaultLanguage()->getId() === $data['langcode']->getValue()
&& $data['langcode']->getOriginalData() !== null
&& $data['langcode']->getAvailableTranslations() !== $data['langcode']->getOriginalData()) {
// do what you want of the matching data in DB
}
这里的 $data 是一个存储(自定义)字段对象的数组。
我目前正在使用 Drupal 8 API,我需要将我的 menu_link_content 类型。为此,我在 entity_presave & entity_predelete 上设置了一个钩子。因为我不想失去数据库和 Drupal 之间的同步,所以当我的代码出现错误时,我会停止 Drupal 保存实体。这就是为什么我迷上了 presave 而不是 save。无论如何...
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')查询我的数据库时,我什么也得不到...因为该行已经更新。所以我无法将其从我的数据库中删除
我想到了一种方法,如果实体的langcode在更新过程中被更改以触发remove函数(因为我认为实体的lancode不能更改,我唯一一次看到是我删除翻译的时候)。但我不认为这是最好的方法.. 有某种 hook_entity_translation_predelete 吗?
Drupal hook list 没有显示任何内容
如果我不更新数据库中的行(通过注释更新行),来自 modulename_entity_translation_delete 的代码运行良好
感谢您的帮助!
如果我删除了实体的翻译(即使我之前捕获了更新),我发现捕获的唯一方法是检查:
- 我更新数据的语言代码是默认语言
- 我有一个以前的注册数据(所以这个实体不是新的)
- 之前注册的语言代码不再存在于可用的翻译中
对我来说,代码如下所示:
if (\Drupal::languageManager()->getDefaultLanguage()->getId() === $data['langcode']->getValue()
&& $data['langcode']->getOriginalData() !== null
&& $data['langcode']->getAvailableTranslations() !== $data['langcode']->getOriginalData()) {
// do what you want of the matching data in DB
}
这里的 $data 是一个存储(自定义)字段对象的数组。