1

Just a little background: I've got a set of paragraphs fields that I need to be able to translate, and given the limitations on translation with paragraphs, I've come up with the idea, that it may just be best to create a new node of a new content type (for holding the paragraphs fields) for each language of the main node. I would then use Inline Entity Form to bring those fields into my main node.

The problem I run into is that translation copies the value from the original language when I create a translation. Most of the time this is desirable, but in this case, it means that all languages get the same referenced node, when I specifically want them all to have their own new node.

Is there any way to suppress the value copy functionality for a specific field when translating?

1 Answer 1

4

Unset the field values in a hook_ENTITY_TYPE_translation_create:

use Drupal\node\NodeInterface;

/**
 * Implements hook_ENTITY_TYPE_translation_create().
 */
function mymodule_node_translation_create(Nodeinterface $translation) {
  if ($translation->getType() == 'product') {
    unset($translation->body);
  }
}
5
  • Sorry for the slow reply to this. I'm finding that $translation's structure is not as simple as indicated here, and am trying to figure out what should be unset to allow me to prevent inline entity form from getting a default node to work with.
    – UltraBob
    Mar 5, 2019 at 6:52
  • This code examples avoids copying the default value from the original language for the field body. You only need to replace this with your field name unset($translation->field_example); and adjust the content type and it should work.
    – 4uk4
    Mar 5, 2019 at 9:03
  • I guess I am not familiar enough with Drupal 8 OO coding. I tried inspecting $translation with xdebug, and there must be some getter and setter magic happening that I didn't account for. It appears to work very well. Thank you!
    – UltraBob
    Mar 6, 2019 at 1:04
  • it doesn't work for paragraphs. In hook Nodeinterface is changed with EntityInterface
    – Sharique
    Jan 18, 2022 at 7:34
  • @Sharique, the code example needs Nodeinterface because of getType(). If the argument is declared more generic you can't use a more specific method, at least not in an IDE.
    – 4uk4
    Jan 18, 2022 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.