Improve Empty facet behavior.

Created on 31 May 2024, 6 months ago
Updated 3 June 2024, 6 months ago

Problem/Motivation

When 'Display text' is selected for the 'Empty facet behavior' in the facet's settings, there is no Title of the facet and only the empty results text is printed.

Steps to reproduce

Configure a facet to display text when empty results.
Go to page where there is no options for the facets and see that the facet widget contains only the empty results text, without title.

Proposed resolution

Improve code of the FacetsForm to fix this issue.

✨ Feature request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡¦Ukraine khiminrm

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

Comments & Activities

  • Issue created by @khiminrm
  • πŸ‡ΊπŸ‡¦Ukraine khiminrm

    Attaching a quick patch which helped me to fix the issue when using the facets_form_checkbox widget.

  • πŸ‡ΊπŸ‡¦Ukraine khiminrm

    btw, the code which builds the container with the empty text can be found in Drupal\facets\FacetManager\DefaultFacetManager::build() under the comment

    // No results behavior handling. Return a custom text or false depending on
    // settings.
  • πŸ‡ΊπŸ‡¦Ukraine khiminrm

    Maybe this should be on the widget plugin base e.g. add it's own custom config similar to the Facet's config. And use it instead there.

  • πŸ‡ΊπŸ‡¦Ukraine khiminrm

    I've created custom CheckboxesWidget plugin and used such code:

        if (!empty($this->processedItems)) {
          $content = [
            '#type' => 'checkboxes',
            '#options' => $options,
            '#default_value' => $default_value,
            '#after_build' => [[static::class, 'indentCheckboxes']],
            '#depths' => $depths,
            '#ancestors' => $ancestors,
            '#indent_class' => $this->getConfiguration()['indent_class'],
          ];
        }
        else {
          $content = [
            '#type' => 'container',
            '#attributes' => [
              'data-drupal-facet-id' => $facet->id(),
              'class' => ['facet-empty'],
            ],
            'empty_text' => [
              '#markup' => $this->t('No applicable options available.'),
            ],
          ];
        }
        return [
          $facet->id() => [
            '#type' => 'fieldset',
            '#title' => $facet->getName(),
            '#attributes' => [
              'data-drupal-facets-form-ancestors' => Json::encode($ancestors),
            ],
            $facet->id() => $content,
          ],
        ];
Production build 0.71.5 2024