Problem/Motivation

Found in #1853524: Reintroduce Views integration for Book that rendering fields using optional relationship does not work when the relationship does not exist.

Steps to reproduce:
- Add an article with one tag
- Add another article without tags
- Create a view of articles with page display
- Set page path to /test-page
- Add node title field "Content: title"
- Add a relationship using field_tags "field_tags: Taxonomy term" and do not check the "Require this relationship" checkbox
- Add term name field "Taxonomy term: Name"
- Save the view
- View /test-page.

Expected result: You should see two articles and one term

Actual result: Blank page with text "Fatal error: Call to a member function bundle() on a non-object in core/modules/views/src/Entity/Render/EntityFieldRenderer.php on line 203"

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

olli’s picture

olli’s picture

+++ b/core/modules/views/src/Entity/Render/EntityFieldRenderer.php
@@ -138,13 +138,18 @@ public function render(ResultRow $row, Field $field = NULL) {
         unset($this->build[$row->index][$field_id]);
       }
-      else {
+      elseif (isset($this->build[$row->index])) {
         // In the uncommon case where a field gets rendered several times
         // (typically through direct Views API calls), the pre-computed render
         // array was removed by the unset() above. We have to manually rebuild
         // the render array for the row.
         $build = $this->buildFields([$row])[$row->index][$field_id];
       }
+      else {
+        // In case the relationship is optional, there might not be any fields
+        // to render for this row.
+        $build = [];

This part is different from the interdiff.txt in #1853524-49: Reintroduce Views integration for Book.

The last submitted patch, 1: 2534780-1-test.patch, failed testing.

dawehner’s picture

I'm a little bit confused about the test, because it does not use entities, so the fix in the entity related code will actually not run for those tests ...

+++ b/core/modules/views/src/Tests/Handler/RelationshipTest.php
@@ -18,6 +18,7 @@
 class RelationshipTest extends RelationshipJoinTestBase {
+  use UserCreationTrait;

Nitpick: I think we use an empty line before that.

olli’s picture

+++ b/core/modules/views/src/Tests/Handler/RelationshipTest.php
@@ -133,4 +134,51 @@ public function testRelationshipQuery() {
+      'author' => [
+        'id' => 'author',
+        'table' => 'users_field_data',
+        'field' => 'name',
+        'relationship' => 'uid',
+      ],

#4: I'm trying to use that code by rendering user entity field here. Is there a better way to do that? Also, I wonder if we should explicitly set 'hide_empty' and 'required' to FALSE.

jibran’s picture

#1 fixed the error for me in #2321721-67: Provide a views relationship for each dynamic entity reference field. Thank you @olli for finding and fixing this.

jibran’s picture

Priority: Normal » Major

It is at least major if not critical because it's a very normal use case.

dawehner’s picture

#4: I'm trying to use that code by rendering user entity field here. Is there a better way to do that? Also, I wonder if we should explicitly set 'hide_empty' and 'required' to FALSE.

OH I see, well it still feels better to start maybe with the entity_test table.

Tim Asplin’s picture

Hello,

Thanks for this, fixed same issue for me.

Had a view with an optional relation ship and was causing: Fatal error: Call to a member function bundle() on a non-object in core/modules/views/src/Entity/Render/EntityFieldRenderer.php

Thanks for the patch

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

OH I see, well it still feels better to start maybe with the entity_test table.

But this way it actually even ensures that the other usescases work.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

@olli nice find, nice fix and nice test. Committed 8ddd134 and pushed to 8.0.x. Thanks!

  • alexpott committed 8ddd134 on 8.0.x
    Issue #2534780 by olli, dawehner: Fatal error rendering fields using an...

Status: Fixed » Closed (fixed)

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