【问题标题】:How to update entity translations on hook_presave?如何更新 hook_presave 上的实体翻译?
【发布时间】:2020-01-10 07:29:02
【问题描述】:

保存时我需要更新节点上的字段值。我正在使用 hook_entity_presave 来获取值并更新节点保存上的字段。

但我想在该节点的所有语言翻译中更新该字段,但它只更新主要语言('en')节点。

$node = Node::load($cid);
if (empty($node)) {
  return FALSE;
}
$node->set('field_ship_name', $name);
$node = $node->save();

提前感谢您的帮助。

【问题讨论】:

    标签: drupal entity translation


    【解决方案1】:

    试试这个:

    $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();
    }
    

    【讨论】:

    • 我可以在没有循环的情况下一次更新所有翻译吗?
    猜你喜欢
    • 2022-07-11
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多