Undefined array key "" in Drupal\better_exposed_filters\BetterExposedFiltersHelper::sortNestedOptions() (line 186 of modules/contrib/better_exposed_filters/src/BetterExposedFiltersHelper.php).

Created on 17 June 2022, over 2 years ago
Updated 21 March 2023, over 1 year ago

recently i did a ยซcomposer updateยป and i got this error:

<?php
Undefined array key "" in Drupal\better_exposed_filters\BetterExposedFiltersHelper::sortNestedOptions() (line 186 of modules/contrib/better_exposed_filters/src/BetterExposedFiltersHelper.php).
?>
๐Ÿ› Bug report
Status

Active

Version

5.1

Component

Code

Created by

๐Ÿ‡ช๐Ÿ‡ธSpain leandro713 ๐Ÿ‡ช๐Ÿ‡ธ Madrid ๐Ÿค 

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States greenskin

    Confirmed, we too get this error. Also seeing it in 6.x.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States greenskin

    Note am seeing this when using an exposed taxonomy field with "Show hierarchy in dropdown" enabled. When this hierarchy setting is disabled I'm not seeing the error.

  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States greenskin

    The issue seems to stem from relying on the existence of the delimiter to determine current level while also not requiring the filter which includes a "- All -" option (code recognizes this as a level 1 when it should be 0 because of starting with "-"). Patch sets the current level to 0 when the key is "All".

  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    37 pass
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.0.7 + Environment: PHP 7.3 & MySQL 5.7
    last update over 1 year ago
    Composer error. Unable to continue.
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    37 pass
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia pragna

    I have applied patch but getting same issue event if I used latest version 6.0.3.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States esteinborn Albany, NY

    Experiencing this issue in 6.0.3.

    Specifically with "show hierarchy" checked. And it doesnt matter what I use as the "All" text, even if i use it without dashes, it still throws this error, and even ignores my "All" text replacement...

    @pragna did you apply the patch AND make that additional change? or only your change?

    Would you be able to roll a patch with your changes so we can RTBC?

    Thanks!

  • First commit to issue fork.
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia keshavv India

    The solution given in #10 works for me. I have created the patch for the same.
    Thank you.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States esteinborn Albany, NY

    When I run $key instead of $prev_key, it appends a hyphen to ALL my taxonomy terms in the list. the original patch is the only one that works for me.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States esteinborn Albany, NY

    writing back here to say the initial patch #9, not the second one does resolve the issue for me in D10 on latest version of BEF 6.0.3.

    can someone else test this so we can RTBC?

  • Status changed to Postponed: needs info 7 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Not able to replicate in 6.0.x are we still seeing this?

  • Status changed to Closed: outdated 7 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    If still a problem in 6.0.x please reopen

  • Status changed to Active 4 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States karenann

    + Drupal core 10.3.1
    + Better Exposed Filters 6.0.6
    + PHP 8.2.21

    I have a nested taxonomy.
    Patch 9 did not help. Patch 13 did, so I looked into why.

    I am getting the error on 202 of src/BetterExposedFiltersHelper.php in Better Exposed Filters 6.0.6

    200      // If we are going down a level, keep track of its parent value.
    201      if ($cur_level > $level) { 
    202        $parent[$cur_level] = $flat_options[$prev_key];
    203      }

    Warning: Undefined array key "" in Drupal\better_exposed_filters\BetterExposedFiltersHelper::sortNestedOptions() (line 202 of modules/contrib/better_exposed_filters/src/BetterExposedFiltersHelper.php).

    I see on line 188, before the iteration, prev_key is set to NULL:
    $prev_key = NULL;

    I also see on line 213, which is within each iteration, that prev_key is set to key:
    $prev_key = $key;

    I'm guessing, though I haven't confirmed, that with $prev_key set to NULL, the first iteration of foreach ($flat_options as $key => &$choice) { } is failing when we try to set $parent[$cur_level] to equal $flat_options[$prev_key] because that equates to setting it to $flat_options[NULL];

    I'm going to tool around with this and will report back.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States karenann

    The following edit seems to help. Basically just does a null test. Would love a set of eyes before I roll a patch to submit. It doesn't do an isset to ensure $flat_options[$prev_key] actually exists, but I feel like that's not necessary.

    diff --git a/web/modules/contrib/better_exposed_filters/src/BetterExposedFiltersHelper.php b/web/modules/contrib/better_exposed_filters/src/BetterExposedFiltersHelper.php
    index 513952c90..934b1e4ae 100644
    --- a/web/modules/contrib/better_exposed_filters/src/BetterExposedFiltersHelper.php
    +++ b/web/modules/contrib/better_exposed_filters/src/BetterExposedFiltersHelper.php
    @@ -199,7 +199,7 @@ public static function sortNestedOptions(array $options, $delimiter = '-') {
     
           // If we are going down a level, keep track of its parent value.
           if ($cur_level > $level) {
    -        $parent[$cur_level] = $flat_options[$prev_key];
    +        $parent[$cur_level] = ($prev_key ? $flat_options[$prev_key] : $prev_key);
           }
     
           // Prepend each option value with its parent for easier sorting.
  • Status changed to Needs review 4 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States karenann

    Rolling the patch I mentioned in #19.

  • Status changed to Needs work 4 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Please open an MR and check if an issue against 7.0.x

  • Status changed to Postponed: needs info 3 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Can this be confirmed against 7.0.x please

  • I'm still seeing this on 7.0.1 with the error

    Warning: Undefined array key "" in Drupal\better_exposed_filters\BetterExposedFiltersHelper::sortNestedOptions() (line 202 of modules/contrib/better_exposed_filters/src/BetterExposedFiltersHelper.php).
    
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Actually need steps to reproduce.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Following up for steps to reproduce else going to close this one out.

  • Status changed to Closed: outdated 8 days ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave
Production build 0.71.5 2024