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(DrupalCoreEntityEntityInterface $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(DrupalCoreEntityEntityInterface $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:
DrupalCoreEntityEntityStorageException: 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 DrupalCoreEntitySqlSqlContentEntityStorage->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.
Sponsored by SupremePR