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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett created an issue. See original summary.

tim.plunkett’s picture

Status: Active » Needs review
FileSize
1.35 KB
tstoeckler’s picture

Status: Needs review » Needs work

I 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.

tim.plunkett’s picture

Oh, I was looking for something like that! I was testing on default comments, and they didn't have that setting.

Here's a test

The last submitted patch, 4: 2936793-entity_ref-4-FAIL.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 4: 2936793-entity_ref-4-PASS.patch, failed testing. View results

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
3.39 KB
7.36 KB

Here 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.

Status: Needs review » Needs work

The last submitted patch, 7: 2936793-entity_ref-7.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
1.64 KB
9 KB

Turns 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

EclipseGc’s picture

Status: Needs review » Reviewed & tested by the community

This 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

webchick’s picture

Status: Reviewed & tested by the community » Fixed

@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!

tacituseu’s picture

Status: Fixed » Needs work

Looks like this broke HEAD on PHP 5.x:
https://www.drupal.org/pift-ci-job/860681
https://www.drupal.org/pift-ci-job/860721

Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testContentEntityReferenceItem
PHPUnit_Framework_Exception: PHP Fatal error:  Call to undefined function \Drupal\Core\Field\Plugin\Field\FieldType\LanguageItem::getAllowedLanguageCodes() in /var/www/html/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php on line 128
Fatal error: Call to undefined function \Drupal\Core\Field\Plugin\Field\FieldType\LanguageItem::getAllowedLanguageCodes() in /var/www/html/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php on line 128
tim.plunkett’s picture

webchick’s picture

Status: Needs review » Fixed

Ok great, thanks for the quick turnaround. Got that one in too.

Committed and pushed to 8.5.x/8.6.x.

Gábor Hojtsy’s picture

Assigned: Unassigned » webchick
Status: Fixed » Needs work

Contrary to the commit message this was only committed to 8.5 (straight). Found while reviewing my own commit mistakes ;)

Gábor Hojtsy’s picture

Assigned: webchick » Unassigned
Status: Needs work » Fixed

Hm, 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.

  • webchick committed f724db3 on 8.6.x
    Issue #2936793 by tim.plunkett, tstoeckler, EclipseGc:...

  • webchick committed 877918c on 8.5.x
    Issue #2936793 by tim.plunkett, tstoeckler, EclipseGc:...

  • webchick committed 02af9bc on 8.5.x
    Issue #2936793 follow-up by tim.plunkett: Fix PHP5's hacking and...

  • webchick committed 8e3287f on 8.6.x
    Issue #2936793 follow-up by tim.plunkett: Fix PHP5's hacking and...
Wim Leers’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.