- 🇩🇰Denmark ressa Copenhagen
I was looking for a way to create a new node, and populate a multi-value Paragraphs field, and found this issue. I didn't find the solution, but finally figured it out so here it is:
$cars = [ array("Volvo",22), array("BMW",15), array("Land Rover",17), ]; // print_r($cars); foreach ($cars as $car) { $paragraph = Paragraph::create([ 'type' => 'para_item', 'field_para_item_name' => $car[0], 'field_para_item_number' => $car[1], ]); // Add the paragraphs to the parent entity's paragraphs field. $node->field_para->appendItem($paragraph); $paragraph->save(); } $node->save();
- 🇫🇮Finland oakulm
Sorry to raise old issues but this keeps popping in Google. You should not include references to concrete classes ie. Paragraph but should use service injection:
\Drupal::entityTypeManager()->getStorage('paragraph')->create([ 'type' => 'param_item', ])
Same goes with Node.