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).
?>
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

leandro713 created an issue. See original summary.

leandro713’s picture

i was on 5.0 version and it worked fine :-/

neslee canil pinto’s picture

Status: Active » Postponed (maintainer needs more info)

@leandro713 not able to reproduce this issue. Can you add steps to reproduce this, please

leandro713’s picture

as i said before, i was using 5.0 with no errors in a exposed form using the «Sort filter options» option (this is critical)
after upgrading to 5.1 version (without doing anything more) i'm gettin the upper stated error.

i'm not making up the error :-)

jacqui.tenderwolf’s picture

I am also getting this error when I select "Sort filter options".

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

I am using version 5.2.0 in Drupal 9.4.3.

LarsLamberti’s picture

Any updates here? More info needed? Still experiencing this, BEF 5.2 & Drupal 9.5.1

greenskin’s picture

Status: Postponed (maintainer needs more info) » Active

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

greenskin’s picture

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.

greenskin’s picture

Title: Undefined array key "" in Drupal\better_exposed_filters\BetterExposedFiltersHelper::sortNestedOptions() (line 186 of modules/contrib/better_exposed_filters/src/BetterExposedFiltersHelper.php). » Undefined array key "" in Drupal\better_exposed_filters\BetterExposedFiltersHelper::sortNestedOptions() line 186
Status: Active » Needs review
StatusFileSize
new679 bytes

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

pragna’s picture

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

I changed code like below:
Before: $parent[$cur_level] = $flat_options[$prev_key];
After: $parent[$cur_level] = $flat_options[$key];

It is solved issue.

esteinborn’s picture

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!

keshavv made their first commit to this issue’s fork.

keshavv’s picture

StatusFileSize
new552 bytes

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

esteinborn’s picture

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.

esteinborn’s picture

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?

smustgrave’s picture

Version: 8.x-5.1 » 6.0.x-dev
Status: Needs review » Postponed (maintainer needs more info)

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

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

If still a problem in 6.0.x please reopen

karenann’s picture

Status: Closed (outdated) » Active

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

karenann’s picture

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.
karenann’s picture

Status: Active » Needs review
StatusFileSize
new583 bytes

Rolling the patch I mentioned in #19.

smustgrave’s picture

Version: 6.0.x-dev » 7.0.x-dev
Status: Needs review » Needs work

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

smustgrave’s picture

Status: Needs work » Postponed (maintainer needs more info)

Can this be confirmed against 7.0.x please

philipprudloff’s picture

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).
smustgrave’s picture

Status: Postponed (maintainer needs more info) » Needs work
smustgrave’s picture

Status: Needs work » Postponed (maintainer needs more info)

Actually need steps to reproduce.

smustgrave’s picture

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

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)
karenann’s picture

For some reason, I wasn't getting alerts of the request for more info.

Since patch #20 has been working for me, honestly, I don't want to bother the maintainers, but thought it'd be good to report back what I've seen.

I'm running Drupal 10.6.3 and better_exposed_filters 6.0.6.

One place I can recreate this is https://www.salemstate.edu/news. In this page, there are two exposed filters on the left; by category and by audience.

The "by audience" is not the problem, the views filter uses the content type's audience field and the field pulls on a flattened taxonomy list.

The "by category" is the problem and here's why, its two-fold. This exposed filter does not use the content type's category field. Instead, it uses "Has taxonomy term" and the vocabulary selected is a nested taxonomy. Both of these factors are required to produce this error in my environment. When I keep "Has taxonomy term" but change the selected Vocabulary to be "audience," which is flat, no error. When I scrap the whole exposed filter field and try directly using the content type's field_category_tags, it calls in the nested taxonomy list without error. (I'm getting mixed results with that test and am out of time to test.)

So, to reproduce this:

  1. use "Has taxonomy term" instead of the content type's field (entity reference w/ type Taxonomy term)
  2. select a vocabulary that has nested terms
  3. in settings for bef, in Exposed Filter Settings > exposed filter > Advanced filter options: check "Sort filter options"