Add a Soft Limit option

Created on 5 February 2025, 3 months ago

Problem/Motivation

Facets 3 allows to add facets as exposed filters, using Ajax. This works very well, but becomes a bit cumbersome if the facets have a lot of options.

Facet blocks can be configured with a "soft limit" so that only the first 5, 10 or 20 items are displayed, while the rest are collapsed. Only when the user clicks on "See all", all items are displayed.
This makes it easier for the user of the site to get a first overview, especially when the facet items are sorted by count. And it makes it much easier to theme, when for example one facet has just a few items, while another has 50 or so...

The maintainers of the Facets module suggested that this should be provided by Better Exposed Filters.

Proposed resolution

Add a configuration option to set a soft limit, similar to that available for Facet blocks.

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

7.1

Component

User interface

Created by

πŸ‡³πŸ‡±Netherlands ifrik

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

Merge Requests

Comments & Activities

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

    I need this feature too.
    As a quick solution I've taken javascript from the facet's module soft-limit.js, modified it to use hardcoded limit, labels, selectors and used it in custom theme. But still need to exclude some filters.

      function softLimit() {
        // Soft limit.
        var limit = 15;
        var zero_based_limit = (limit - 1);
        var facetsList = $('..bef-checkboxes');
    
        // Hide facets over the limit.
        facetsList.each(function () {
          var allLiElements = $(this).find('.form-check');
          $(once('applysoftlimit', allLiElements.slice(zero_based_limit + 1))).hide();
        });
    
        // Add "Show more" / "Show less" links.
        $(once('applysoftlimit', facetsList.filter(function () {
          return $(this).find('.form-check').length > limit;
        }))).each(function () {
          var facet = $(this);
          var showLessLabel = Drupal.t('Show less');
          var showMoreLabel = Drupal.t('Show more');
          $('<a href="#" class="facets-soft-limit-link"></a>')
            .text(showMoreLabel)
            .on('click', function () {
              if (facet.find('.form-check:hidden').length > 0) {
                facet.find('.form-check:gt(' + zero_based_limit + ')').slideDown();
                facet.find('.form-check:lt(' + (zero_based_limit + 2) + ') a, .form-check:lt(' + (zero_based_limit + 2) + ') input').focus();
                $(this).addClass('open').text(showLessLabel);
              }
              else {
                facet.find('.form-check:gt(' + zero_based_limit + ')').slideUp();
                $(this).removeClass('open').text(showMoreLabel);
              }
              return false;
            }).insertAfter($(this));
        });
      }
    
  • πŸ‡³πŸ‡ΏNew Zealand dieuwe Auckland, NZ

    This would be a great feature, one of the few things missing out of the box with Facets 3 integration with BEF.

    The sample JS code there was very helpful for a temporary custom implementation, just a note that "form-item" is the class I needed on my theme as I don't have "form-check" (another option would have been "form-type-checkbox").

  • First commit to issue fork.
  • πŸ‡§πŸ‡ͺBelgium StryKaizer Belgium

    Feature pushed in branch. Needs review
    For now, works for radios/checkboxes widget and links widget.

  • Pipeline finished with Canceled
    14 days ago
    Total: 151s
    #474049
  • πŸ‡§πŸ‡ͺBelgium StryKaizer Belgium
  • Pipeline finished with Success
    14 days ago
    Total: 200s
    #474051
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thanks for working on it! Will need an update hook for the new keys + schema

  • πŸ‡©πŸ‡ͺGermany yannickoo Berlin
  • Pipeline finished with Success
    14 days ago
    Total: 273s
    #474095
  • πŸ‡§πŸ‡ͺBelgium StryKaizer Belgium

    Update hook + config schema added.

  • Pipeline finished with Success
    14 days ago
    Total: 175s
    #474143
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Rock on! I'll put it top of my list for this week, may be able to get a relase out Friday.

  • First commit to issue fork.
  • Pipeline finished with Canceled
    13 days ago
    Total: 333s
    #474786
  • Pipeline finished with Canceled
    13 days ago
    Total: 546s
    #474790
  • Pipeline finished with Success
    13 days ago
    Total: 280s
    #474799
  • πŸ‡ΊπŸ‡ΈUnited States kmonty San Francisco, CA

    The ESLint job is now passing. If I have more time at ddd2025, I'll also strip out jQuery in favor of vanilla ECMA 2020 JS.

  • Pipeline finished with Success
    13 days ago
    Total: 174s
    #474844
  • Pipeline finished with Success
    13 days ago
    Total: 173s
    #474857
  • πŸ‡ΊπŸ‡ΈUnited States kmonty San Francisco, CA

    Fixed all outstanding build issues.

    Resolved a PHPCS failure unrelated to this ticket in πŸ› Resolve PHPCS issues in 7.1.x-dev Active

  • πŸ‡ΊπŸ‡ΈUnited States kmonty San Francisco, CA

    Rolling back my lint fixes at the request of the maintainer.

  • Pipeline finished with Success
    13 days ago
    Total: 272s
    #474989
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Looking good, think we just need test coverage

  • πŸ‡³πŸ‡±Netherlands ifrik

    This is not working for me, neither on my existing site nor on a clean Drupal 11 with just Search API and Facets 3 as additional modules.

    I see the soft limit as an option in the Better Exposed Filters config for my facet, but all items keep on being displayed.

    How have you been testing that yourself? So maybe there's a difference in the config that gets in the way.

  • πŸ‡³πŸ‡±Netherlands ifrik
  • πŸ‡³πŸ‡±Netherlands ifrik

    As just checked: the underscore should be a dash

  • Pipeline finished with Success
    12 days ago
    Total: 168s
    #475831
  • πŸ‡§πŸ‡ͺBelgium StryKaizer Belgium

    underscores issue addressed.
    Still needs tests

  • πŸ‡³πŸ‡±Netherlands ifrik

    Thanks! This works for me now if the filter widget is set to "Links" but not if it's set to display "Checkboxes/Radiobuttons"

  • πŸ‡³πŸ‡±Netherlands ifrik
  • Pipeline finished with Success
    12 days ago
    Total: 240s
    #475961
  • πŸ‡³πŸ‡±Netherlands ifrik

    It now works for "Links" and for "Checkboxes"

  • πŸ‡³πŸ‡±Netherlands ifrik

    Testing it with a hierarchical facets: This creates two links to "show more".

    It would be a better UX if the soft limit would only apply to the top level of hierarchical facets. By selecting one of the top level ones, the user has already committed themselves to drilling down further, and will be clicking on "show more" for the second level anyway.

  • Pipeline finished with Success
    11 days ago
    Total: 184s
    #476505
  • πŸ‡³πŸ‡±Netherlands ifrik

    Perfect!

  • πŸ‡§πŸ‡ͺBelgium StryKaizer Belgium

    Good! It looks like this feature only needs tests now.

Production build 0.71.5 2024