Problem/Motivation

View exposed filter displays an error with a term ID multiple select and group filter.

The website encountered an unexpected error. Please try again later.
TypeError: Illegal offset type in isset or empty in Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid->validateExposed() (line 366 of core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php).
Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid->validateExposed(Array, Object) (Line: 169)
Drupal\views\Form\ViewsExposedForm->validateForm(Array, Object)

Steps to reproduce

Create view follow steps below:
- Node type Article have reference field to the taxonomy term "Tags"
- Create a view to show Article
- Add a Tid filter
- Select Grouped filters
- Allow multiple selections
- Input group label and select the value in the group table

Test view I exported: views.view_.test_exposed_form_tid_grouped_filter.yml

Screen record: https://www.youtube.com/watch?v=FQ1nnpWHkko

Proposed resolution

Detect if an array is returned if so:
- filter out any empty values, and check that there is something in the array.
- extract the equivalent value from the group_info -> group_items $input and concatenate into $this->validated_exposed_input

core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php:348

if ($this->options['is_grouped']) {
      if (is_array($input)) {
        $filtered_inputs = array_filter($input);
        if ($filtered_inputs) {
          $values = [];
          foreach ($filtered_inputs as $filtered_input) {
            $values += $this->options['group_info']['group_items'][$filtered_input]['value'];
          }
          $this->validated_exposed_input = array_values($values);
          return;
        }
      }
      elseif (isset($this->options['group_info']['group_items'][$input])) {
        $this->validated_exposed_input = $this->options['group_info']['group_items'][$input]['value'];
        return;
      }
    }

Issue fork drupal-3357973

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

somersoft created an issue. See original summary.

somersoft’s picture

StatusFileSize
new1.47 KB
smustgrave’s picture

Version: 9.4.x-dev » 10.1.x-dev
Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests, +Needs issue summary update

Issue summary should be updated with proposed solution. "Apply patch" doesn't count unfortunately.

Also as a bug it will need a test case

Moving version to 10.1 as 9.4 is only under security coverage.

luke.stewart’s picture

Summarised patch and added to the proposed solution. Removed the needs issue summary update.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

phthlaap’s picture

Issue summary: View changes
StatusFileSize
new7.28 KB

phthlaap changed the visibility of the branch 3357973-grouped-exposed-taxonomy to hidden.

phthlaap changed the visibility of the branch 3357973-grouped-exposed-taxonomy to active.

phthlaap’s picture

phthlaap’s picture

Issue summary: View changes
phthlaap’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

Ran test-only feature and showed you do get the WSOD https://git.drupalcode.org/issue/drupal-3357973/-/jobs/1383399 removing that tag

Manually testing following the steps and confirmed the issue

Just FYI, not a problem, but don't need to include the code in the proposed solution. Explaining the solution usually work. Again not a problem if you do just mentioning.

LGTM

quietone’s picture

Status: Reviewed & tested by the community » Needs work

So, we have updates to the issue summary. Thank you!

The test-only tests have been run and there has been manually tested. That is all great. But what is missing is a code review. I left some notes in the MR.

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

weddled’s picture

I am having his issue with Drupal 10.2.5. Add a filter to a views and use taxonomy term select expose filter, grouped, allow multiples (vocabulary). Get the error: TypeError: Illegal offset type in isset or empty in Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid->validateExposed() (line 349 of /opt/drupal/web/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php). Any updates on this?

pcambra’s picture

When saving some of these views, there's an error caused by trusted config

pcambra’s picture

I think this is affecting other filters that can be grouped

TypeError: trim(): Argument #1 ($string) must be of type string, array given in trim() (line 1683 of /var/www/html/docroot/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php).
pcambra’s picture

Title: Grouped exposed taxonomy term filters does not work when input from user is an array. » Grouped exposed filters do not work when input from user is an array.
Component: taxonomy.module » views.module
pcambra’s picture

pcambra’s picture

I'm seeing a lot of duplication for the same issue

pcambra’s picture

I've removed the commit from #21 as this issue is already handled in #2920039: Views' User Name exposed group filter validation

However, not sure which one is the best option to keep as this is dupe from #2576927: Grouped exposed taxonomy filter fails validation for autocomplete widget

baluertl’s picture

Title: Grouped exposed filters do not work when input from user is an array. » Grouped exposed filters do not work when input from user is an array

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.