CshsOption could not be converted to string when using Better Exposed Filters

Created on 24 May 2024, 6 months ago
Updated 21 August 2024, 3 months ago

Problem/Motivation

CSHS filters will crash the page when configured to use exposed filter widgets from BEF.

Steps to reproduce

1. Create a View (page display) of nodes with an entity reference field pointing to a taxonomy.
2. Configure the view to have a filter on that taxonomy entity reference field. Use CSHS on this filter.
3. Configure Exposed Form to use Better Exposed Filters.
4. In the exposed form settings, configure the field's filter settings to use either "Links" or "Checkboxes/Radio Buttons".
5. Save the exposed form options and view.
6. View the view.

On step 5, you'll see the first hints of the issue with an error.

On step 6, the page will crash with the following error:

Error: Object of class Drupal\cshs\Component\CshsOption could not be converted to string in Drupal\better_exposed_filters\BetterExposedFiltersHelper::flattenOptions() (line 131 of modules/contrib/better_exposed_filters/src/BetterExposedFiltersHelper.php). 

The issue lies in BEF's BetterExposedFiltersHelper::flattenOptions() which expects the typical Select render element options. Since CSHS replaces option values with the CshsOption object, BEF's flattenOptions falls into the last case (id as key, label as value), where the option value is coerced to a string. CshsOption currently cannot be coerced to a string.

Proposed resolution

Could be done in two ways:
- Allow CshsOption to be coerced into a string by returning the label. This is the quick-n-dirty way, to conform with BEF's logic.
- Refactor CshsOption so that it conforms to the object case of the Select render element (object with an options property). I believe this is "The Right Way" since we already the CshsOption instance.

Remaining tasks

- Review
- Test

User interface changes

- None.

API changes

- Potential change to CshsOption's properties.

Data model changes

- Potential change to CshsOption's properties.

🐛 Bug report
Status

Needs work

Version

4.0

Component

Code

Created by

🇺🇸United States fskreuz

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

Comments & Activities

  • Issue created by @fskreuz
  • Status changed to Needs review 6 months ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.1 + Environment: PHP 8.2 & pgsql-14.1
    last update 6 months ago
    15 pass
  • 🇺🇸United States fskreuz
  • Status changed to Needs work 3 months ago
  • 🇫🇮Finland heikkiy Oulu

    I tested this patch to be able to translate the default All options in the filter but it seems like after this patch is applied all the terms in a vocabulary where terms are in two levels are displayed in a single select list. Without this patch it works as intended and I get two different select elements where I first select the first level term and then the second level term.

    I suspect that the patch might create a regression in the normal select filter but might work in a links / checkboxes version.

    I am marking this as Needs work unless I missed something critical.

Production build 0.71.5 2024