In Drupal 8, I have a content type and more than a few nodes that have a Paragraph field with unlimited number of values, and so I need to programmatically add a paragraph at an index to the existing values in the field. I know in the form editor widget I’m able to add a field value then drag the field to the order that I need, or change the weight then save the content.
I’m currently loading the node and appending the value like so
$paragraph_node = Paragraph::create($fields); $paragraph_node->save(); $node = Node::load($nid); $node->field_paragraph_repeater->appendItem( [ 'target_id' => $paragraph_node->id(), 'target_revision_id' => $paragraph_node->getRevisionId(), ] ); $node->save();
but this only adds the value at the end, how would I be able to add this with a weight?