drupal - 如何更新 hook_presave 上的实体翻译?
问题描述
保存时,我需要更新节点上的字段值。我正在使用 hook_entity_presave 来获取值并更新节点保存上的字段。
但我想在该节点的所有语言翻译中更新该字段,但它只更新主要语言('en')节点。
$node = Node::load($cid);
if (empty($node)) {
return FALSE;
}
$node->set('field_ship_name', $name);
$node = $node->save();
我在这里先向您的帮助表示感谢。
解决方案
尝试这个:
$node = Node::load($cid);
if (empty($node)) {
return FALSE;
}
$languages = $node->getTranslationLanguages($include_default = TRUE);
foreach($languages as $lang) {
$node_translation = \Drupal::service('entity.repository')->getTranslationFromContext($node, $lang);
$node_translation->set('field_ship_name', $name);
$node_translation = $node_translation->save();
}
推荐阅读
- svn - git pull-remote 分支找不到远程参考 [尝试了许多解决方案但失败]
- python - selenium.common.exceptions.TimeoutException 同时通过 expected_conditions 在元素上调用 .click()
- java - 利用 spring kafka 在 kafka 消费者中手动提交偏移的方法
- python - 训练后的神经网络步骤
- python - 按索引访问枚举中的项目
- php - 在两个单词之间执行两个空格
- python - 表单中字段之间的Django间距
- mysql - 从每个组中获取除最大值之外的所有项目 - mysql
- kubernetes - 与 ConfigMap 相比,Secrets 的价值是多少
- c++ - 平衡数组索引,同时从左右对数组求和