2

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.

1

3 Answers 3

1

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();
0

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.

0

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

Your Answer

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

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