Hey, so this module's facet resource Drupal\facets\Plugin\facets\facet_source\SearchApiViews only supports one views with one display. Is it possible to support multiple displays for one view in the same time?
So I have a search page to search jobs using facets blocks search. For the searched job results, we need to display a block of featured jobs above the regular job results, just look like this page https://jobs.drupal.org/. The featured jobs block should share the same facets search filters as the regular jobs block.
I created a views, and a block display to be used the facets resource. And put the facets blocks, and this views' display block in the same page. Now the problem comes to how to construct the featured jobs block above the regular jobs block.
I once thought I can create an attachment block display for this views, but this attachment block won't be filtered when facets search has been triggered. The gets back to this class Drupal\facets\Plugin\facets\facet_source\SearchApiViews. It only support one display inside the views. I played with it a bit, and haven't been able to figure it out yet. Anyone can help? Thanks!
| Comment | File | Size | Author |
|---|---|---|---|
| #26 | Selection_061.png | 101.42 KB | letrollpoilu |
Comments
Comment #2
borisson_We're currently rewriting the facet sources in #2772745: Search API integration doesn't check/define feature support of backends, however even with that work - I don't think we actually tested a block and a page controlled by the same facet before. I don't think that would work out of the box anyway. I think you're going to need to do a search api query alter and apply the same query arguments to the query as that are applied to the other query
Comment #3
smiletrl commentedThanks borisson_. I was having issue to alter the query, and now this problem has been solved.
Comment #5
ayalon commentedThe main issue is, that views attachment are not supported at all eventhough they belong normally to a page view.
I did it like that:
I check for the name of the attachment view and then replace it with the related search_id of the views page.
Comment #6
valgibson commentedI stumbled upon the same problem and used the custom code in comment #5, to alter my attached view. But as I found out: in newer versions the $search_id string notation has changed. E.g. $search_id should be 'search_api:views_page__search__page'.
Comment #7
friera commentedThat works for me. It would be great to use the same facet for different displays.
Comment #8
borisson_@friera: it was a design decision to not do that, we wanted to allow different configuration per display and that's needed as well (can't use dropdown widget on a rest view, can't use a search api adapter for core search facets, ...). So that won't change, certainly not in the 1.x branch. If we'd ever want to do that, we'd have to do a huge refactor of search api + facets and I don't see that happening in the near future.
Sorry, but the custom code pasted in #6 by @valgibson is the best solution for this problem.
Comment #9
jpascoal commentedHello everyone! I have the same problem. I'm kind of a newbie in Drupal.
Where can I put this code posted in the comment #6? In which file?
Thanks in advance!
Comment #10
smiletrl commented@jpascoal, you may take a look at https://www.drupal.org/docs/8/creating-custom-modules and https://www.drupal.org/docs/8/creating-custom-modules/a-practical-guide-....
So what you may do is
I didn't expect so many people have similar issues. Anyway, here's my code to fix this issue originally.
The search id might need to be changed to something like 'search_api:views_page_jobs__featured_jobs' according to #6 for newer version of search api module.
The only difference I see from #5/#6 is above code adds tag to the original query. So if code wants to do something with above altered query, the tag can be used. See
Comment #11
jpascoal commentedHi @smiletrl, I'm trying to create a view (machine name: homepage_view) with a page (machine name: page_1) and an attachment (machine name: attachment_1).
So, I implemented the following code in the Query.php file the in search_api module:
Also, I implemented the following code in the Queryinterface.php file, also in search_api module:
public function mymodule_search_api_query_alter(QueryInterface &$query);It's not working. What I'm doing wrong? Can you guys help me?
Thanks!!!
Comment #12
borisson_You shouldn't alter contrib modules like that, that's not a good idea.
It's also not supposed to happen in the search api module, but in a custom module. Create your own module (modules/custom/mymodule/ with an info file and a .module file (modules/custom/mymodule/mymodule.info.yml (with contents similar to http://cgit.drupalcode.org/sandbox-sidharrell-2466449/tree/hello_world.i...), and a modules/custom/mymodule/mymodule.module file with the alter hook in it).
Reading the documentation provided by @smiletrl will help you get started with this.
Comment #13
jpascoal commentedThank you @borisson_, I'll try that!
Comment #14
jpascoal commentedHello everyone! It works! Thanks @borisson_ and @smiletrl for the help. I've created a custom module and ran the code:
After flushing all the cash, the code ran for both views (page_1 and attachment_1). However, after I changed the facet filter or changed to other page and returned to the homepage_view, the code ran only for page_1 (I need to flush all the cash again to make it runs to page_1 and attachment_1 again).
What should I do?
Thanks in advance!
Comment #15
jpascoal commentedHello again!
I've solved the problem!
The problem was that the attachment_1 was with a tag-based cashing. After I changed to none, it worked!
Comment #16
vaccinemedia commented@borisson_ is it possible to do this on block displays too? I currently have a search api view with a page display (displayed as a table of results) and also a block in the banner region of the theme (displayed as a map) and I have 4 facets with blocks in the "content top" region. So from a page perspective I have:
Search api view (map)
Facet 1 (block) Facet 2 (block) Facet 3 (block) Facet 4 (block)
Search api page view (table in the content region)
So I can't use an attachment as the facet blocks need to be sandwiched in-between the map in the banner region and the table in the content region. Here's my code:
The block in the banner region however is not picking up the same filter as the page on which it is placed. Is there something I am missing in the config of the block or in the query alter?
Comment #17
vaccinemedia commentedI got it :) The code above works as it should (I just need to update the comments ;) )
I made it work by going to the facet config at:
admin/config/search/facets
Next to the 4 existing ones I selected clone and selected the block view as the destination on the next config screen and finally saved the generated clone.
Now I have 8 facets: 4 for the page and 4 identical ones associated with the block view and the ones for the page are the ones which have been used as the blocks. I made the mistake of trying to clone the existing ones as a test and had the same names and therefore got the error that the machine name already existed. I then made new ones with different names / machine names and these had different url aliases.
Cloning the existing facets from the page view made the machine names different but with the same url aliases which did the trick.
If this isn't in documentation I think we should get this in somewhere - maybe on Drupal.org so people can find the instructions using a search engine such as Google. I may write up a tutorial on this very subject ;)
Comment #18
vaccinemedia commentedUPDATE! ===========
It turned out I didn't even need the code example module to have multiple views displays repeating the search api query driven by the facets. All that I needed to do was make sure that each of the views which needed it had exact clones (with different machine names) of the main page view facets with the same url aliases.
I found this out by having a data export attached to the page view which did not respect the facet selections. So I added / cloned the page facets to the export view and it worked.
Un-installing the custom module had no effect on the map block view which also still worked.
So what's needed is simply to add facets to the main view and then clone them for each other the other displays which need to respect the same filter / query.
Comment #19
proweb.ua commented#14 works for me
Comment #20
strykaizerregarding comment from #8
Attachments are a special case, and we probably do not want this behaviour.
Also, attachments are no separate facet source at this moment, so you can not even fix it at this moment.
We probably need to fix this specificly for attachments, based on code from #6.
The hard part is that attachments can be attached to multipe blocks/pages, so I'm not sure how to tackle this. Maybe we can just fire the alterQuery on every facetsource the attachment is attached to and it'll work, haven't tested it though
Comment #21
ariane commented#6 worked for me, thanks!
Comment #22
valgibson commentedAh, so nice to see my code suggestions still work after so many years! I'm probably going to need it too for a new project. Have a nice day everyone!
Comment #23
pieterjandp commentedIn case someone wants to support multiple views displays with AJAX enabled: I managed to do this by performing an additional Drupal ajax request (in addition to the solution posted in #6), based on https://stackoverflow.com/a/3597640/528310 and https://git.drupalcode.org/project/facets/blob/8.x-1.x/js/facets-views-ajax.js#L80-83:
MYMODULE.libraries.yml:
js/search.js:
Comment #24
dmouseTo solve this I created an event subscriber to change the Search Id Source and I extended the checkbox widget to render the correct url for each facet.
BTW, I'm using a view with 3 displays and these are linked using the "Global: Link to display" in the header of my first display
Comment #25
erik_petra commented#6 or #14 at first didn't work, I needed to disable caching in attachment to make it work as mentioned in #15.
Comment #26
letrollpoilu commentedI don't get it... I think I've done everything properly but it's still not working.
My goal is to do a global search on the website, for that I have an Index with news, media... When people are searching with a tag, I want them to see like max 3 news with this tag, then max 3 media with this tag. So I have my view with the facets and it works just fine. I just can't limit each content type to 3 items.
So I created attachments per content type, one for news, media... and set a pager of 3. The idea is to print on the same page:
- The main view (hidden, like not printed but generated)
- The attachments
- The facets
Now the facets should filter the attachments so that I have several content types groups filtered.
So I checked that there is no cache on my attachment, then I put this code on a module:
This is still not working. Am I doing something wrong? Attached are my views.
https://ibb.co/dJctfB0
https://ibb.co/sQbNvFL
https://ibb.co/7Jtr9Hc
Thanks for your help!
Comment #27
dimonuga commentedAs per document, since 1.14 version API hooks depreciated.
I've tried to use solution in #24, but did not succeed. can anybody share examples to implement pre_execute in new approach?
Comment #28
drclaw commentedJust wanted to +1 Comment-#18 and re-iterate that if you just clone the facets from one display to another and keep the facet alias the same, the view will pick it up. In @vaccinemedia's words:
I don't think the preExecute or query_alter is necessary at all. At least not for the relatively simple example of a "featured" block inheriting the facets from the main listing.
Comment #29
v.kydyba commentedSolution #24 works for me, thanks.
Comment #30
mkalkbrenner#24 basically works. But since Facets itself subcribes to the same event now you need to ensure that your event subscriber has the higher priority. So the adjusted code looks like this:
Comment #31
vurt commentedThank you, the solution with the eventsubscriber #30 is a good general workaround to use facets with attachment views. Worked for me.
Comment #32
tvalimaa commented#30 It seems that code got it working. Thanks you.
Comment #33
mlncn commentedAnyone doing this (e.g. eight items from one display, 12 from the next, so as to have visually distinct as far as view mode and number per row but all filtered with the same facets) with Facets 3.0.x? Seems things have changed there, or i'm missing a trick.
Comment #34
ressaThanks @dmouse and @mkalkbrenner. I have attempted to use your suggestions, but it seems to not work any more, maybe due to changes in version 3, as @mlncn mentions?
I have added the first part with
function alterSearchId(@mkalkbrenner's updated version) in my_module.module and the second part in Checkbox.php, like this:On /admin/config/search/facets my Views page is listed as "Facet source - search_api:views_page__map__map - View Map, display Map - Configure" which I use the facets on.
I assume I should replace
$query->setSearchId('views_page:solr_search_content__page_1');in the example withsearch_api:views_page__map__map, right?After this, under each facet, I also assume I would get checkboxes, in stead of the single select current dropdown, but I still get the dropdown ...