Problem/Motivation

Creating a node with a entity reference field and a Reference method based on a view trows this error:
Fatal error: Call to a member function bundle() on a non-object in path/to/drupal8.dev/core/modules/views/src/Plugin/entity_reference/selection/ViewsSelection.php on line 174

Steps to reproduce

  1. Create a content type "Brewery", with a name, and a boolean "Active"
  2. Create a content type "Beer" With a name, a body, and an entity reference field, with a reference method of "Views". The view is an "Entity Reference" view of all active breweries (Content: Active (= 1)
  3. Create a content type "Order" with a title, ammount, and order field. The "order" field is an Entity reference field, with a reference method of "views". The view is an "Entity Reference" view of "Beers" with an active brewery. (Listing al Beers, A Relation to brewery, and a filter on the related field ((field_beer_brewery: Content) Content: Active (= 1))
  4. Creating a new node of type "Order" results in:
    Fatal error: Call to a member function bundle() on a non-object in /.../core/modules/views/src/Plugin/entity_reference/selection/ViewsSelection.php on line 174

Proposed resolution

Fix how referenced entities are stored and accessed in results.

Remaining tasks

User interface changes

Not applicable

API changes

Not applicable

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug
Issue priority Major because fatal errors can occur with sufficiently complex views
Unfrozen changes Unfrozen because it only changes some internal flawed logic
Prioritized changes The main goal of this issue is to fix a bug in entity reference related to views selection widget
CommentFileSizeAuthor
#22 views-selection-fatal-2350699-22.patch10.11 KBjhedstrom
#22 interdiff.txt789 bytesjhedstrom
#22 views-selection-fatal-2350699-TEST-ONLY.patch4.14 KBjhedstrom
#19 views-selection-fatal-2350699-19-TEST-ONLY.patch3.87 KBjhedstrom
#14 interdiff.txt5.97 KBolli
#14 2350699-views-selection-fatal-14.patch10 KBolli
#11 2350699-views-selection-fatal-11.patch4.82 KBvijaycs85
#9 views-selection-fatal-2350699-09-WILL-FAIL.patch4.03 KBjhedstrom
#1 vw_active-beers.png542.8 KBAnonymous (not verified)
#1 vw_active-breweries.png477.43 KBAnonymous (not verified)
vw_active-beers.png498.82 KBAnonymous (not verified)
vw_active-breweries.png541.61 KBAnonymous (not verified)
cc_order.png360.54 KBAnonymous (not verified)
cc_beer.png370.67 KBAnonymous (not verified)
cc_brewery.png360.22 KBAnonymous (not verified)

Comments

Anonymous’s picture

Issue summary: View changes
StatusFileSize
new477.43 KB
new542.8 KB
Anonymous’s picture

I got my xdebug working, and this is the XdebugStack:

[0] file:///var/www/awesome/core/modules/views/src/Plugin/entity_reference/selection/ViewsSelection.php.Drupal\views\Plugin\entity_reference\selection\ViewsSelection->getReferenceableEntities:174
[1] file:///var/www/awesome/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php.Drupal\entity_reference\ConfigurableEntityReferenceItem->getSettableOptions:83
[2] file:///var/www/awesome/core/modules/options/src/Plugin/Field/FieldWidget/OptionsWidgetBase.php.Drupal\options\Plugin\Field\FieldWidget\OptionsWidgetBase->getOptions:129
[3] file:///var/www/awesome/core/modules/options/src/Plugin/Field/FieldWidget/SelectWidget.php.Drupal\options\Plugin\Field\FieldWidget\SelectWidget->formElement:38
[4] file:///var/www/awesome/core/lib/Drupal/Core/Field/WidgetBase.php.Drupal\Core\Field\WidgetBase->formSingleElement:312
[5] file:///var/www/awesome/core/lib/Drupal/Core/Field/WidgetBase.php.Drupal\Core\Field\WidgetBase->form:89
[6] file:///var/www/awesome/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php.Drupal\Core\Entity\Entity\EntityFormDisplay->buildForm:159
[7] file:///var/www/awesome/core/lib/Drupal/Core/Entity/ContentEntityForm.php.Drupal\Core\Entity\ContentEntityForm->form:53
[8] file:///var/www/awesome/core/modules/node/src/NodeForm.php.Drupal\node\NodeForm->form:125
[9] file:///var/www/awesome/core/lib/Drupal/Core/Entity/EntityForm.php.Drupal\Core\Entity\EntityForm->buildForm:97
[10] file:///var/www/awesome/core/lib/Drupal/Core/Form/FormBuilder.php.call_user_func_array:{/var/www/awesome/core/lib/Drupal/Core/Form/FormBuilder.php:405}:405
[11] file:///var/www/awesome/core/lib/Drupal/Core/Form/FormBuilder.php.Drupal\Core\Form\FormBuilder->retrieveForm:405
[12] file:///var/www/awesome/core/lib/Drupal/Core/Form/FormBuilder.php.Drupal\Core\Form\FormBuilder->buildForm:224
[13] file:///var/www/awesome/core/lib/Drupal/Core/Entity/EntityFormBuilder.php.Drupal\Core\Entity\EntityFormBuilder->getForm:53
[14] file:///var/www/awesome/core/modules/node/src/Controller/NodeController.php.Drupal\node\Controller\NodeController->add:99
[15] file:///var/www/awesome/core/lib/Drupal/Core/Controller/HtmlPageController.php.call_user_func_array:{/var/www/awesome/core/lib/Drupal/Core/Controller/HtmlPageController.php:76}:76
[16] file:///var/www/awesome/core/lib/Drupal/Core/Controller/HtmlPageController.php.Drupal\Core\Controller\HtmlPageController->getContentResult:76
[17] file:///var/www/awesome/core/lib/Drupal/Core/Controller/HtmlPageController.php.Drupal\Core\Controller\HtmlPageController->content:53
[18] file:///var/www/awesome/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php.call_user_func_array:{/var/www/awesome/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php:126}:126
[19] file:///var/www/awesome/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php.Symfony\Component\HttpKernel\HttpKernel->handleRaw:126
[20] file:///var/www/awesome/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php.Symfony\Component\HttpKernel\HttpKernel->handle:66
[21] file:///var/www/awesome/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php.Drupal\Core\StackMiddleware\KernelPreHandle->handle:53
[22] file:///var/www/awesome/core/lib/Drupal/Core/StackMiddleware/PageCache.php.Drupal\Core\StackMiddleware\PageCache->handle:52
[23] file:///var/www/awesome/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php.Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle:58
[24] file:///var/www/awesome/core/vendor/stack/builder/src/Stack/StackedHttpKernel.php.Stack\StackedHttpKernel->handle:23
[25] file:///var/www/awesome/core/lib/Drupal/Core/DrupalKernel.php.Drupal\Core\DrupalKernel->handle:567
[26] file:///var/www/awesome/index.php.{main}:22

Hope this is of any help.

Anonymous’s picture

Priority: Normal » Major

According the Tips for making a good issue report I think I can mark this issue priority "Major"

jhedstrom’s picture

Confirmed this is an issue.

Digging into it a bit, the problem is in this code from ViewsSelection::getReferenceableEntities():

    if ($result) {
      foreach($this->view->result as $row) {
        $entity = $row->_entity;
        $return[$entity->bundle()][$entity->id()] = $entity->label();
      }
    }

For whatever reason, $row->_entity is null.

jhedstrom’s picture

Issue tags: +VDC
jhedstrom’s picture

I think the issue lies in \Drupal\views\Plugin\views\query\Sql::loadEntities. From the docblock:

* If the entity belongs to the base table, then it gets stored in
* $result->_entity. Otherwise, it gets stored in
* $result->_relationship_entities[$relationship_id];

and the relevant code in that method:

        if ($relationship_id == 'none') {
          $results[$index]->_entity = $entity;
        }
        else {
          $results[$index]->_relationship_entities[$relationship_id] = $entity;
        }

So, in this example, even though the entity does belong to the base table (it's a 'beer' node, filtered by brewery status), since it is filtered on a relationship, the logic above fails to place it in the _entity variable.

vijaycs85’s picture

Title: Call to a member function bundle() views/src/Plugin/entity_reference/selection/ViewsSelection.php on line 174 » [Regression] Call to a member function bundle() views/src/Plugin/entity_reference/selection/ViewsSelection.php on line 174
Version: 8.0.0-beta1 » 8.0.x-dev
vijaycs85’s picture

Title: [Regression] Call to a member function bundle() views/src/Plugin/entity_reference/selection/ViewsSelection.php on line 174 » Call to a member function bundle() views/src/Plugin/entity_reference/selection/ViewsSelection.php on line 174
jhedstrom’s picture

Status: Active » Needs review
StatusFileSize
new4.03 KB

This adds a failing test. All that is needed to reproduce this error is to add any relationship, and filter using that relationship. In this test, I add a relationship to the users table, and filter on UID.

Status: Needs review » Needs work

The last submitted patch, 9: views-selection-fatal-2350699-09-WILL-FAIL.patch, failed testing.

vijaycs85’s picture

Status: Needs work » Needs review
Issue tags: +CapgeminiDrupalDay2014
StatusFileSize
new4.82 KB

Thanks @jhedstrom for the tests. Here is the patch that checks for entity of type so that no fatal. Still need to check, what the result should display as test is looking for a node. So still one fail, but no fatal

jhedstrom’s picture

+++ b/core/modules/views/src/Plugin/entity_reference/selection/ViewsSelection.php
@@ -171,7 +171,9 @@ public function getReferenceableEntities($match = NULL, $match_operator = 'CONTA
-        $return[$entity->bundle()][$entity->id()] = $entity->label();
+        if ($entity instanceof EntityInterface) {
+          $return[$entity->bundle()][$entity->id()] = $entity->label();
+        }

I don't think this is the way to fix the issue. While it will remove the fatal error, it will make it impossible to use complex views for the selection widget. I think the fix needs to go further up the stack, perhaps into how Sql::loadEntities() is categorizing results.

Status: Needs review » Needs work

The last submitted patch, 11: 2350699-views-selection-fatal-11.patch, failed testing.

olli’s picture

StatusFileSize
new10 KB
new5.97 KB

Here's a patch from #2320989: only one relationship per entitytype allowed which reverts parts of #1712456: How to leverage cache tags in Views. The interdiff is against #9.

olli’s picture

Status: Needs work » Needs review
jhedstrom’s picture

Issue summary: View changes

The fix in #14 makes sense to me. I've added a beta phase evaluation. Somebody else should bump to RTBC.

dawehner’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Someone seriously needs to update the IS:

@olli
Can you please explain why you had to reverts parts of the changes in the other issue?

olli’s picture

@dawehner: reading the code up from the hunk in #6, we must make getEntityTableInfo() return table info keyed by alias to know the relationship id, that's why I reverted changes to getEntityTableInfo(). I think we could try loading the entities by type like the current code instead of by table like in #14.

jhedstrom’s picture

Status: Needs work » Needs review
StatusFileSize
new3.87 KB

I was about to update the issue summary, but when trying to reproduce this, I was unable to do so. I wonder if it has been fixed elsewhere? Even the test in #9 passes with the removal of the distinct option (which was throwing a different error). I've re-attached that to test.

jhedstrom’s picture

So...it appears the issue was fixed (bonus points if somebody can find the fix--bisect is failing me). I think it's still worth adding this test to the entity reference module to avoid future regressions.

jhedstrom’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: -Needs issue summary update +Needs tests

This is definitely still an issue, and can be reproduced using the steps originally reported. The test to prove it needs a bit of work though. I've simplified the issue summary.

jhedstrom’s picture

Title: Call to a member function bundle() views/src/Plugin/entity_reference/selection/ViewsSelection.php on line 174 » Using entity reference views selection handler with a relationship results in a fatal error
Status: Needs work » Needs review
StatusFileSize
new4.14 KB
new789 bytes
new10.11 KB

This fixes the test. Interdiff is on #14.

The last submitted patch, 22: views-selection-fatal-2350699-TEST-ONLY.patch, failed testing.

jhedstrom’s picture

Issue tags: -Needs tests
jhedstrom’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 22: views-selection-fatal-2350699-22.patch, failed testing.

bforchhammer’s picture

Status: Needs work » Closed (duplicate)
Related issues: +#2383197: Entities not loaded for relationships on same entity type

I think the problem in this issue is the same as described in #2383197: Entities not loaded for relationships on same entity type. I'm working on a new patch at the moment: I'll include the test from #22 over there to ensure this is fixed as well.

avpaderno’s picture

Issue tags: -#entity +#entity