Problem/Motivation
When creating a new view for displaying nodes, the wizard can throw cause "Notice: Trying to access array offset on value of type null in Drupal\node\Plugin\views\wizard\Node->buildFilters() (line 288 of core/modules/node/src/Plugin/views/wizard/Node.php)."
This happens because the node wizard scans for all taxonomy term reference fields on the selected content type and then checks if any of them have the autocomplete form widget configured for the entity form display:
foreach ($taxonomy_fields as $field_name => $field) {
$widget = $display->getComponent($field_name);
// We define "tag-like" taxonomy fields as ones that use the
// "Autocomplete (Tags style)" widget.
if ($widget['type'] == 'entity_reference_autocomplete_tags') {
$tag_fields[$field_name] = $field;
}
}
The problem is $display->getComponent()
will return null
if the component is hidden on the form display. This causes the next if statement to throw the notice as you're trying to access the 'type' property of NULL.
This notice is new to PHP 7.4.
Note that EntityDisplayInterface::getComponent is already documented that it can return null
:
/**
* Gets the display options set for a component.
*
* @param string $name
* The name of the component.
*
* @return array|null
* The display options for the component, or NULL if the component is not
* displayed.
*/
public function getComponent($name);
Steps to reproduce
- Make sure error logging/notices are configured to be displayed.
- Create a content type with a taxonomy term reference field.
- Configure form display for that content type so the reference field is hidden.
- Visit /admin/structure/views/add to create a new view. Make sure "Content" is selected. You should see the PHP notice.
Proposed resolution
Check that the component actually exists and is not null before trying to use it.
Remaining tasks
User interface changes
N/A
API changes
N/A
Data model changes
N/A
Release notes snippet
N/A
Comment | File | Size | Author |
---|---|---|---|
#35 | 3167733-34-backport-d8.patch | 3.61 KB | vrwired |
#25 | 3167733-25-test_only_should_fail.patch | 2.15 KB | Spokje |
#25 | 3167733-25.patch | 3.65 KB | Spokje |
#25 | interdiff_20_25.txt | 4.14 KB | Spokje |
#7 | after-3167733-6-patch.png | 50.21 KB | surya.s |
Comments
Comment #2
bkosborneProlly needs test but here's the fix.
Comment #3
bkosborneComment #4
larowlanWe triaged this as part of a bug smash initiative call and confirmed this is a valid bug report.
We'll need a test here.
Comment #5
binnythomas CreditAttribution: binnythomas at Axelerant for Drupal India Association commentedI am having the same problem in 8.9.1 as well. I have a taxonomy reference field that is hidden.
Comment #6
vishnukumar CreditAttribution: vishnukumar at Zyxware Technologies commented@bkosborne patch working
Before patch
After patch
I think we can use the
!empty
here. I have created another patch.Comment #7
surya.s CreditAttribution: surya.s at Zyxware Technologies commented@vishnukumar, the given patch works fine.
Comment #8
bkosborneneeds tests as per #4
Comment #10
tfranz CreditAttribution: tfranz commentedI had the same problem with the flag-module:
Core: 9.0.7
Flag: 8.x-4.0-beta2
Error on admin/structure/views/add:
Notice: Trying to access array offset on value of type null in Drupal\node\Plugin\views\wizard\Node->buildFilters() (Zeile 282 in /web/core/modules/node/src/Plugin/views/wizard/Node.php).:
Applying the patch removed the error.
Comment #11
kndrI really don't know what we can test here. The piece of code works as it is expected even if the PHP notice is presented. I tried to find a test case that breaks 'Add view' page functionality. No success. It looks like we have a PHP notice and nothing else. Any help or suggestions?
Comment #12
SpokjeLet's see what TestBot thinks of this test.
Comment #13
SpokjeTest fails without patch, test pass with patch => Needs Review
Comment #14
SpokjeOops...
Comment #17
SpokjeComment #18
SpokjeFixed Coding Standard message and incorrect declaration of
$modules
.Comment #19
SpokjeComment #20
SpokjeFixed Coding Standard message
Comment #21
SpokjeComment #22
kndrThank you @Spokje for your test-only patch. Now I know how to test this kind of bugs :)
I am marking RTBC since:
Comment #23
alexpottThis fix looks good.
Given this is a bug in node module code this test should be in the Node module. See \Drupal\Tests\node\Functional\Views\NodeRevisionWizardTest for example. If we move the code and change the namespace we should be good to go.
Comment #24
SpokjeArgh, should have caught that one myself.
Got a bit frisky and moved this test and
\Drupal\Tests\node\Functional\Views\NodeRevisionWizardTest
to a new foldercore/modules/node/tests/src/Functional/Views/Wizard
and namespaceDrupal\Tests\node\Functional\Views\Wizard
to keep the View Wizard tests (now a whopping number of 2) in\Drupal\Tests\node\Functional\Views
together.Hope this isn't out of scope.
Comment #25
SpokjeRight....
Needs testing against PHP7.4 and I somehow messed up the test-only-fail patch.
Let's try that again...
Comment #26
SpokjeComment #27
alexpottThe logic above here is looping around this already... so we're doing more loops than is really necessary.
We can combine the two by doing...
Can you open a follow-up for this? Shouldn't be part of the change here.
Comment #28
Spokje@alexpott: Ah, TBH: I haven't had a thorough look at the actual fix, just whacked together the Test bit.
But I agree to not get "too loopy", follow-up created here: [PP-1 ]Make bug-fix code for #3167733 (PHP 7.4 notice in views node wizard if a taxonomy field widget is hidden) more efficient
Comment #29
RoSk0Back to RTBC, counting that we have follow up now.
Thanks to everyone!
Comment #30
alexpottCommitted and pushed 9b75b5807b to 9.2.x and bc64f97f83 to 9.1.x. Thanks!
Backported to 9.1.x as a non-disruptive bugfix.
Comment #34
vrwired CreditAttribution: vrwired commentedProviding a backport patch for 8.9.13
Comment #35
vrwired CreditAttribution: vrwired commented