Problem/Motivation

With php 7.4 I get this notice
Notice: Trying to access array offset on value of type null in Drupal\views\Plugin\views\display\EntityReference->query() (line 196 of /var/www/html/web/core/modules/views/src/Plugin/views/display/EntityReference.php) when I try to programmatically execute a views with entity_reference display

Steps to reproduce

I have a code like this

  /** @var \Drupal\views\ViewExecutable $view */
  $view = Views::getView($views_id);
  $view->execute($views_display_id);

inside a hook_form_views_exposed_form_alter() implementations. The "$views_display_id" is an entity_reference display id.

Proposed resolution

Set default values for entity_reference_options for the entity_reference views display in the code. The default options should match the default values that would typically be applied if the entity_reference view is executed under normal circumstance, i.e. through Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::getReferenceableEntities().

Remaining tasks

User interface changes

none

API changes

default values set for entity_reference_options for the entity_reference views display

Data model changes

none

Release notes snippet

TBD

Issue fork drupal-3180227

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

robertom created an issue. See original summary.

robertom’s picture

Attached a proposed patch

Lendude’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

@robertom Thanks for supplying the patch.

Usually adding empty() checks really just hides an underlying problem. I remember there being some logic to make sure that entity references always have a limit, but your code path might be circumventing that. We really need an automated test to prove that a normal code path indeed triggers the notice.

Michael Zetterberg fd. Lopez’s picture

Maybe this issue is somehow related? https://www.drupal.org/project/entity_reference_views_select/issues/3179436

I got the notice when using a Viewfield in a node with Always use default value checked and a view with entity references.

mobile@dpi-art.nl’s picture

I have the same notice when using drupal_view() (twig-tweak) in a node.(Drupal 8.9.14)
The patch #2 from @robertom solved the problem.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

super_romeo’s picture

Patch #2 for D9 works for me.
Thanks, @robertom!

nikita_tt’s picture

#2 works for 9.1.7. Thanks!

jurgenhaas’s picture

I'm getting the same error even when just opening the view in the views ui. The problem being, that $options is NULL my in case. It gets defined in line 158 inside the same callback:

$options = $this->getOption('entity_reference_options');

and the DisplayPluginBase does have a lot of options, but none for entity_reference_options. I wasn't able to figure out where those options should come from. I wonder if this is related to the entity type being used in that context?

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

maxilein’s picture

With D.9.2.9 I am getting a similar message:
I created a new view for type user. But without page or block. Just the master display.
I add the email field. And a second Name field, which I overwrite with values like (name and email ...) .
When I try to ADD an entity reference view the following notice appears


Error message

    Notice: Trying to access array offset on value of type null in Drupal\views\Plugin\views\display\EntityReference->query() (line 195 of core/modules/views/src/Plugin/views/display/EntityReference.php).

    Drupal\views\Plugin\views\display\EntityReference->query() (Line: 1301)
    Drupal\views\ViewExecutable->build('entity_reference_1') (Line: 70)
    views_fieldsets_views_ui_display_tab_alter(Array, Object, 'entity_reference_1') (Line: 539)
    Drupal\Core\Extension\ModuleHandler->alter('views_ui_display_tab', Array, Object, 'entity_reference_1') (Line: 381)
    Drupal\views_ui\ViewEditForm->getDisplayTab(Object) (Line: 204)
    Drupal\views_ui\ViewEditForm->form(Array, Object) (Line: 106)
    Drupal\Core\Entity\EntityForm->buildForm(Array, Object) (Line: 41)
    Drupal\views_ui\ViewFormBase->buildForm(Array, Object)
    call_user_func_array(Array, Array) (Line: 532)
    Drupal\Core\Form\FormBuilder->retrieveForm('view_edit_form', Object) (Line: 278)
    Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 48)
    Drupal\Core\Entity\EntityFormBuilder->getForm(Object, 'edit', Array) (Line: 230)
    Drupal\views_ui\Controller\ViewsUIController->edit(Object, 'entity_reference_1')
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 578)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 158)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
    Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 717)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)


I did not try the patch yet.

The error is gone in the moment I add a PAGE to the view.

danflanagan8’s picture

Status: Needs work » Needs review
FileSize
2.26 KB

Here's a patch that adds a few lines to the existing test for \Drupal\views\Plugin\views\display\EntityReference to try to trigger the error message.

It looks like this warning shows up when the view is executed directly, instead of being called through the ViewsSelection EntityReferenceSelection plugin. So that's what the new part of the test does.

Status: Needs review » Needs work

The last submitted patch, 12: 3180227-12-FAIL.patch, failed testing. View results

danflanagan8’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

