Problem/Motivation
If we clone an entity that has the URI field and attempt to save, we get a fatal error.
ContentEntityBase::createDuplicate() copies all field values including the RDF URI field item list. When the duplicate is saved, assignUri() sees a non-empty $this->list and skips generation, then tries to INSERT the original URI for the new entity ID, causing a mysql integrity constraint violation.
Steps to reproduce
On an entity type that has rdf_sync uri_plugin active, try to duplicate it and save.
$term = Term::create(['vid' => 'category', 'name' => $this->randomString()]);
$term->save();
$duplicate = $term->createDuplicate();
$duplicate->save(); // throws integrity constraint violation
Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'http://localhost/taxonomy-term/category/1e82d2b0-e18e-4817-a3...' for key 'uri': INSERT INTO "test73327032rdf_sync_uri" ("entity_type", "entity_id", "uri", "bundle") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3); Array
(
[:db_insert_placeholder_0] => taxonomy_term
[:db_insert_placeholder_1] => 2
[:db_insert_placeholder_2] => http://localhost/taxonomy-term/category/1e82d2b0-e18e-4817-a3c0-bf0070d5...
[:db_insert_placeholder_3] => category
)
Proposed resolution
- Implement __clone() on RdfSyncUriFieldItemList to flush $this->list and reset $this->valueComputed when the field item list is cloned.
Tried it, doesn't work (wrong layer, fields is not reset on duplicated entity).
- Implement hook_entity_duplicate in rdf_sync.module to clear the URI field on the duplicate entity before it is saved.
This hook was only added since core 11.2.0 https://www.drupal.org/node/3268812
On earlier core versions, update RdfSyncUriFieldItemList::assignUri to clear the value if the URI already exists in DB.
Remaining tasks
?
User interface changes
None
API changes
None
Data model changes
None
Issue fork rdf_sync-3582431
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 #3
herved commentedComment #4
herved commentedComment #5
herved commentedComment #6
herved commentedComment #7
herved commentedComment #8
herved commentedComment #9
herved commentedComment #10
alorencComment #11
alorencComment #12
alorencComment #14
claudiu.cristeaMerged thank you