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

  1. Make sure error logging/notices are configured to be displayed.
  2. Create a content type with a taxonomy term reference field.
  3. Configure form display for that content type so the reference field is hidden.
  4. 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

CommentFileSizeAuthor
#35 3167733-34-backport-d8.patch3.61 KBvrwired
#34 3167733-34-backport-d8.patch3.61 KBvrwired
#25 3167733-25-test_only_should_fail.patch2.15 KBSpokje
#25 3167733-25.patch3.65 KBSpokje
#25 interdiff_20_25.txt4.14 KBSpokje
#24 interdiff_20_24.txt4.14 KBSpokje
#24 3167733-24-test_only_should_fail.patch2.15 KBSpokje
#24 3167733-24.patch3.65 KBSpokje
#20 interdiff_12_20.txt510 bytesSpokje
#20 3167733-test_only_should_fail.patch2.05 KBSpokje
#20 3167733-20.patch2.8 KBSpokje
#19 interdiff_12_18.txt724 bytesSpokje
#18 3167733-test_only_should_fail.patch2.05 KBSpokje
#18 3167733-18.patch2.8 KBSpokje
#18 3167733-test_only_should_fail.patch2.05 KBSpokje
#14 3167733-test_only_should_fail.patch2.05 KBSpokje
#14 interdiff_12_13.txt590 bytesSpokje
#14 3167733-13.patch2.8 KBSpokje
#12 3167733-test_only_should_fail.patch2.06 KBSpokje
#12 3167733-12.patch2.8 KBSpokje
#7 after-3167733-6-patch.png50.21 KBsurya.s
#7 before-3167733-6-patch.png69.56 KBsurya.s
#6 interdiff_2-6.txt662 bytesvishnukumar
#6 3167733-6.patch764 bytesvishnukumar
#6 after-3167733.png78.73 KBvishnukumar
#6 before-3167733.png323.66 KBvishnukumar
#2 3167733.patch756 bytesbkosborne
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bkosborne created an issue. See original summary.

bkosborne’s picture

Status: Active » Needs review
FileSize
756 bytes

Prolly needs test but here's the fix.

bkosborne’s picture

Issue summary: View changes
larowlan’s picture

We triaged this as part of a bug smash initiative call and confirmed this is a valid bug report.

We'll need a test here.

binnythomas’s picture

I am having the same problem in 8.9.1 as well. I have a taxonomy reference field that is hidden.

vishnukumar’s picture

@bkosborne patch working
Before patch
before patch

After patch
After patch

I think we can use the !empty here. I have created another patch.

surya.s’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
69.56 KB
50.21 KB

@vishnukumar, the given patch works fine.

bkosborne’s picture

Status: Reviewed & tested by the community » Needs work

needs tests as per #4

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

tfranz’s picture

I 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.

kndr’s picture

I 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?

Spokje’s picture

Spokje’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

Test fails without patch, test pass with patch => Needs Review

The last submitted patch, 14: 3167733-13.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 14: 3167733-test_only_should_fail.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Spokje’s picture

Assigned: Unassigned » Spokje
Spokje’s picture

Spokje’s picture

FileSize
724 bytes
Spokje’s picture

Spokje’s picture

Assigned: Spokje » Unassigned
Status: Needs work » Needs review
kndr’s picture

Status: Needs review » Reviewed & tested by the community

Thank you @Spokje for your test-only patch. Now I know how to test this kind of bugs :)

I am marking RTBC since:

  1. #20 patch applies cleanly.
  2. Tests pass.
  3. #20 test-only patch fails as expected.
  4. No coding standard problems were found by the test bot.
  5. Code is clear and addresses the issue from the issue summary.
  6. Feedback from previous comments has been addressed.
  7. Issue title is clear and relevant.
  8. Issue metadata looks okay.
  9. Manually tested and patch works as expected. See screenshots at #6. I can confirm the same result for the #20 patch.
alexpott’s picture

Status: Reviewed & tested by the community » Needs work

This fix looks good.

+++ b/core/modules/views/tests/src/Functional/Wizard/HiddenTaxonomyTermReferenceFieldTest.php
@@ -0,0 +1,62 @@
+use Drupal\Core\Field\FieldStorageDefinitionInterface;
+use Drupal\field\Entity\FieldConfig;
+use Drupal\field\Entity\FieldStorageConfig;
+
+/**
+ * Tests node wizard and content type with hidden Taxonomy Term Reference field.
+ *
+ * @group Views
+ * @group node
+ */
+class HiddenTaxonomyTermReferenceFieldTest extends WizardTestBase {

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.

Spokje’s picture

Given this is a bug in node module code this test should be in the Node module.

Argh, should have caught that one myself.

Got a bit frisky and moved this test and \Drupal\Tests\node\Functional\Views\NodeRevisionWizardTest to a new folder core/modules/node/tests/src/Functional/Views/Wizard and namespace Drupal\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.

Spokje’s picture

Right....

Needs testing against PHP7.4 and I somehow messed up the test-only-fail patch.

Let's try that again...

Spokje’s picture

Status: Needs work » Needs review
alexpott’s picture

+++ b/core/modules/node/src/Plugin/views/wizard/Node.php
@@ -279,7 +279,7 @@ protected function buildFilters(&$form, FormStateInterface $form_state) {
         $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') {
+        if (!empty($widget) && $widget['type'] == 'entity_reference_autocomplete_tags') {
           $tag_fields[$field_name] = $field;
         }

The 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...

      $tag_fields += array_filter($this->entityFieldManager->getFieldDefinitions($this->entityTypeId, $bundle), function (FieldDefinitionInterface $field_definition) use ($display) {
        if ($field_definition->getType() == 'entity_reference' && $field_definition->getSetting('target_type') == 'taxonomy_term') {
          $widget = $display->getComponent($field_definition->getName());
          return isset($widget['type']) && $widget['type'] == 'entity_reference_autocomplete_tags';
        }
        return FALSE;
      });

Can you open a follow-up for this? Shouldn't be part of the change here.

Spokje’s picture

@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

RoSk0’s picture

Status: Needs review » Reviewed & tested by the community

Back to RTBC, counting that we have follow up now.

Thanks to everyone!

alexpott’s picture

Version: 9.2.x-dev » 9.1.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 9b75b5807b to 9.2.x and bc64f97f83 to 9.1.x. Thanks!

Backported to 9.1.x as a non-disruptive bugfix.

  • alexpott committed 9b75b58 on 9.2.x
    Issue #3167733 by Spokje, vishnukumar, bkosborne, surya.s, kndr,...

  • alexpott committed bc64f97 on 9.1.x
    Issue #3167733 by Spokje, vishnukumar, bkosborne, surya.s, kndr,...

Status: Fixed » Closed (fixed)

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

vrwired’s picture

Providing a backport patch for 8.9.13

vrwired’s picture

FileSize
3.61 KB