Problem/Motivation
Drupal\Core\Entity\Plugin\DataType\EntityReferenceItem::getPropertyDefinitions() hardcodes the assumption that content entities have numeric IDs:
static::$propertyDefinitions[$key]['target_id'] = array(
// @todo: Lookup the entity type's ID data type and use it here.
'type' => 'integer',
'label' => t('Entity ID'),
'constraints' => array(
'Range' => array('min' => 0),
),
);
Proposed resolution
Suggested by @fago in #1757452-68: Support config entities in entity reference fields:
I'd suggest taking the "id" entity key and looking up the definition of the first field item property (or go with the main property once introduced by #2015687: Convert field type to FieldType plugin for taxonomy module ).
Remaining tasks
Write a patch.
User interface changes
None.
API changes
None.
Related Issues
#1757452: Support config entities in entity reference fields
| Comment | File | Size | Author |
|---|---|---|---|
| #45 | interdiff.txt | 1.44 KB | amateescu |
| #45 | 2107249-45.patch | 10.04 KB | amateescu |
| #43 | interdiff-proper-data-type.txt | 1.94 KB | amateescu |
| #43 | interdiff.txt | 1.46 KB | amateescu |
| #43 | 2107249-43.patch | 9.97 KB | amateescu |
Comments
Comment #1
xanoComment #2
xanoComment #3
berdir2: drupal_2107249_2.patch queued for re-testing.
Comment #6
xanoComment #10
tim.plunkettSee #2345813: Primitive TypedData fields are not cast properly for yet another reason we need this.
Comment #11
jibranreroll
Comment #13
tstoecklerThis is major. This makes it impossible to use content entities with non-integer IDs as targets of entity references. That's a pretty standard use-case.
Comment #14
tstoecklerLet's see if this applies, will roll the patch into it if it does.
Comment #16
tstoecklerComment #17
tstoecklerOops, less custom crap this time. Let's see if this works.
Comment #19
tstoecklerOk, let's try this again.
Comment #20
tstoecklerOK, that was not enough. The schema needs to be updated as well, of course.
Also brings back the test from above. The code changed so much in the meantime that I recreated it, so no interdiff.
Comment #22
tstoecklerBah. Should be green.
Comment #24
tstoecklerTestbot, what's wrong?! :'-(
Comment #26
jelle_sReroll of #24.
Comment #28
jelle_sAfter a lot of digging through code, this patch should be green...
Comment #29
jelle_sComment #30
amateescu commentedThe patch looks pretty good, nice work!
I think it would be cleaner if we move the
$properties = ...call above the firstif()and then add the$properties->getDataType() === 'integer'condition to it, and have a single 'else' case for a varchar target_id.Are the changes to this phpunit test actually testing something related to this patch?
Also, can you please upload a test-only patch with just the test changes to show that we currently cannot reference content entities with string ids? Because that's actually a bug, IMO :)
Comment #31
jelle_sThe changes to the EntityViewsDataTest were just necessary to make it pass again, because of these additions:
there were a couple of things that needed to be mocked. Nothing new is being tested there.
In EntityReferenceItemTest the additions do test the new behavior. I uploaded a tests only patch as well.
Comment #32
jelle_sSorry, tests only patch was wrong. Correct one this time.
Comment #34
amateescu commentedSince this doesn't fail, maybe we need to add some more assertions after the save() call? Something like asserting that 'target_id' has the correct assigned value.
Also, you should use ->target_id instead of ->value, because the entity reference field type does not have a 'value' property.
Comment #35
attiks commented#34 You're right, the save never fails, it just writes
0into the database. Which sounds like a - separate - bug as well.We have to load the entity again and check if our test values is actually set.
Comment #36
tstoecklerThen the test is doing something wrong. When I tried this on an actual site (in the belief that we had already fixed this at some point) I got SQL exceptions because it was trying to save 'some_id' into a SQL int column.
Comment #37
jelle_sNew patch with the added assertions.
Comment #39
jelle_sSo the tests only patch failed as it should and the complete patch passed the tests.
Comment #40
amateescu commentedIt would be nice to also have a confirmation from @berdir or @yched, but the latest patch looks great to me. It's nice to see that typed data / entity field api got to a point where this change is so small and easy to grasp :)
Comment #41
tstoecklerYay! :-)
I guess I can't RTBC this, but it looks great now.
Awesome work @Jelle_S, thanks a ton!!!
Edit: x-post, yay @amateescu
Comment #42
yched commentedI‘m away from my coding env, so not too easy to look at the patch in context :-)
Something looks weird though : unless I'm misreading, we‘re taking the *field* type and settings of the id field we're targeting, and use them as a *TD property* type and settings ? That's wrong, those live in different spaces ? It might happen to work with the two specific cases we're interested about (int and string), but generally speaking that code is mixing apples and oranges ?
Rather than aiming for generic support whatever the type of 'id', I think we'd be better off explicitly coding for the two cases we actually support - especially since the schema() implementation can only support those two cases anyway ?
Comment #43
amateescu commentedHeh, of course I missed something :/
After writing the code for supporting any data type for 'id' (in the second interdiff attached), I think I agree that just hard-coding the integer data type for integer field types like we do in schema() is a bit more straightforward.
Comment #45
amateescu commented#sigh
Comment #46
tstoecklerWow, @yched that is totally correct. Sorry for missing that. Everytime I think I understand Field API and Typed Data integration ... ... it's a trap! :-/
Thanks @amateescu, yes the explicit check makes a lot of sense. I hope we can do better in D9, but for now the explicitness makes it very clear what is going instead of the
mainPropertyName()stuff in the other interdiff.Now that the
propertyDefinitions()stuff was rewritten my only contribution to this patch is the two lines inschema()(the tests were written by @Xano and fixed by @Jelle_S) and I think this has gone sufficiently gone back and forth for me to RTBC now.Thanks again @yched for setting me straight!
Comment #47
tstoecklerComment #48
alexpottThis issue addresses a major bug and is allowed per https://www.drupal.org/core/beta-changes. Committed cb451af and pushed to 8.0.x. Thanks!