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.