Hi there,

I'm trying to build a search API view that lists data in a table, with a map attachment on the top. I've been unable to get the attachment to respect the search results (based on a facet that I am using to search with). For instance, lets say I have content with a field called "state". If I build a list of facets (as a block) and then also have a view, I can successfully get the facet selection to limit the search correctly for the main page. However, if I am trying to put an attachment there to also map the results (or anything, really -- I've even tried just an unsorted list to eliminate maps being a problem), the search does not seem to reflect in this attachment. I've also tried a block on the same page as the view, with similar results.

Is there any way to have an attachment to the view respect a search when someone chooses a facet?

Thanks for the help.

Issue fork facets-3116236

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

whitelancer created an issue. See original summary.

hmdnawaz’s picture

Hey there

I am also facing the issue.

Is there any solution for that?

vaccinemedia’s picture

Me too! If you find a solution please post it here! I have the added complexity that I’m trying to combine facets with exposed filters, in my case facets for taxonomy terms and distance / proximity search for a given address

drunken monkey’s picture

Project: Search API » Facets
Issue tags: -views, -attachment, -blocks, -facets

Thanks for reporting this problem, and sorry for taking a while to reply!

Yes, I see how this might be a use case, and what the problem is: as attachments execute their own search request (even though it might be identical to the one for the main display), and facets are configured to just apply to a single display, the attachment’s search request doesn’t get the filters added.
I’m not sure how to best resolve this, but I think this is more of a Facets problem than a Search API problem. I’m therefore moving this issue, in the hope that one of the Facets maintainers has a clearer idea of what can be done. Basically, we’d need to detect this case (attachment display’s search request being executed) in facets_search_api_query_alter() and add the filters (but not the facets options) from the main display to the query. Seems like something that would be tricky to implement in the current architecture, but maybe there is a clean way of doing this.

(Needless to say, this should in any case be optional behavior. Ideally, probably, this would be configurable right in the attachment display, under “Inherit exposed filters”, but that’s likely to be even harder to do than the functionality itself.)

PS: It seems you (like many others – it's really easy to misinterpret) are confused by the "Issue tags" field. As the guidelines state, they aren't meant for free text tags related to the issue, but only for specific categorization purposes, usually by module maintainers.
So, if you aren't sure your current usage is correct, please just leave the field empty.

hmdnawaz’s picture

Component: Views integration » Code

I have somehow manage it to get it working. Here is my code.

function mymodule_search_api_query_alter(QueryInterface &$query) {
  if ($query->getIndex()->getServerInstance()->supportsFeature('search_api_facets')) {
    /** @var \Drupal\facets\FacetManager\DefaultFacetManager $facet_manager */
    $facet_manager = \Drupal::service('facets.manager');

    $search_id = $query->getSearchId();

    // If we find an attachment view query, we use the same query alter
    // as the page because they belong together.
    if (strpos($search_id, 'views_attachment:search__') === 0) {
      $search_id = 'search_api:views_page__search__page_1';

      // Add the active filters.
      $facet_manager->alterQuery($query, $search_id);
    }
  }
}
saa45’s picture

Hello Hmdnawaz. Where did you put this code to get it working?

hmdnawaz’s picture

Hello @saa45

In .module file.

letrollpoilu’s picture

I think this thread is related to this one: https://www.drupal.org/project/facets/issues/2826650#comment-13706366

I also posted a comment there as it is not working for me.

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

attisan’s picture

Status: Active » Needs review
attisan’s picture

added a views display extender (hint; you need to activate it manually in the advanced views settings) for attachments. with the extender enabled there is an option to select facets that should alter the query. though I'm not exactly "super-happy" with this approach it does work and provide some degree of flexibility (implementation leans on the way views data export handles facets).

attisan’s picture

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

Hi, aattisan, thank you for working on this. I have activated the views display extender option, but am not seeing where I can select facets that should alter the query. Where can I find this?

Thank you in advance!

tgoeg’s picture

Took me a while to find where to enable the extender: Go to admin/structure/views/settings/advanced and at the "Display extenders" section select the new extender.

You need to add an attachment to your view, then there is an additional option down in the second column of the view config.

It however did not solve my issue.

When I change the term in the text search box and click on another checkbox of the facet, the old value of text search box sent before gets sent with the AJAX request (so the text in the text search box gets reset, which is unexpected). I am unsure if this is the OP's problem and is this supposed to be fixed by the MR above?
That is to say, it does not fix it for me (though I don't use any attachment at all, the facets are placed with views_block_area in the header, so unsure whether this is actually the thing I'd need, here. But OP mentioned this happens with blocks as well, so I thought this might be the same problem.)

See attached file https://www.drupal.org/files/issues/2022-09-14/changed_search_reset_when... for a short demo.

Please excuse me if this is something unrelated. Didn't find anything better suited in the issue queue.

john.oltman’s picture

StatusFileSize
new6.59 KB

The merge commit 4826fb22 in MR 89 worked for me, attaching as a patch here if anyone needs it while waiting for this to be merged in. In addition to the patch, follow the instructions in #15 for enabling the view display extender in Views Advanced Settings and then selecting the appropriate facet source in your view config.

tgoeg’s picture

Can @attisan or @drunken-monkey please advise as to whether this is supposed to fix the issue outlined in my screen recording or this is a different issue I should create a separate ticket for?
Thanks a lot in advance!

attisan’s picture

@tgoeg. it "kind of" should. When I find some time, I'll look into it. atm my patch in the MR works, but won't respect uncommitted changes to the search form (uncommitted as in, not already posted) - instead it allows for "sharing" facet with views attachments. I do see your point though.

tgoeg’s picture

Thanks a lot!
That means it won't fix my current issue for now, as I only have the documented shortcoming, all actually posted properties (by previous search requests) do work as they should, also with facets.
I'll wait and am willing to test anything you might provide - thanks in advance, much appreciated!

batalhasamuel’s picture

@john.oltman and @attisan I tested the patch from #16 and it worked partially. If the result exists in both View and Attached View, the facet shows the filter option and filters considering the two parts. But if the filter has an option that only has results for the view, the facet filter option is not shown.
To add more information I'll give a real example: I have a view with a Live event list, and an Attached View with a Recorded event list.
If both lists have events in the French language, the filter option French appears and works for both. If the French language is only present in the attached view from the Recorded event list, the filter option is not shown to me because the rule from facet settings that handle the empty filter result behavior is just considering the View, not the Attachment.

Can someone help me with this? I couldn't go further than this yet.

thiagomoraesp’s picture

Same problem as Samuel ( #20 ) here, someone can help?

strykaizer’s picture

Status: Needs review » Reviewed & tested by the community

concerning #20.

For building facet items, we need 1 single query to retrieve the possible filter values.
Since you have different filters in your attachment, that wont technically be possible as these are 2 different queries to retrieve your resultset.

This is something which facets will probably never support, as it will get complicated very fast if we merge filters from different sources.

Patch from #16 works fine for my use-case, where the attachment renders the first result in a different layout (and other results have offset 1)

borisson_’s picture

This looks great, but I think we should probably add a test or it also?

idebr’s picture

Title: Views blocks/attachments with facet search do not respect search » Add facet search support to Views blocks/attachments
Category: Support request » Task
Status: Reviewed & tested by the community » Needs work
  1. The views Facets display extender needs a config schema. See the metatag module for a reference: https://git.drupalcode.org/project/metatag/-/blob/8.x-1.x/metatag_views/...
  2. Needs a test per #23
effortdee’s picture

Any more updates on this? I'm looking at controlling an attachment map on a views page I have built and for facets to control both the page and the attachment map.

dbrowncode’s picture

This doesn't seem to work; with the MR applied as a patch, and the extender enabled, I'm still getting the No facet source defined for facet errors when I try adding an attachment with the same facets as its parent. Related issue: https://www.drupal.org/project/facets/issues/3505873

dbrowncode’s picture

asilva3’s picture

In order to bypass the issue, I did the following:

  • I went to /facets/src/Entity/Facet.php L492 and changed the line from
    throw new Exception("No facet source defined for facet.");
    to
    \Drupal::logger('facets')->warning('Facet @id had no source; falling back to search_api_string.', ['@id' => $this->id()]);
     return 'search_api_string';
  • Reload the page, make edits to save attachment view so it generates a facet source for the display.
strykaizer’s picture

Views attachments support + the error is fixed for Facets exposed filters in #3505873: Error when adding new display to view

thefancywizard’s picture

I took an approach similar to what was suggested in comment #24 and added an explicit config schema definition for the Facets Views display extender since Config Inspector was flagging this on my site.

This patch introduces facets.views.schema.yml and defines schema for views.display_extender.facets_display_extender including the facet_settings sequence used by the display extender configuration.

This resolves schema validation warnings (e.g. in Config Inspector) without changing any runtime behavior.

The patch only adds schema definitions and does not alter existing configuration or functionality.