Hi There,
First off - great module :) Really checking all the boxes I have on some use cases.
My question is how can one go about creating a paragraphs item programmatically (I have about 15k entries to add, so would prefer to automate it)? So far I've been trying to do this like so:
$node = node_load(...);
$item = entity_create('paragraphs_item', array('bundle' => 'bundle_name', 'field_name' => 'field_name'));
$item->setHostEntity('node', $node);
$wrapper = entity_metadata_wrapper('paragraphs_item', $item);
$wrapper->field_name->set('value');
$wrapper->save(TRUE);
node_save($node);
But it doesn't create anything - the wrapper method seems to work just fine if I'm updating a previously created paragraph item (via paragraphs_item_load).
Any input would be appreciated, been banging my head against the wall for a few hours on this :)
Comments
Comment #1
erik.erskine commentedI managed to create a paragraph and add it to an existing node like this:
Comment #2
ivan zugec commentedHere's another way, copied from Field collection.
Comment #4
jkingsnorth commented+1 for #1
And you can define fields in the normal way:
$paragraph->field_MY_FIELD[LANGUAGE_NONE][0]['value'] = 'Foo';
Comment #5
ziobudda commentedHow can I add a paragraph to a new node never saved ?
Comment #6
Katie.Lacy commented#5 - ziobudda
You can create a paragraph to a new node by programmatically saving a node then using the new nodes object for this new node.
See here on how to create a node programmatically
https://www.drupal.org/node/1388922
Here's an example :
I hope this helps!
Comment #7
derMatze commentedAnyone knows how to do this in D8?
I think there is nothing like setHostEntity()
Update:
Just opened a new issue, so that this one can stay closed:
https://www.drupal.org/node/2707017
Comment #8
rliSaving fields to new paragraph or updating fields to existing paragraph didn't work for me using method in #4.
It would be great to have an example of how can I save the fields in the entity.
Thanks.
Comment #9
keesje commentedbeware of $entity->save() on existing paragraphs. By default it searches for the host entity en saves that too. That might be wat you want.
Caveat in my case was deployment of workbench_moderation. The save of that host entity does not play nice with moderation states, publishing nodes that where not meant to be.
Solution:
$entity->save(TRUE);
see ParagraphsItemEntity.inc
Comment #10
shivachevva commentedTo create a paragraph item programmatically refer: http://drupal.stackexchange.com/questions/188192/how-to-create-paragraphs-item-programmatically#answer-188299
It's working for me.
Comment #11
velocis commentedDrupal Paragraphs programmatically create image fields
Was working with images in Paragraphs, found the following method for adding images programmatically worked for me.
Works fine with S3FS module
Comment #12
delacosta456 commentedhi
Please is there no way to do this with rules .. like it was possible with field collection where you can loop in list and create an entity..
Comment #13
luis0076 commentedI created a paragraph bundle called workexperience with 3 fields (field_cv_from, field_cv_to, field_cv_position) and a content type with a filed paragraph called field_workexperience.
When I use this code
function mymodule_node_presave($node) {
$paragraph = entity_create('paragraphs_item', array('field_name' => 'field_workexperience', 'bundle' => 'workexperience'));
$paragraph->is_new = TRUE;
$paragraph->setHostEntity('node', $node);
$paragraph->save(true);
$paragraph->field_cv_from['und'][0]['value'] = '2016';
$paragraph->field_cv_to['und'][0]['value'] = '2018';
$paragraph->field_cv_position['und'][0]['value'] = 'Project Manager';
}
in the node there are two paragraphs one with data an another empty. Do you help me to understand?
Thx
Comment #14
sgdev commented@luis0076, you are saving the paragraph before adding the fields. put
$paragraph->save();at the end, not before setting the field values.Comment #15
colorfulcoder commentedTried the method in #11 but it doesn't work for me.
I have checked if I correctly upload my file, and it is correctly uploaded to the right location.
Also, if I check the paragraphs object afterwards (after saving and then loading it again) I can see that the fid is in the value field, and the revision field is also filled, so it seems to work there.
However, when I go to the node (user profile) that I attached the paragraph onto, I see a new paragraph with the disorder and date attached but no attached files.
This is driving me nuts, bonkers!
What the heck am I missing?!?
Comment #16
colorfulcoder commentedOk, I figured out that I got the names of my fields mixed up. Now I have fixed that, but I get the following watchdog error:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'field_before_picture_display' cannot be null: INSERT INTO {field_data_field_before_picture} (entity_type, entity_id, revision_id, bundle, delta, language, field_before_picture_fid, field_before_picture_display, field_before_picture_description) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8); Array ( [:db_insert_placeholder_0] => paragraphs_item [:db_insert_placeholder_1] => 57 [:db_insert_placeholder_2] => 59 [:db_insert_placeholder_3] => disorder_before_picture [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6] => 74 [:db_insert_placeholder_7] => [:db_insert_placeholder_8] => ) in field_sql_storage_field_storage_write() (line 514 of /Library/WebServer/Documents/cosmetiek/webroot/modules/field/modules/field_sql_storage/field_sql_storage.module).the name if my field is field_before_picture.
The table field_data_field_before_picture doesn't get a new entry when it should, because of this integrety constraint violation.
I'm now searching where this value (field_before_picture_display) should be set, but for the life of me I can't find it!
How did nobody else get this bug before?
Comment #17
colorfulcoder commentedI found the solution.
The solution posted in #11 is incomplete. One row of code is missing:
$file->display = '1';This is what it should be (in context).
Comment #18
raaesco commentedHow to update a paragraph fields that has been created before as a part of a node.
I don't know how to access de values in paragraph fields and even worse how to update them.
The paragraph already exists.
Any clues?
Thanks!
Comment #19
coffeduong commented@raaesco You'll need paragraph id, then `entity_load` and finish with `->save()`
For example:
Comment #20
drupalninja commentedI am trying to add a paragraph to a node in Rules using the code in #11. I works if I run it as a PHP input in a Basic page but wont work as a part of Rules iteration. Execute PHP Action
Any suggestions
Comment #21
bluegeek9 commentedUnfortunately, Drupal 7 is End of Life and no longer supported. We strongly encourage you to upgrade to a supported version of Drupal.