I have two content types: FOO and BAR. These content types have a field called "field_paragraphs" of type "paragraph". And other fields of course.

Now in a situation my user can click a button (that call one of my function defined via hook_menu) and "copy" content of type FOO to a new content of type BAR, so I need to copy, NOT CLONE, field_paragraphs from FOO to BAR: I need to create a standalone copy of "field_paragraphs"; like two different insert.

I have searched on google but all talk about "clone", but in my case I need to copy so I can not use "clone" module.

How can I do this ?

M.

  • When does the user copy the content? When he clicks save? does he just manually copy it? You are talking about copying the content inside the paragraph bundle that's attached to the paragraph field, and then moving that content to another content type's paragraph bundle inside that other content types paragraph fieldcorrect? Paragraphs is all about reusing paragraph bundles. Duplicating a paragraph field (displays bundles) makes little to no sense to me.. – DMAC Sep 24 '15 at 16:09
  • Sorry @DMAC you are right: my user click a button that start one function defined via hook_menu() that create the copy. I don't understand the rest of your comment: I need to copy value of some fields from FOO to BAR, and one of this field is a paragraph field, not a paragraph bundle. Think to a magazine where I need to recreate a news, where the main content of this "news" is a paragraph field. Now I need to create a new version because in the new content type there are other fields that they are in context into BAR content type but without context into FOO content type. M. – ZioBudda Sep 24 '15 at 21:22
  • You are talking about the Paragraphs module correct? A paragraph field is literally nothing without paragraph bundles. you need to define paragraph bundles then associate them with the paragraph field when using the paragraphs module. This is why i'm so confused. are you talking about the Body field? or a text field you happened to call paragraph? – DMAC Sep 24 '15 at 21:45

EDIT: You can now find a better explanation with the code here.

I know this is old but maybe someone comes here looking for an answer like me, this is tested on Drupal 8.

I found this issue and after looking at last patch, I implemented :

$nodeDuplicate = $node->createDuplicate();

foreach($nodeDuplicate->field_paragraphs as $field){
    $field->entity = $field->entity->createDuplicate();
}

$nodeDuplicate->save();

And I got a duplicate node with duplicated paragraphs fields. If you look at the patch you will see how to add a button to Drupal etc.

Just a note field_paragraphs is the name of my field in the content type, change it with your own.

I didn't have any problem doing it this way, maybe someone has a better way but I did not find it, also at the time of writing this there is still no ported modules for cloning nodes in Drupal 8.

This is an example that could give you an idea how to do it.

   function copy_node_foo($node) {
      if ($node -> type == "FOO") {
          $node_bar = new stdClass();  // Create a new node object
          $node_bar->type = 'BAR';  // Content type
          $node_bar->language = LANGUAGE_NONE;  
          node_object_prepare($node_bar);  //Set some default values
          $node_bar->title = 'Your node title';
          node_bar->field_paragraphs = node->field_paragraphs;
          /* others properties .... */
          node_save(node_bar);
      }
    }
  • Hi, with your line "node_bar->field_paragraphs = node->field_paragraphs" drupal doesn't create a copy of node_bar's paragraphs, it link/use node_bar's paragraphs for "node" too (If I change paragraphs in "node" I see these modify in node_bar too). And this is wrong. And I don't want this: I want a new copy of field_paragraphs. – ZioBudda Sep 28 '15 at 13:01
  • I see, do this : load the entity with the id contained in node->field_paragraphs, create a new entity of type paragraph, copy the values of the original paragraph in the new paragraph nod the id, save the new paragraph entity and save the id in the node_bar->field_paragraphs. If you can load the entity referenced in node->field_paragraphs and send me the dpm result I can send you back the code. – Riccardo Ravaro Sep 28 '15 at 19:06
  • I have found this code: <?php $node = node_load(...); $paragraph = new ParagraphsItemEntity(array('field_name' => 'name_of_field_in_node', 'bundle' => 'name_of_paragraph_bundle')); $paragraph->is_new = TRUE; $paragraph->setHostEntity('node', $node); $paragraph->save(); ?> but in this code node is already present. In my case is new (never saved). How can I add a paragraphs to a new node ? – ZioBudda Oct 19 '15 at 12:43

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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