Problem/Motivation
I have two content types (Book
and Page
) and a taxonomy vocabulary (Notes
). Notes
vocabulary has an image field (field_image
). Page
content type has an entity reference field (field_note
) that references terms in Notes
vocabulary. Book
content type has an entity reference field (field_page
) that references nodes of type Page
.
I created a view that displays content of type Book
. I also added two relationships: field_pages
that appears in Book
and field_note
that appears in Page
. (field_note
relationship uses field_pages
relationship because notes are attached to pages).
I also turned on an option to display all messages with backtrace information in Logging and errors
configuration panel.
Then I added the following content:
- A term "First note" to
Notes
vocabulary and attached an image to it - A node of type
Page
and referenced a term "First note" to it - Two nodes of type
Page
without term references. - A node of type
Book
with three references to nodes of typePage
.
When the view page is displayed, I get an error:
Notice: Undefined index: field_note in Drupal\views\Plugin\views\field\FieldPluginBase->getEntity() (line 395 of core/modules/views/src/Plugin/views/field/FieldPluginBase.php).
Drupal\views\Plugin\views\field\FieldPluginBase->getEntity(Object)
Drupal\field\Plugin\views\field\Field->getItems(Object)
...
Proposed resolution
getEntity()
method in FieldPluginBase
class does not check of relationship existence therefore if a node of type Book
is displayed and if a referenced node of type Page
does not have a reference to term in Notes
vocabulary, NULL
is returned and an undefined index error is thrown.
getEntity()
method in FieldPluginBase
class should check whether relationship exists and return NULL if it doesn't.
Related Issues
#2133471: Notice: Undefined index: uid in simple a user view is related to this issue in sense that variable existence check is not performed.
Comment | File | Size | Author |
---|---|---|---|
#13 | undefined-index-2319005-13.patch | 661 bytes | maijs |
#9 | undefined-index-2319005-9.patch | 661 bytes | jyotisankar |
#4 | d8-views-undefined-index-in-relationship-2319005-4.patch | 663 bytes | adci_contributor |
#1 | d8-views-undefined-index-in-relationship-2319005.diff | 1.21 KB | maijs |
Comments
Comment #1
maijs CreditAttribution: maijs commentedA patch against 8.0.x branch (commit d21c9a4) is attached.
Comment #2
maijs CreditAttribution: maijs commentedComment #3
jhedstromPatch no longer applies.
Comment #4
adci_contributor CreditAttribution: adci_contributor commentedTrying to reroll.
Docblock is '@inheritdoc' now, so I didn't touch it.
Comment #5
dawehner@adci_contributor
Can you please login with your personal drupal.org account?
In general I think this patch should not be applied, because even it might fix the symptom I doubt that this fixes the underlying problem.
The question we have to ask is: why is the relationship value not set here?
Comment #6
adci_contributor CreditAttribution: adci_contributor commented@dawehner actually I'm already logged on with my personal account. :)
Comment #7
jhedstromAs I understand the issue, this occurs when a relationship is optional.
Comment #8
jyotisankar CreditAttribution: jyotisankar commentedI am working on it
Comment #9
jyotisankar CreditAttribution: jyotisankar commentedComment #10
David Hernández CreditAttribution: David Hernández commentedHello!
Thank you for working on this issue!
We should all try and use the same sprint tag. According to https://groups.drupal.org/node/447258 it should be SprintWeekend2015 with no #.
Comment #11
David Hernández CreditAttribution: David Hernández commentedComment #12
manningpete CreditAttribution: manningpete commentedPatch applies.
Comment #13
maijs CreditAttribution: maijs at Wunder commentedThis issue still persists and is very annoying. The patch has been updated.
Comment #14
mikgreen CreditAttribution: mikgreen commentedWorks as advertised.
I've had to enable error logging to see notices.
They are gone after applying patch.
Comment #17
maijs CreditAttribution: maijs commentedQA testbot failure with applying the patch in #15 seems to be a mistake on testbot part. Having done manual testing and new "passed" status, setting back to RTBC.
Comment #18
maijs CreditAttribution: maijs commentedComment #19
alexpottLooks like a we could write a test to cover this.
Comment #21
dawehnerI'm 200% sure this is a duplicate of an existing issue out there, but I cannot find it.
#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
all also sounds like duplicates to this non findable issue.
Comment #32
smustgrave CreditAttribution: smustgrave at Mobomo commentedClosing this out as it appears to have been already added to core.
Comment #33
smustgrave CreditAttribution: smustgrave at Mobomo commentedComment #34
xjmPer discussion with @smustgrave and @quietone, we're not sure that #32 is the case.