I have a site with multiple language. Assume I have a node (nid = 1) with origin languages is en and translate to es. And I clone this node with code:

$node = Node::load(1);
$nodeDuplicate = $node->createDuplicate();

And I have a new node (nid = 2), and the new node have two content translate en, es same the node id 1.

But I don't want clone all content translate. I only want to clone one language es or en. So how can do that?


A suggestion : use ->removeTranslation() to get rid of the languages you don't want to be cloned.

Example to get rid of 'es language' so that we clone only the english version :

$langcodetoremove = 'es';
$node = \Drupal\node\Entity\Node::load(1);
$nodeDuplicate = $node->createDuplicate();

hope this helps

  • 2
    Thank you. I tried but I have problem when I want to clone language is not origin. In my case, I can not use removeTranslation for en because this is origin language. – Jonh Nov 21 '18 at 16:06

You can create a new node with the field values retrieved from the (translated) node:

$node = Node::load(1);
$values = $node->getTranslation('es')->toArray();
// remove nid, uuid, vid and revision details from $values
$node_duplicate = Node::create($values);
  • Yes, I tried this way. I have to set $values['language'] to langue I want to clone. But I have problem with field paragraph. Because your way field type paragraph can't not duplicate. – Jonh Nov 21 '18 at 16:33
  • Yes, paragraphs can only be referenced by a single node. You would have to clone the paragraphs as well. – 4k4 Nov 21 '18 at 16:38
  • Can you provide me with example? I had tried follow this snippet. But don't know map with your code to working. – Jonh Nov 21 '18 at 16:45
  • 2
    The snippet is for a fixed field, a universal solution would need to iterate through fields and replace all paragraphs. Important is that you replace the paragraph id and the revision id, see drupal.stackexchange.com/questions/236544/… – 4k4 Nov 21 '18 at 16:54

Try this:

$node = Node::load(1);
$nodeDuplicate = $node->createDuplicate();

$langcode = 'fr';  
  • Help me clone content is not origin language. Thank you. – Jonh Nov 21 '18 at 16:13

You can try to use this code for copy translation with all field types and all node types:

<?php use Drupal\node\Entity\Node; $exclude = [ 'nid', 'vid', 'revision_timestamp', 'revision_uid', 'revision_log', 'revision_default', 'revision_translation_affected', 'content_translation_source', 'content_translation_outdated', 'created', 'changed', 'default_langcode', 'uuid' ]; $node = Node::load(NODE_ID); if (!empty($node->hasTranslation(LANGCODE))) { $node = $node->getTranslation(LANGCODE); } $node_type = $node->getType(); $fields = $node->getFieldDefinitions('node', $node_type); $copyied_fields = []; foreach ($fields as $field_name => $field_definition) { $field_name = $field_definition->getName(); if (!in_array($field_name, $exclude)) { $field_value = $node->$field_name->value; if (empty($field_value)) { $field_value = $node->$field_name->getValue(); } $copyied_fields[$field_name] = $field_value; // Add body format. if ($field_name == 'body') { $copyied_fields['body'] = array( 'value' => $node->body->value, 'format' => 'full_html', ); } } } $new_node = Node::create($copyied_fields); $new_node->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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