Well, it looks like that test failed perfectly:

1) Drupal\Tests\views\Functional\Plugin\DisplayEntityReferenceTest::testEntityReferenceDisplay
Trying to access array offset on value of type null

/var/www/html/core/modules/views/src/Plugin/views/display/EntityReference.php:195
/var/www/html/core/modules/views/src/ViewExecutable.php:1301
/var/www/html/core/modules/views/src/ViewExecutable.php:1391
/var/www/html/core/modules/views/tests/src/Functional/ViewTestBase.php:125
/var/www/html/core/modules/views/tests/src/Functional/Plugin/DisplayEntityReferenceTest.php:283
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php:722

I'm going to remove the Needs tests tag.

I'm not convinced that simply suppressing the error is the right thing to do. What is a use case for executing an EntityReference display outside of the context of ViewsSelection? Is this something that should even be supported?

Setting to NR to get some feedback.

danflanagan8’s picture

After sleeping on this, I've decided to post a patch that sets default values for entity_reference_options in the code. These match the default values that would typically be applied if the entity_reference view is executed under normal circumstance, i.e. through Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::getReferenceableEntities(). This seems reasonable to me.

Note that much of the interdiff is related to running only the updated test in #12 and now wanting to run all tests.

vrwired’s picture

Thanks danflanagan8, good solution! For those of us still lagging behind on D8, here's patch for 8.9.19.

vrwired’s picture

FileSize
1.85 KB

Previous patch missed a line... updated.

The last submitted patch, 16: 3180227-16.patch, failed testing. View results

bwoods’s picture

Confiming that patch in #17 works in 9.3.9. Thanks!

metallized’s picture

Works.

metallized’s picture

Status: Needs review » Reviewed & tested by the community

hswong3i made their first commit to this issue’s fork.

quietone’s picture

Version: 9.4.x-dev » 10.0.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +Bug Smash Initiative, +Needs issue summary update

Reviewing RTBC issues.

I downloaded the MR and the patch in #17, ran diff and found that the two are identical. Therefor removing credit for the MR.

The Issue summary does not have a proposed resolution, adding tag for that.

The patch is for 9.4 and there is a fail test, in #12. Changing version to 10.0

+++ b/core/modules/views/src/Plugin/views/display/EntityReference.php
@@ -156,7 +156,15 @@ class EntityReference extends DisplayPluginBase {
+    // @see Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::initializeView()

I did go read ViewsSelection::initializeView() and it offers no insight to me into why the default options are set to the values below. Can we have something more helpful here?

Setting to NW. At least the two items should be easy.

Lendude’s picture

+++ b/core/modules/views/tests/src/Functional/Plugin/DisplayEntityReferenceTest.php
@@ -275,6 +275,12 @@ public function testEntityReferenceDisplay() {
+    // Execute the View without setting the 'entity_reference_options'.
+    $view->destroy();
+    $view = Views::getView('test_display_entity_reference');
+    $view->setDisplay('entity_reference_1');
+    $this->executeView($view);

And it would be great to actually assert something after executing the view. Maybe that the options got set? If we don't assert anything, and don't have comment explaining what why we are executing without setting the 'entity_reference_options', this just looks like dead code outside the context of this issue. So maybe either update the comment to say something like 'Test that this doesn't trigger notices', or add an assertion (I'd vote for adding the assertion).

danflanagan8’s picture

Assigned: Unassigned » danflanagan8
Issue summary: View changes
Issue tags: -Needs issue summary update

Thanks for the reviews @quietone and @Lendude.

I have updated the IS. Removing tag.

I'll try to address the comments in a new patch.

danflanagan8’s picture

Status: Needs work » Needs review
FileSize
2.4 KB
2.08 KB

Here's an updated patch for 10.0.x that attempts to address the comments in #24 and #25. The new assertion added to the test comes after the line that triggers the fail in #12, so we don't need to run this as a test-only.

Please speak up if the new comments aren't good enough! I'm happy to make additional changes.

danflanagan8’s picture

Try that again...

Interdiff is still relative to #15.

Rajab Natshah’s picture

Status: Needs review » Reviewed & tested by the community
danflanagan8’s picture

Assigned: danflanagan8 » Unassigned

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 28: 3180227-28.patch, failed testing. View results

AswathyAjish’s picture

I have the same issue in drupal 9.4.5 version. I tried #17, but no use.

Any other solution?

mikkmiggur’s picture

Got that issue with drupal 9.4.7.
What solution is suggested?

danflanagan8’s picture

The patch in #28 was RTBC but it got knocked back by a random test fail. I've triggered a re-test. That patch is worth trying. And maybe you can re-RTBC it if it works for you and the tests pass.

If that patch does not apply to 9.4.x you can tag this issue as needing a re-roll or you can do a re-roll yourself and post it.

mkindred’s picture

I was seeing the same error when creating an entity reference view within the UI. I was able to apply #28 to 9.4.8, and it fixed the error.

rpayanm made their first commit to this issue’s fork.

marysmech’s picture

Patch #28 fixed problem for me.

rpayanm’s picture

Status: Needs work » Needs review
stevenlafl’s picture

Status: Needs review » Reviewed & tested by the community

#28 fixes the described issue and does not change functionality. Works programmatically as well as with entity select field options arrays.

bilias’s picture

#28 fixed problem as well
9.5.0 / php 8.0.26

xjm’s picture

I checked with @rpayanm as to why he opened an MR when there is a working patch. He said he had noticed a previous test failure on the issue and was trying to see if he could fix it, starting with a rebase to get the patch current:
https://www.drupal.org/pift-ci-job/2455217

It looks like that issue was an unrelated failure in the Book module. We had a spate of the same failures in 10.0.x HEAD environments in August and September, around the time of the above job, so it is unrelated to this issue. @rpayanm and I talked a little about the different kinds of test failures and what it looks like when a reroll/rebase is needed (i.e. "Patch does not apply").

Thanks @rpayanm for talking through it! I'm going to close the redundant MR and remove the credit for it. Feel free to ask me anytime for help figuring out if a test failure is related to the issue or not. :) I have the world's second-largest database of historical Drupal core CI failures in my email. :D

xjm’s picture

xjm’s picture

Assigned: Unassigned » Lendude
Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs subsystem maintainer review

So my first thought upon reviewing this is, is there a reason for the custom code to not initialize the view? The Views API expects a series of steps to be taken when preparing the view.

My other concern is hardcoding default values from a different API. However, the test should start failing (I think) if we ever break the 1:1 relationship between the defaults we're setting and the defaults being applied externally, so that should be alright too.

Other than that, this looks good to me. We have a failing test in #12 proving the bug, and review from @Lendude (who is one of the Views maintainers) to improve the test in #25. Overall, if we decide that the view doesn't need to be or shouldn't be initialized, it seems like a good solution. It is also well-documented.

Before committing this, I'd like to get final signoff from a Views subsystem maintainer on the change. (Yes, I am also a Views subsystem maintainer, but it's been awhile OK?) 😅

