Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
EntityReferenceItem::generateSampleValue() attempts to load a referenceable entity for use as a sample value.
If it cannot, it returns NULL, and unexpected behavior will result.
Specifically, if generating a comment entity, it must create a parent entity or there will be fatal errors in CommentAccessControlHandler, CommentDefaultFormatter, etc.
Proposed resolution
Generate a sample entity if no referenceable entities are found.
Remaining tasks
N/A
User interface changes
N/A
API changes
N/A
Data model changes
N/A
Comment | File | Size | Author |
---|---|---|---|
#9 | 2936793-entity_ref-9.patch | 9 KB | tim.plunkett |
#9 | 2936793-entity_ref-9-interdiff.txt | 1.64 KB | tim.plunkett |
#7 | 2936793-entity_ref-7.patch | 7.36 KB | tim.plunkett |
#7 | 2936793-entity_ref-7-interdiff.txt | 3.39 KB | tim.plunkett |
#4 | 2936793-entity_ref-4-PASS.patch | 4.67 KB | tim.plunkett |
Comments
Comment #2
tim.plunkettComment #3
tstoecklerI think this makes sense. Also good catch on the recursion problem, I wouldn't have thought of that!
I think we should fetch the handler and check the
target_bundles
setting, if it exists, to fetch a valid bundle.Comment #4
tim.plunkettOh, I was looking for something like that! I was testing on default comments, and they didn't have that setting.
Here's a test
Comment #7
tim.plunkettHere are the other fixes.
Interestingly enough, Migrate had test coverage acknowledging that this doesn't work for comment or aggregator_item, but now it does.
The other fix was around the allowed values callback changing.
\Drupal\Core\Field\Plugin\Field\FieldType\LanguageItem already has an @todo pointing to #2329937: Allow definition objects to provide options, which is why this is a bit trickier now.
Comment #9
tim.plunkettTurns out that creating blocked users doesn't really help much, ever.
Opened #2936864: Discuss adding a 'status' field type that could be used in place of 'boolean' as a follow-up
Comment #10
EclipseGc CreditAttribution: EclipseGc at Acquia commentedThis looks super sensible and jives with things I had to do with user timezone when we introduced the sample entity generation utilities to core.
RTBC.
Eclipse
Comment #11
webchick@tim.plunkett walked me through this patch this afternoon. It's a soft-blocker to the Layout Builder, since under the right circumstances you can trigger the bug that's being fixed here via the auto-generated preview stuff.
The basic logic here is handling the fallthrough case where there isn't existing referenceable content to randomize, which would normally return NULL, causing issues up the chain. Here we add code to generate a new entity from scratch if we can't find one to reference (e.g. on a blank, brand new site).
This one bug fix uncovered lots of tasty other morsels elsewhere in the code, which the patch proceeds to clean up.
The one eyebrow-raising thing for me was the addition of
+class StatusItem extends BooleanItem {
in User module. Tim explained that this was added because we most likely do not want purely random 1s or 0s, but instead to create non-blocked users, which makes sense to me. This is also consistent with the approach @EclipseGc mentions in #10 with how time zones were handled.There's an issue at #2936864: Discuss adding a 'status' field type that could be used in place of 'boolean' that discusses if/whether we want to move this "up" a level to that of other primary field types, but there are some UI considerations there, and if we do decide to do that, we'll likely have to mirror the approach with time zones at that time as well.
So TL;DR, this fixes a bug, fixes various test coverage that was working around the bug, and helps make user-generation more sane going forward. Sweet! :)
Committed and pushed to 8.6.x, cherry-picked to 8.5.x. Thanks!
Comment #12
tacituseu CreditAttribution: tacituseu commentedLooks like this broke HEAD on PHP 5.x:
https://www.drupal.org/pift-ci-job/860681
https://www.drupal.org/pift-ci-job/860721
Comment #13
tim.plunkettComment #14
webchickOk great, thanks for the quick turnaround. Got that one in too.
Committed and pushed to 8.5.x/8.6.x.
Comment #15
Gábor HojtsyContrary to the commit message this was only committed to 8.5 (straight). Found while reviewing my own commit mistakes ;)
Comment #16
Gábor HojtsyHm, when I tried to push my commits, this commit came up. (After I freshly pulled this branch right before). Not sure what happened there, but Angie did put this in 8.6 too.
Comment #21
Wim LeersThis reminds me of #2430669: Cannot create comments from REST - field access is too strict and #2631774: Impossible to update Comment entity with REST (HTTP PATCH): bundle field not allowed to be updated, but EntityNormalizer::denormalize() requires it :)