Problem/Motivation

Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::stripAdminAndAnchorTagsFromResults() causes a fatal Uncaught PHP Exception Error: "Call to a member function bundle() on null if the view $results contain elements which are do not have a ['#row']->_entity property (i.e. an instance of \Drupal\views\ResultRow).

This can happen if the $results render array have a property, e.g. #cache

Steps to reproduce

Create a view with an Entity Reference Selection display
Implement hook_views_post_render, in it alter the $output and add a cache tag, e.g. $output['#cache']['tags'][] = 'workbench_access_view';
Render the view, notice the fatal error

Proposed resolution

Change stripAdminAndAnchorTagsFromResults() looping over the results to use a standard Element::children($results)

Remaining tasks

Review, add tests

User interface changes

NA

API changes

NA

Data model changes

NA

Comments

fenstrat created an issue. See original summary.

fenstrat’s picture

Issue summary: View changes
Status: Active » Needs review
Issue tags: +Needs tests
Related issues: +#3201775: The views filter needs to have cacheability metadata applied when using Views caching
StatusFileSize
new1.24 KB

Here's the fix. This could probably do with a test as well.

We hit this with #3201775: The views filter needs to have cacheability metadata applied when using Views caching where workbench_access does a hook_views_post_render() and adds a cache tag.

lendude’s picture

Status: Needs review » Needs work

So the type hints here are ancient and usually just mention 'array', without specifying if we mean a 'render array'. Following the code back from the call to stripAdminAndAnchorTagsFromResults this acts on the result of \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::getDisplayExecutionResults which uses \Drupal\views\ViewExecutable::executeDisplay which DOES specify it returns a render array.

So yes I agree, the method should assume it can get more than just an array of rows and the change makes sense.

But yeah, it needs a test. I could have sworn we had some for stripAdminAndAnchorTagsFromResults but can't find it.

acbramley’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new3.15 KB
new4.39 KB

I spent quite a bit of time trying to find existing tests for this and there indeed doesn't seem to be any. In fact there doesn't seem to be any for the selection plugin at all, just executing an entity reference view display (DisplayEntityReferenceTest).

Here's a red/green patch combo. Not entirely convinced this is the right place for the test or test hook so happy to move those if needed.

The last submitted patch, 4: 3341448-4--test-only.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 4: 3341448-4.patch, failed testing. View results

acbramley’s picture

Status: Needs work » Needs review

Given all the failures we definitely need to move the pre_render, or add another state flag to trigger it.

acbramley’s picture

Status: Needs review » Needs work

Didn't mean to change status.

acbramley’s picture

Status: Needs work » Needs review
StatusFileSize
new3.33 KB
new4.57 KB
new2.1 KB

Added a state guard to only add the cache tag when we want it.

The last submitted patch, 9: 3341448-9--test-only.patch, failed testing. View results

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Tested following the steps in the issue summary

Added $output['#cache']['tags'][] = 'foo';
to a random hook_views_post_render() (didn't check view if so applied to all)
Create entity reference view for the Article tags field
Created an Article
Checked logs and noticed error.

larowlan’s picture

Adding issue credits

  • larowlan committed bd88cad2 on 10.1.x
    Issue #3341448 by acbramley, fenstrat, Lendude, smustgrave:...
larowlan’s picture

Title: EntityReference ViewsSelection::stripAdminAndAnchorTagsFromResults() should call Element::children($results) » [Needs backport] EntityReference ViewsSelection::stripAdminAndAnchorTagsFromResults() should call Element::children($results)
Version: 10.1.x-dev » 10.0.x-dev

Committed to 10.1.x

Will backport once tests come back greened (which I've queued)

  • larowlan committed 4f3a6731 on 10.0.x
    Issue #3341448 by acbramley, fenstrat, Lendude, smustgrave:...

  • larowlan committed 813b70ab on 9.5.x
    Issue #3341448 by acbramley, fenstrat, Lendude, smustgrave:...
larowlan’s picture

Version: 10.0.x-dev » 9.5.x-dev
Status: Reviewed & tested by the community » Fixed

Backported to 10.0.x and 9.5.x

larowlan’s picture

Title: [Needs backport] EntityReference ViewsSelection::stripAdminAndAnchorTagsFromResults() should call Element::children($results) » EntityReference ViewsSelection::stripAdminAndAnchorTagsFromResults() should call Element::children($results)

Status: Fixed » Closed (fixed)

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

drupalevangelist’s picture

Hi All,
I updated the Inline Entity Form module to version 8.x-1.0-rc15 and getting the following error when I go to the create node page that has an inline entity item:

Error: Call to a member function bundle() on null in Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection->stripAdminAndAnchorTagsFromResults() (line 289 of core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php).

I also posted a comment here: https://www.drupal.org/node/2822764.

My site is running on D9.5.9, and on PHP 8.1

I will appreciate it if you kindly provide some insight.

Thank you,
Emdad

acbramley’s picture

@drupalevangelist please create a new issue in the queue with steps to reproduce. It sounds like your view is producing results that can't be loaded.