8

Below is my solution with an example, that may help you.

Update existing field
$target_id = 62;
$paragraph = Paragraph::load($target_id);
$typeform_field = $paragraph->field_archtics_field->value;
$archtics_field = $paragraph->field_archtics_label->value;
$paragraph->set('field_fieldname1', 'TEST1');          
$paragraph->set('field_fieldname2', 'TEST2');          
$paragraph->save();

//Create field and attached in node can be find here. https://www.drupal.org/project/paragraphs/issues/2707017

Thanks
Debraj nayak
  • 81
  • 1
  • 1
  • 2

3 Answers3

13

Comments in the code should explain everything:

<?php

use Drupal\paragraphs\Entity\Paragraph;

// Create single new paragraph
$paragraph = Paragraph::create([
  'type' => 'paragraph_machine_name',
  'field_machine_name' => 'Field value',
]);
$paragraph->save();

// Create multiple new paragraphs
$multiParagraph1 = Paragraph::create([
  'type' => 'paragraph_machine_name',
  'field_machine_name' => 'Field value',
]);
$multiParagraph2 = Paragraph::create([
  'type' => 'paragraph_machine_name',
  'field_machine_name' => 'Field value',
]);
$multiParagraph1->save();
$multiParagraph2->save();


// Save paragraph to node it belongs to
$newCompanyNode = Node::create([
  'type' => 'node_machine_name',
  'title' => 'new_node_title',
  // Insert a single paragraph
  'node_field_paragraph_machine_name' => array(
    'target_id' => $paragraph->id(),
    'target_revision_id' => $paragraph->getRevisionId(),
  ),
  // Insert multiple paragraphs into the same reference field
  'node_paragraph_field_machine_name' => array(
    array(
      'target_id' => $multiParagraph1->id(),
      'target_revision_id' => $multiParagraph1->getRevisionId(),
    ),
    array(
      'target_id' => $multiParagraph2->id(),
      'target_revision_id' => $multiParagraph2->getRevisionId(),
    ),
  ),
]);

// Makes sure this creates a new node
$newCompanyNode->enforceIsNew();
// Saves the node
// Can also be used without enforceIsNew() which will update the node if a $newCompanyNode->id() already exists
$newCompanyNode->save();
6

Here is an example for updating an existing paragraph item (fill in $nid and $paragraph_field with the values that you need):

$entity = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$result = $entity->get($paragraph_field)->referencedEntities();
if (!empty($result)) {
  foreach ($result as $paragraph) {
    $paragraph->set('field_fieldname1', 'some value');
    $paragraph->save();
  }
}
Barrett
  • 81
  • 1
  • 3
  • Because the paragraph is saved but the node is not saved how to create a new node revision? This will also not fire hook_node_update. – gagarine Jun 10 '22 at 14:03
  • Paragraphs are a different entity. `hook_node_update` passes a reference to the node, but it would not have changed at all, because it's referencing the same paragraph entity. You would need to use paragraph revisions, as shown in the other answer, `'target_revision_id' => $multiParagraph2->getRevisionId(),`. – mbomb007 Aug 15 '23 at 14:52
0

I spent hours make it work, the problem was when adding a paragraph to my multivalues node field, the existing values were lost. I finally came to this working solution (retrieving existing paragraphs and adding them back together with the new paragraph), maybe not the most elegant way though...

    $currentUser = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
    $product = Node::load($form_state->getValue('product_id'));
    
    //create new ordered_item paragraph
    $order = Paragraph::create(['type' =>'item_ordered']);
    $order->field_item_ordered_number = $form_state->getValue('quantity');
    $order->field_item_ordered_product = $product->id();
    $order->uid = $currentUser->id();
    $order->save();
    
    $order_items[] = [
    'target_id' => $order->id(),
    'target_revision_id' => $order->getRevisionId(),
    ];
    
    //check if user has already a cart
    $cart=\Drupal::service('dpnews_home.get_user_infos')->getUserCart($currentUser);
    
    foreach ($cart->field_ordered_item as $item) {
        $existing_order=$item->entity;
        $order_items[] = [
        'target_id' => $existing_order->id(),
        'target_revision_id' => $existing_order->getRevisionId(),
        ];
    }
    //if not create new cart for user
    if(!isset($cart)){
        $cart = Node::create(['type' => 'panier']);
        $cart->uid = $currentUser->id();
        $cart->title= 'Panier en cours - utilisateur '.$currentUser->id();
        $cart->save();
    }
    
    //add ordered item to cart
    $cart->set('field_ordered_item',$order_items);
    $cart->save();
duck
  • 29
  • 8