5

以下是我的示例解决方案,可能会对您有所帮助。

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

//创建字段并附加在节点中可以在这里找到。 https://www.drupal.org/project/paragraphs/issues/2707017

Thanks
4

3 回答 3

11

代码中的注释应该解释一切:

<?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();
于 2018-01-23T10:34:04.083 回答
3

以下是更新现有段落项目的示例(使用您需要的值填写 $nid 和 $paragraph_field):

$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();
  }
}
于 2019-04-11T21:45:26.393 回答
0

我花了几个小时让它工作,问题是在我的多值节点字段中添加一个段落时,现有值丢失了。我终于找到了这个可行的解决方案(检索现有段落并将它们与新段落一起重新添加),但也许不是最优雅的方式......

    $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();
于 2021-10-26T12:35:27.890 回答