Problem/Motivation
When an optional relationship on a grouped view is empty, the view fails with a fatal error.
Recoverable fatal error: Argument 1 passed to Drupal\views\Plugin\views\field\Field::createEntityForGroupBy() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in /core/modules/views/src/Plugin/views/field/Field.php on line 826 and defined in Drupal\views\Plugin\views\field\Field->createEntityForGroupBy() (Line 868 in /core/modules/views/src/Plugin/views/field/Field.php).
How to reproduce:
Fresh Drupal 8.1.9 installation.
Create entity reference field in basic page called "used in" to reference content, limit bundle to article.
Create a view "article_rel" showing content elements of bundle Article.
Add a relationship to field_used_in from basic page. (optional).
- Add a node article 1.
- Add a node article 2.
- Add a node article 3.
- Add a node article 4.
- Add a basic page 1.
- reference article 1 and article 2 in "used in" field
- Add a basic page 2.
- reference article 2 in "used in" field
View now shows duplicate entries for basic page 2.
Enable aggregation.
View now fails with error message from above.
Problem here is $this->getEntity() in FieldBase doesn't cover the case of optional / empty relationships. which returns nothing right now. (Returning null explicitely instead of nothing would be probably even nicer here).
That null is then passed to createEntityForGroupBy which expects an EntityInterface causing this bug.
Proposed resolution
Following the behavior for non existing fields setting the output to null would be just fine. Similar for bundles which don't provide those fields Views aggregation: Grouping a field that doesn't exist on all bundles causes an error..
| Comment | File | Size | Author |
|---|---|---|---|
| #27 | 2799415-27-8.3.patch | 8.81 KB | lendude |
| #27 | 2799415-27-8.2.patch | 8.79 KB | lendude |
| #27 | interdiff-2799415-17-27.txt | 1.09 KB | lendude |
| #21 | 2799415-17-test-only-1.patch | 7.44 KB | Andre-B |
| #18 | interdiff-2799415-17.txt | 2.23 KB | Andre-B |
Comments
Comment #2
Andre-Badding patch
Comment #3
Andre-BComment #4
Andre-BComment #5
cilefen commentedThis is at least Major according to the standards:
Comment #6
Andre-Bmaybe someone can help me here with the automated patch. I can't get the view to actually group the contents, the grouped view then actually should fail (at least that's the idea.).
Comment #7
Andre-BComment #8
lendudeThis sounds like a duplicate of at least one (maybe all) of these :
#2731433: Fatal error when using group by on views
#2715483: [regression] getEntity() doesn't always return entity, which results in add comment field not working in Views
#2319005: Notice: Undefined index: [field name] is thrown if relationship entities are missing
#2457999: Cannot use relationship for rendered entity on Views
d.o seems to have issues with me downloading the patch here at the moment, so can't check if the fix overlaps with any of the fixes proposed in those issues.
@Andre-B sounds like you might be in a good position to look at the existing fixes and maybe consolidate all the efforts into one issue? If we have 5 issues open for it, it sounds like something that people run into with some frequency.
Comment #9
dawehner@lendude+++++++
Thank you for working on this quite tough problem space. Groupby, especially in the context of the generic field handler is always challenging.
I think your setup is a little bit borked.
You group by both name and field_data_test_unlimited and the same time, which results in effectively no grouping. Maybe you want to group just by the second field?
The attached patch just fixes some minor bits.
Comment #10
Andre-B@Lendude: I already looked at the issues mentioned. They are similar but I can not really tell wether they are all caused by the same issue (and if my patch would cover all of them).
#2715483: [regression] getEntity() doesn't always return entity, which results in add comment field not working in Views
#2319005: Notice: Undefined index: [field name] is thrown if relationship entities are missing
#2457999: Cannot use relationship for rendered entity on Views
#2731433 looks like to be the same. but since the steps to reproduce exactly aren't available I opened this one here.
I can move over my findings patch etc. to #2731433: Fatal error when using group by on views or save the hassle, close #2731433 and continue here?
Comment #11
Andre-B@dawehner ain't quiet at it yet. Here are the views I created locally to reproduce the bug. maybe that helps.
see view-with-group-by.txt
Comment #12
lendude@Andre-B yeah lets open this up and maybe close #2731433: Fatal error when using group by on views, this has a fix and test proposed. Setting to needs review to see what the testbot thinks.
Comment #15
Andre-BI think I got it now.
Comment #16
lendudeLooking good. Bit of nitpicking:
This needs a docblock
This could use some explanation about what we test/expect.
Needs a capital at the start and should 'do not' not be 'may not'? They can provide an entity, they just don't always do.
Also a test-only patch would be nice to show the bug in the current HEAD.
Comment #17
dawehnerThe fix looks great for me as well!
Comment #18
Andre-BComment #19
Andre-BComment #21
Andre-Btest only added against 8.1.x. @Lendude did you mean another branch for that one? - not sure if I manage to invest some time before the weekend for that.
Comment #22
lendude@Andre-B test-only against 8.1.x is perfect
If you are still in a rerolling mood, this is now over 80 characters long. But that could just be fixed on commit. Will RTBC if it all comes back green.
Nice work!
Comment #24
Andre-B@Lendude the test only patch failed testing, the other went through just fine. So RTBC? that 80 characters could just be fixed on commit.
Comment #25
lendudeWe got a test, we got a fix. Nice.
Comment #26
alexpottLonger than 80 chars.
Double assignment of the same variable in the same
iflooks a bit fragile. Also I can't see how $this->createEntityForGroupBy() can ever return a bool as its documentation claims... so the whole thing might be easier to understand if it was like this:Comment #27
lendudeFeedback for #26 applied, looks much better indeed. Needs a separate 8.3.x patch so rolled that too.
Comment #28
xjm@dawehner, @tim.plunkett, @alexpott, @cilefen, and I discussed this issue awhile back. As a fatal error that can be triggered with just the UI, this is definitely a major bug.
@tim.plunkett noted that it seems related to #2457999: Cannot use relationship for rendered entity on Views.
Comment #29
dawehnerWe could get rid of 2 lines of code when we predeclare
$build_list = NULLComment #30
lendudeTurn out I missed one in #8, this is a duplicate of #2666166: View with not required relationship and aggregation enabled fails to execute. Work was done on both issues and that is the older issue, so moving this there.
Comment #31
xjm