[Solved] Creating paragraph programmatically, referring to an entity and including nested paragraph

scaffolding Asks: Creating paragraph programmatically, referring to an entity and including nested paragraph
I’m creating a paragraph in a custom module.

The goal is to automatically create this paragraph for every node of a particular type. It’s a paragraph that leads to a donation form. I expect there will be hundreds of these pages on my site.

I’m using hook_node_presave(); to create the paragraph. Much of this is working as expected for the simpler types of data (strings, image references).

However, I’m having trouble referring to two fields in the paragraph, of the following types:

  • field_donation_form, which is an entity reference field type
  • field_donation_items, which is an entity reference revisions field type – this is a field that needs to be added three times (there are three options for field_donation_items)

I’ve looked in a handful of places for the documentation on how to refer to entities but I’m just not seeing them. If you are able to point me towards the right area, that would be super helpful.



use DrupalmediaEntityMedia;
use DrupalparagraphsEntityParagraph;

function event_add_default_content_node_presave(DrupalnodeNodeInterface $entity) {
  if ($entity->bundle() == 'event' && $entity->get('field_event_type')->target_id== '30') {

      //declaring $donation_options array for nested paragraphs

      $donation_options = [];
      //creating three paragraphs to be saved in $donation_options

      $donation_options[0] = Paragraph::create([
        'type' => 'quiz_question',
        'field_text_plain_long' => 'text 1',
        'donation_amount' => 10,
      $donation_options[1] = Paragraph::create([
        'type' => 'quiz_question',
        'field_text_plain_long' => 'text 2',
        'donation_amount' => 20,
      $donation_options[2] = Paragraph::create([
        'type' => 'quiz_question',
        'field_text_plain_long' => 'text 3',
        'donation_amount' => 30,

      //Creating the 'slice_donate' paragraph
      $paragraph = Paragraph::create(['type' => 'slice_donate']);
      $paragraph->set('field_title', 'Donate'); 
      $paragraph->set('field_media', 52); //'52'= image library id
      $paragraph->set('field_quote', 'Donation slice text'); 
      $paragraph->set('field_donation_form', $entity->get('donation_form')->target_id== '1');
//which is accessible here: admin/structure/donation_form/1
//Drupal's error message related to this field is: 
//DrupalCoreEntityEntityStorageException: Field donation_form is unknown.

      $paragraph->set('field_monthly_donation_items', $donation_options);
//This field refers to another paragraph (item_donation_amount), 
//and expects three items to be added, each with two 
//text fields (donation amount, caption).


Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.