Score:2

Append new paragraph item to existing node

rs flag

I want to append a new paragraph item to a existing node, which has previously a paragraph item. I just want to add another paragraph item with the previous one. But my script only updates the previous added paragraph item with the latest data, not appending with the previous one.

$nid = \Drupal::entityQuery('node')->condition('title', $product_name)->execute();
        $nid = array_values($nid)[0];
        if(empty($nid)) {
            // this node doesn't exit

            $paragraph = Paragraph::create([
                'type' => 'product_details',
                'field_title' => $product_details_title,
                'field_description_details' => $product_details_description,
            ]);
            $paragraph->save();

            $node = Node::create([
                'type'        => 'products',
                'title'       => $product_name,
                'field_composition_api' => $product_api,
                'field_product_geography_area'  => [
                    ['target_id' => 26]
                ],
                'field_product_details'  => array(
                  array(
                    'target_id' => $paragraph->id(),
                    'target_revision_id' => $paragraph->getRevisionId(),
                  ),
                  
                ),
                'created' => time(),
            ]);
            $node->save();
        }
        else {
            // this node already exits

            $paragraph = Paragraph::create([
                'type' => 'product_details',
                'field_title' => $product_details_title,
                'field_description_details' => $product_details_description,
            ]);
            $paragraph->save();

            

            $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);

            $node->field_product_details = array(
                array(
                    'target_id' => $paragraph->id(),
                    'target_revision_id' => $paragraph->getRevisionId(),
                )
            );
            $node->save();
        }

I have already checked this . But no luck!

Please help me out of this. I just want to append new paragraph item to a existing node without affecting previously added paragraph item to the same node.

4uk4 avatar
cn flag
See https://drupal.stackexchange.com/questions/292832/create-nested-paragraphs-programmatically
Score:1
rs flag

After spending hours on this, I came to this solution. Currently the script retrieves existing paragraph items and adding them again together with the new paragraph item.

            $product_details = array();
            $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);

            foreach ($node->field_product_details as $item) {
                $existing_product_details = $item->entity;
                $product_details[] = [
                    'target_id' => $existing_product_details->id(),
                    'target_revision_id' => $existing_product_details->getRevisionId(),
                ];
            }

            $paragraph = Paragraph::create([
                'type' => 'product_details',
                'field_title' => $product_details_title,
                'field_description_details' => $product_details_description,
            ]);
            $paragraph->save();

            $product_details[] = [
                'target_id' => $paragraph->id(),
                'target_revision_id' => $paragraph->getRevisionId(),
            ];

            $node->set('field_product_details',$product_details);
            $node->save();
Score:0
cn flag

You don't need to retrieve the existing paragraphs. You can treat the field itself as array and add items to it:

        $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);

        $paragraph = Paragraph::create([
            'type' => 'product_details',
            'field_title' => $product_details_title,
            'field_description_details' => $product_details_description,
        ]);

        $node->field_product_details[] = $paragraph;
        $node->save();

Also, you can add unsaved paragraphs to the field. Then you don't need to save the paragraph and add the target/revision IDs.

Score:0
cn flag

You can try the code below

 paragraph = Paragraph::create([
                        'type' => 'paragraph_type',
                        'field_name' => $name,
                    ]);
                   
    $node->field_paragraph->appendItem($paragraph);
    $paragraph->save();
    $node->save();

Ref to: https://www.drupal.org/project/paragraphs/issues/2707017#comment-14917158

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.