Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
In the D7 version a new paragraph could programmatically be created by using something like this:
$node = node_load(...);
$entity = entity_create('paragraphs_item', array('field_name' => 'name_of_field_in_node', 'bundle' => 'name_of_paragraph_bundle'));
$entity->setHostEntity('node', $node);
$entity->save();
Mentioned here: https://www.drupal.org/node/2395131
Basically this still works (use "type", instead of "bundle"), but there is no setHostEntity(). Anyone knows how to link the paragraph with the parent/host entity?
Issue fork paragraphs-2707017
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
killua99 CreditAttribution: killua99 as a volunteer commentedWhat I know is, now paragraphs on Drupal 8 use entity_reference_revisions maybe you want to take a look in there.
Check how entity_reference_revisions link the paragraphs with the entity who is using it. Remember not only Content Type could use paragraphs.
Comment #3
derMatze CreditAttribution: derMatze commentedMeanwhile I found a solution:
First create the paragraph, than add the paragraph to the corresponding node/entity.
Comment #4
ArlaThat indeed looks like the standard way to do it.
You can also use appendItem and/or pass the entire Paragraph entity as the value:
Comment #6
vijaycs85Thanks @derMatze for raising this issue and the solution... landing here from google search :)
Comment #7
bkeller CreditAttribution: bkeller commentedLanded here from Google as well, and had to modify a bit to get it to work for my purposes.
I'm using paragraphs with just a text field and an optional image, and a node can have multiple paragraphs attached.
Maybe this will help someone else...
Comment #8
nitin.k CreditAttribution: nitin.k as a volunteer and commentedPlease use below namespace..
Comment #9
ben.hamelin CreditAttribution: ben.hamelin commented#7 worked for me
Drupal core 8.3.7
Paragraphs 8.x-1.0
Comment #10
couloir007 CreditAttribution: couloir007 commented#7 works as well as appendItem except for when I open the node and save it I get this:
8.4.2
Comment #11
couloir007 CreditAttribution: couloir007 commentedScratch that. I'm feeling like an idiot. I was accidentally adding the wrong paragraph type. It added, but I guess it didn't like it.
Comment #12
miststudent2011 CreditAttribution: miststudent2011 as a volunteer commentedHere is a better example :
Source :
https://stackoverflow.com/a/48399547/
Comment #13
sibopa CreditAttribution: sibopa commentedThe #12 here have limitation in case you have an unlimited number of items to add. #7 will work better. Thanks!
Comment #14
sadikyalcin CreditAttribution: sadikyalcin commentedHere is a more efficient way for unlimited and nested paragraphs. Note: you do not need to save the paragraphs when creating a new node; the relationship will be assigned automatically.
Credit to '4k4' on Stack: Source
Comment #15
ahaomar CreditAttribution: ahaomar commentedI want to create Paragraph types can you some one guide me how can i create via programming
Comment #16
dorficus CreditAttribution: dorficus as a volunteer commented@ahaomar the issue queue probably isn't the best place to be asking that. There is the forum section of Drupal.org and/or Drupal Answers. It's more likely that someone will see your question there and have time to answer rather than in the issue queue where the focus is on specific issues within projects.
With that being said, I suggest looking at the code for Paragraph.php and see how the base paragraph is created then maybe looking at a module that extends paragraphs like Bootstrap Paragraphs and see how those are built so that you can get a starting point.
Comment #18
xandermar CreditAttribution: xandermar commentedWorkable in Drupal 9
Comment #19
mxr10_ CreditAttribution: mxr10_ commentedHi team,
I'm having some issues trying to save some field using paragraphs,
Where i run my code happen this:
The first field has value and second is empty
and the next block the first field is empty and second has value
screenshoot
This is my code:
I don't know what to do to fill both field
Could you please help me?
Regards
Mario
Comment #20
ressa CreditAttribution: ressa at Ardea commentedI 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 here, but finally figured it out so here it is:
Comment #21
oakulm CreditAttribution: oakulm as a volunteer and at Drontti Oy for Drontti Oy commentedSorry 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:
Same goes with Node.