Updated: Comment #N
Problem/Motivation
There is an unwritten assumption that EntityStorageControllerInterface::save() will provide the original entity in $entity->original if the entity is not new (being updated).
This isn't documented that storage controllers should do this, but all of our implementations do it. But yet they do it very differently, because only ConfigEntity has a dedicated method to help with this
ConfigEntityInterface has getOriginalId() and setOriginalId() because it supports renames, but the ability to ask the entity for its original ID is not necessarily Config specific, other entity types can just declare that they have no original ID, and we remove one more thing that calling code (like storage controllers) needs to special case.
Proposed resolution
Move getOriginalId()/setOriginalId() to EntityInterface
Remaining tasks
N/A
User interface changes
N/A
API changes
Any code implementing EntityInterface must now implement getOriginalId() and setOriginalId().
If the entity type does not support renames, then getOriginalId() can return NULL, and setOriginalId() can ignore the parameter.
Comment | File | Size | Author |
---|---|---|---|
#10 | original-id-2218033-10.patch | 5.38 KB | tim.plunkett |
Comments
Comment #1
tim.plunkettA helper for
$id = $entity->getOriginalId() !== NULL ? $entity->getOriginalId() : $entity->id();
might be nice, not sure.Comment #2
sunHm. That's a bit weird to me.
What breaks if those two default implementations are removed?
Hm. Either
mixed|null
or perhaps we want to be really explicit and make it actuallyint|string|null
...?I think I'd prefer the latter, but no strong opinion.
Comment #3
tim.plunkett1) ContentEntityBase, MenuLink, EntityCacheTest, TestEntity, FieldUITestNoBundle will all need some sort of empty implementation. Why not let it live there?
2) Oh good point. I went back and forth on those types several times, but forgot to update the docblocks
Comment #4
tim.plunkettChatted with @sun more on IRC, we agreed to leave the implementation on Entity. We can always remove it if we want (maybe after MenuLink is converted).
I also expanded the docblock to explain more about when and why getOriginalId is used.
Comment #5
sunI liked the (inter)diff in https://privatepaste.com/34c16bba22 — was that a mistake or did you intentionally skip the additional changes? :)
Comment #6
tim.plunkettUgh uploaded wrong patch. Will reroll later unless someone beats me to it
Comment #7
tim.plunkettDoh.
Comment #8
sunCoolio, thanks!
Comment #10
tim.plunkett#2217975: ConfigEntityBase::preSave() doesn't properly support renames; ConfigStorageController::save() preforms renames too early moved some code around, git apply --3way was able to resolve it automatically
Comment #12
tim.plunkett10: original-id-2218033-10.patch queued for re-testing.
Comment #13
tim.plunkettComment #15
tim.plunkett10: original-id-2218033-10.patch queued for re-testing.
Comment #17
webchick"The test did not complete due to a fatal error." in Drupal\field\Tests\FormTest->testFieldFormDefaultValue() has been coming up a lot today, but has nothing to do with this patch per se. (Test client #958 fwiw) Retesting.
Comment #18
webchick10: original-id-2218033-10.patch queued for re-testing.
Comment #19
tim.plunkettOne more time, with gusto!
Comment #20
webchickGusto gets it done. :)
Committed and pushed to 8.x. Thanks!