3

I am trying to attach a paragraph, to a node. I am currently trying to do it in node_update hook.

The value for paragraph will come a custom field. Currently am trying to achieve this with hard-coded value. I tried two methods, but both are not working.

Method 1

function my_module_node_update(Drupal\Core\Entity\EntityInterface $entity) {

  $paragraph = Paragraph::create([
  'type' => 'attendance',
  'field_stakeholder' => array(
      "value"  =>  '. Roshni, UPS Kharwna from Dunda, Uttarkashi (UTK_130000377) (49645)',
    ),
  ]);

  $paragraph->save(); 

//this is working
 dpm($paragraph->id());

 $current[] = array(
      'target_id' => $paragraph->id(),
      'target_revision_id' => $paragraph->getRevisionId(),
    );



   $entity->set('field_attendance', $current);  
   $entity->save();

}

Method 2

function my_module_node_update(Drupal\Core\Entity\EntityInterface $entity) {

  $paragraph = Paragraph::create([
  'type' => 'attendance',
  'field_stakeholder' => array(
      "value"  =>  '. Roshni, UPS Kharwna from Dunda, Uttarkashi (UTK_130000377) (49645)',
    ),
  ]);

  $paragraph->save(); 

//this is working
 dpm($paragraph->id());

   $entity->field_attendance->appendItem($paragraph);
   $entity->save();

}

However, I am getting an error:

Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '213436' for key 'PRIMARY': INSERT INTO {node_revision} (nid, vid, langcode, revision_timestamp, revision_uid, revision_log) 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); Array ( [:db_insert_placeholder_0] => 41374 [:db_insert_placeholder_1] => 213436 [:db_insert_placeholder_2] => en [:db_insert_placeholder_3] => 1535117690 [:db_insert_placeholder_4] => 1 [:db_insert_placeholder_5] => ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 777 of /home/rupali/fa-rupali/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

I feel like the error is coming as the hook is called twice. The code is working till paragraph creation. I am able to verify it.

  • 1
    Did you try remove $entity->save(); in method 1? – MrD Aug 24 '18 at 15:33
  • 1
    Why do you need to do this in a update hook which runs after already saving the host entity? If possible in any way, you should do it in hook_node_presave() so you don't have to save the etntiy again – Berdir Aug 24 '18 at 20:09
0

I used this code in hooky_entity_presave

       $paragraph = Paragraph::create([
              'type' => 'attendance',
              'field_stakeholder' => array(
               "value"  =>  '". Seema Bist, PS Manjkot from Dunda, Uttarkashi (UTK_130000379) (49647)"',
               //"value"  =>  '49645'
            ),
          ]);
              $paragraph->save();


            $entity->field_attendance->appendItem($paragraph);

Paragragh array is display (target id), but didn't display value of target id in input value. in Means data added in node__field_at table, but not target id with node id is doesnt add in paragraph__field_st (st is entity reference field in paragraph type.

How to save data in paragraph entity reference field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.