Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
With current dev i experience a ajax facets block issue that is not there without ajax.
How to reproduce:
- Have content types "event" and "organisation"
- Have a view that lists both types, with an optional context from path like view/%type, so we have view/event, view/organisation, and view/all
- Have one facet block based on an event field and one based on an organisation field for that view page
- go to view/organisation (so the view context "content type" is "organisation")
expected:
- on view/organisation, only facets from type "organisation" are shown
experienced without ajax:
- works as expected
experienced with ajax enabled:
- works as expected on first page load
- when clicking a facet item: facets block shows facets from event and organisation
So it looks like ajax does not see the "organisation" context.
Comment | File | Size | Author |
---|---|---|---|
#32 | 2986981-32.patch | 1.55 KB | Jeroen Dost |
#30 | 2986981-30.patch | 1.51 KB | neclimdul |
#26 | facets-2986981-26.patch | 694 bytes | daveiano |
#17 | facet_results_view_hook.patch | 1.5 KB | matteodem |
#13 | ajax_facet_block_views_context-2986981-13.patch | 1.52 KB | nchar |
Issue fork facets-2986981
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
Comment #2
hannessslightly updated IS to be clearer and better to reproduce.
Comment #3
hannessComment #4
larowlanThe issue here is the views ajax request doesn't pass along the ?f[0]= parameters so the facets url processor doesn't pick up that there are active facets.
Will take a look to see what I can suss out.
Comment #5
larowlanHere's how I fixed it
Replace YOUR_MODULE_NAME and YOUR_VIEW_ID as appropriate.
Comment #6
borisson_@larowlan: is that something that we should include in the facets module as well?
Comment #7
hannessthanks larowlan!
@borisson_: i think it should go into facets, as view contexts are a common use case (and i remember that it was discovered months ago in the ajax facets issue, but not as a blocker).
we currently discovered that facets and domain access does not work probably together as well. in this case the facets show all counts and items as if there is no domain access - similar to this issue. but it appears independent from ajax, so maybe not related to this.
currently it is not that important for us, we will look into it later.
Comment #8
larowlanUnfortunately, due to the way views uses this, I'm not sure if it can.
See the issue is that whilst views keeps ajax settings per view, in the case of the ajax path its a single value for the whole page.
Now I guess we could argue that any view on that page should pass along the facet url parameters, but I don't know enough about facets to be able to say whether that is correct.
So if every view on the page that uses ajax would still work if we pass along those params, then yes we can. But if we need to be selective about it, then its probably a per-site thing where the site developer makes the decision with knowledge about what views there are on that page.
If you think it is harmless, I'll roll a patch.
Please open a separate issue, that is unrelated.
Comment #9
geek-merlin(Streamlining the IS).
Comment #10
geek-merlin@larowlan: Thanks for your code in #5. It looks though like this code fixes lost query parameters. Any idea how to fix lost context (which is part of the path, not the query parameters)?
Comment #11
larowlanAh, I took 'context' to mean 'active facets', but I assume you're meaning context as in the actual drupal context api. Apologies.
Comment #12
rboedeker CreditAttribution: rboedeker commentedThank you very much for digging into this issue and to provide the code for us!
I am facing the issue as well and for me your code works perfekt for "link-facets". I am still facing issues with checkbox facets: If I klick on a checkbox facet, the view is updated as expected, however the facetgroup is getting inactive and the checkbox can not be unselected anymore...
Do you have any idea to fix it ?
Comment #13
nchar CreditAttribution: nchar at Netstudio commentedI solved the problem for my project with this patch.
Comment #14
lukasss CreditAttribution: lukasss commented#13 works for me
Comment #15
rboedeker CreditAttribution: rboedeker commentedThank you for Patch #13 ! It looks promissing.
I still have the issue with checkbox facets: Obviously js "checkbox-widget.js" turns the link into a checkbox and disabels all checkboxes belonging to the facet.
If I manually refresh the page the checkboxes are set in the right way and I can klick another checkbox that than triggers the View to refresh the result. However all checkboxes out of the facet are disabled until I manually do a page refresh...
Any idea to get the last step done ?
Comment #16
Siavash CreditAttribution: Siavash commented#13 worked for me, thanks!
Comment #17
matteodem CreditAttribution: matteodem commentedSince #13 didn't work for me I created a patch with a new hook called hook_facets_search_api_results_view_preprocess, which allows to set the view arguments (or other adjustments) manually.
Comment #18
geek-merlinComment #19
DamienMcKennaComment #20
DamienMcKennaAfter some testing, this isn't working for me either.
Comment #21
DamienMcKennaRelated: #2939710: Add support for "Search API (tags based)" caching in Views
Comment #22
DamienMcKennaComment #23
mstrelan CreditAttribution: mstrelan commentedHeads up if using #5, this doesn't play nicely with views_ajax_history as that module detects the presence of the ? in the URL to determine whether clean urls are enabled or not.
See #3157331: Non-clean URL path added to views ajax url
Comment #24
jhedstromI just encountered this, and traced the issue to a default argument plugin that uses pretty common logic for getting the current node:
Since that fails on ajax routes (no node parameter), this results in the view losing its contextual filter value.
even the core 'Get Content ID from URL' plugin uses logic like this, so those would fail on ajax requests (see #2769251: Contextual filter don't work with ajax in view.) if they were attempting to load view results as the facets module does.
I couldn't find a decent alternative that is common practice for loading the current entity on ajax requests, but will update this if/when we find a workaround.
Comment #25
jhedstromComment #26
daveianoI was facing a similar problem when setting contextual Filters for a view via
hook_views_pre_execute()
, something like this:The Initial page load works, only facets from the filtered results are shown, but If clicking on a facet (ajax enabled) the facet shows everything and forgets the programmatically set contextual filters. I ended up just adding the
$view->preExecute();
call in SearchApiDisplay.php to invoke the hook. See attached patch. Probably also the other methods like pre_view should be executed here or are there any culprits when doing this? So perhaps this fix and a working fix for contextual filters set by URL should do it?
Comment #27
jhedstromWe ended up working around this using the
path.current
service, as that does have the current path (not the ajax request path) of, for instancenode/123
. From there we added this method to retrieve the object similar to the route match service:Comment #28
neclimdulI'm not sure what the problems people where running into with this patch. I've been using the patch in #13 in several different types of fairly highly trafficked facet page with ajax views since 04/2019. It seems to work perfectly. at fixing the views context.
It also seems like the most correct fix as its recreating the logic views uses to correctly setup the views arguments:
https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/modules/view...
So, without knowing how it didn't work for people, I'm not sure how to move this forward. We have to run a number of other patches to get the views ajax working correctly like #3031544: Ajax facet adds q and _wrapper_format to the browser URL and now #3191091: QueryString url_processor broken(ish) for Ajax Views so maybe the problem people have is related to those? There where also other problems when a lot of these reviews happened that have been committed so maybe #13 works better now?
edit: remove unrelated issue link
Comment #29
acThe patch in #13 solves the issue described. Some of the other comments are a bit of topic IMO. I would support committing #13
Comment #30
neclimdulbroken by #2939710: Add support for "Search API (tags based)" caching in Views and follow ups. No meaningful change.
Still running in production. Still just correctly applying the path logic from views. Any chance of committing it?
Comment #31
acAgreed. I have been using this in production for years.
Comment #32
Jeroen Dost CreditAttribution: Jeroen Dost at React online commentedReroll for v2.0.6
Comment #33
neclimdulconflicted with #3264284: Facets summary should be cacheable, in case facets are using cacheable source.
Comment #34
AndySipple CreditAttribution: AndySipple at Kanopi Studios commentedPatch #33 worked for me.
My current use case.
Drupal views with an exposed filter search input and exposed and the filters are exposed as a block and the view is an exposed block.
Ajax turned on for the view using with combination with views ajax history module and this patch.
Now ajax works and respects facets + the views exposed filter yay!
Current markup with inside a paragraph using twig tweaks:
Comment #35
burnellw_cit CreditAttribution: burnellw_cit commentedComment #36
joseph.olstad@burnellw_cit , patches should be created relative to the module, not your project.
You should also rely on composer to apply properly rolled patches such as patch number 32
https://www.drupal.org/docs/develop/git/using-git-to-contribute-to-drupa...
Comment #37
joseph.olstad@burnellw_cit,
A properly rolled patch looks like this:
Yours was made relative to your project, is incorrect.
If you use composer to specify patches you would be able to use correctly rolled patches such as patch #32.
Please review this with esteemed colleagues , for further assistance please reach out to someone on slack in the Drupal channels or on https://drupalchat.me . Your organisation may have to change it's process/build slightly to adhere to build norms.
https://www.drupal.org/docs/develop/git/using-git-to-contribute-to-drupa...
Comment #39
nickvanboven CreditAttribution: nickvanboven as a volunteer commentedI have created a fork with the changes from #32 and added a check for the route object, this can be null if the request is serialized and used at a other time, we use this to get the view options on cron when processing some jobs.
Comment #41
joseph.olstad@nickvanboven, the 2 fails seen in MR 137 comment#40 not related to your changes.
#3358365: HEAD tests failing since March 23, 2023