So, setting NR, tagging for subsystem maintainer review, and assigning to @Lendude. @Lendude, if you think the current patch in #28 is a good solution, please untag the subsystem maintainer review tag and set the issue back to RTBC, and we'll try to get this in. Thanks!

Lendude’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs subsystem maintainer review

Thanks for taking a good look at this @xjm!

The Views API expects a series of steps to be taken when preparing the view.

So, in this context, it's not the Views API that expects this, but the EntityReferenceSelection that expects this. And now we are executing the View outside the context of EntityReferenceSelection. So, to me, we should either throw a warning that this particular display expects you to do more steps, or we should be lenient and just set what's needed.
I fully agree that hardcoding values from a different API feels iffy, but since this is done specifically in the 'entity_reference' display only, I can live with it here.

But if others feels that emitting an error is the better course here, I do see a point for that.

  • xjm committed 0f6101a7 on 10.1.x
    Issue #3180227 by danflanagan8, robertom, xjm, Lendude, quietone: Notice...

  • xjm committed 0b548bb6 on 10.0.x
    Issue #3180227 by danflanagan8, robertom, xjm, Lendude, quietone: Notice...

  • xjm committed 88e0d463 on 9.5.x
    Issue #3180227 by danflanagan8, robertom, xjm, Lendude, quietone: Notice...
xjm’s picture

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

Thanks @Lendude for your feedback! So we'll go ahead with this change. I'm still a little hesitant about it, but with both docs and test coverage, it's probably the best it can be under the circumstance for fixing the DX here without entirely overhauling how display plugins work. And, given the number of people apparently encountering this bug (just look at the number of followers and "WFM" comments), it's probably best to fix it as we can in production branches.

I'm removing credit for @vrwired for D8 backport patches as they were after D8's end-of-life. Retaining credit for @robertom's original issue report, @danflanagan8's fix, and helpful code reviews.

Committed to 10.1.x. Since this is initializing default values to non-disruptive defaults when a notice would be thrown, it also seems safe in its current form to backport to 10.0.x and 9.5.x, I also cherry-picked it there. (The alternate solution of throwing a warning or exception would not have been backportable.) Thanks everyone!

Status: Fixed » Closed (fixed)

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