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
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | interdiff-3341448-4-9.txt | 2.1 KB | acbramley |
| #9 | 3341448-9.patch | 4.57 KB | acbramley |
| #9 | 3341448-9--test-only.patch | 3.33 KB | acbramley |
Comments
Comment #2
fenstratHere'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.
Comment #3
lendudeSo 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
stripAdminAndAnchorTagsFromResultsthis 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.
Comment #4
acbramley commentedI 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.
Comment #7
acbramley commentedGiven all the failures we definitely need to move the pre_render, or add another state flag to trigger it.
Comment #8
acbramley commentedDidn't mean to change status.
Comment #9
acbramley commentedAdded a state guard to only add the cache tag when we want it.
Comment #11
smustgrave commentedTested 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.
Comment #12
larowlanAdding issue credits
Comment #14
larowlanCommitted to 10.1.x
Will backport once tests come back greened (which I've queued)
Comment #17
larowlanBackported to 10.0.x and 9.5.x
Comment #18
larowlanComment #20
drupalevangelist commentedHi 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
Comment #21
acbramley commented@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.