I would like to propose a new processor to merge facets together. While I was working in a case where I had to do it, I walked throught different places however I didn't see a generic solution provided out-of-the-box from Facets module per se.

Then I came with my solution and I would like to share here. The code I wrote I have some details here: https://www.keboca.com/drupal-8-merge-facets-together

Plus it is possible it would help as well on the following closed tickets:
How can I merge combine facets in drupal 8 Search API and Facets
Trying to merge multiple content types into a single facet via a facet processor plugin
How can I merge or combine facets?

I'm leaving here a path which allows to create facet groups based on current content types.

Issue fork facets-3133309

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:

    Support from Acquia helps fund testing for Drupal Acquia logo

    Comments

    keboca created an issue. See original summary.

    jacobbell84’s picture

    This is a great idea, I needed this exact functional recently. I almost wonder if we could make this more generic....at the end of the day, this is really about grouping facets; I don't see that it makes a huge difference if those facets are node types vs other facets. The UI would need to be adjusted, but the underlying logic should be the same.

    In the mean time, I did some testing on the patch. There's a few issues that I attempted to correct:

    1. The 'sitewide' query string parameter was hard coded in the patch, but the query string parameter can be anything. Instead it should read from the URL Processor's getFilterKey function to get the query string parameter name.
    2. the 'content_types' machine name was hard coded, but that could be any value. Instead it should read from $facet->getUrlAlias() function to retrieve that machine name
    3. The way the dependency injection was being handled a little non-standard. I tweaked it to conform more to the standard create/__construct approach vs having a setting function for each dependency.

    The above changes are working for my use case, but should hopefully work for everyone.

    jacobbell84’s picture

    Status: Active » Needs review

    The last submitted patch, facet_processor_merge_node_types-1.patch, failed testing. View results
    - codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

    Status: Needs review » Needs work

    The last submitted patch, 2: facet_processor_merge_node_types-3133309-1.patch, failed testing. View results

    mandclu’s picture

    Status: Needs work » Reviewed & tested by the community

    Works perfectly for the intended use case.

    idebr’s picture

    Status: Reviewed & tested by the community » Needs work

    The automated tests results report failures and Drupal code style violations. Can you make sure the results in the automated tests are fixed first?

    madhaze’s picture

    Status: Needs work » Needs review
    FileSize
    11.37 KB
    641 bytes

    I made a update to the patch to support translations on the new Facet name when displayed.

    Status: Needs review » Needs work
    madhaze’s picture

    First patch #8 was missing the translation function. This one has it corrected.

    esterc’s picture

    This merge tool is useful but it has issues if more than one Facet group is added, it only shows first group. Anyone has a fix for this?

    dxvargas’s picture

    I've tried patch #10 but in the end I didn't use this processor.

    For two reasons:
    # It only works with nodes (I would need it to work with groups);
    # It doesn't work with Facets form (I've tested with the checkboxes).

    I don't consider this critical but it would be nice to have it addressed.

    jiong_ye’s picture

    jiong_ye’s picture

    FileSize
    11.41 KB

    renamed the patch

    mgaskey’s picture

    FileSize
    11.39 KB

    Re-rolled the patch in #14 to improve the language used in comments and the UI. The functionality works great in the original patch.

    pcambra’s picture

    Version: 8.x-1.x-dev » 3.0.x-dev
    thomps9012’s picture

    FileSize
    2.34 KB

    For context we're using version 2.0.6 of facets, solr / solr cloud, and json:api search index.

    Here's what we're seeing for the url of our merged node facet (in the metadata section of our json:api response):

    https://base_url.com/jsonapi/index/solr_node_index?filter[facet_group]=facet_item&filter[0]=facet_group:facet_item&filter[1]=facet_group:facet_item
    

    Which is not a clickable link.

    Our patch corrects the generated url to the below:

    https://base_url.com/jsonapi/index/solr_node_index?filter[facet_merged_name][condition][path]=facet_group&filter[facet_merged_name][condition][value][0]=facet_item_1&filter[facet_merged_name][condition][value][1]=facet_item_2&filter[facet_merged_name][condition][value][2]=facet_item_3
    

    Which is a clickable link

    Hopefully this patch helps resolve this issue for some others, if not

    thomps9012’s picture

    FileSize
    10.76 KB

    FYI the interdiff is in the previous comment and file 3133309-18.patch attached to this comment is the actual patch

    and 20 resolves an issue with passing the translate object as a query parameter

    thomps9012’s picture

    FileSize
    10.86 KB

    Resolution to patch 19 which was attempting to pass the translate object as a query parameter

    mkalkbrenner’s picture

    We already have a combine facets processor in 3.0.x. doesn't it solve the requirements here?

    pcambra’s picture

    @mkalkbrenner the use case here is a common requirement by clients to group content types (and other fields) together in the same value for filtering, for example to produce a checkbox widget like so:

    [ ] Latest
    [ ] Articles
    [ ] Documents

    Where Latest are from content types blog and news, Articles are from content type article and page, and Documents are from document and file content type (just a quick example).

    This can be done either at index time with a computed field or something, or at search time, with the work on this patch, I don't think it's the same as combining facets.