Hello,
I am using Search Api with solr, views and facet api. And everything is working fine :)
What I want is to change the Views title of the search results according to the facet options that have been selected.
If the title of the views is "Articles" and the user selects, from "Categories" facet, the option "Software". The title must change to "Articles in Software".
Right now I change the title using "hook_views_pre_render" by taking the option from the URL. Pretty lame-non-drupal-way, I know....:)
Is there a hook that I can get the term ids of the options selected and being able to change views title?
Thanks!
Comments
Comment #1
drunken monkeyYou could look directly at the filters set on the Search API query object. You can get that by calling
search_api_current_search()
, or inhook_search_api_query_alter()
(but that probably won't work since you can't really change the view title from there – you could set the page title, though).Regarding the Facet API, I have to admit I don't really know what hooks it provides – either look yourself in the
facetapi.api.php
file or ask in the Facet API issue queue. Also, since the Facet API can change the breadcrumbs to reflect selected facets, maybe changing the page title would be possible in a similar way? (However, don't know if that's enough for you, since you write you want to change the view title …)Hope that helps.
Comment #2
matrixlord CreditAttribution: matrixlord commentedThanks drunken monkey for the quick response,
I am using dsm(search_api_current_search()) but I can't get the actual values passed to search_api.
I am looking right now at current_search module that comes with facet_api. Maybe the answer for me is somewhere there.
Comment #3
drunken monkeyThe data you need isn't available through a
dsm()
, you have to use the appropriate methods on the query object. See the documentation inincludes/query.inc
, or this example code:And somewhere in those filters will be the values passed to the Search API.
Comment #4
matrixlord CreditAttribution: matrixlord commentedThanks again drunken monkey,
I found how to get the selected facets. I wouldn't get very easy there by myself, so your help is appreciated!
Comment #5
drunken monkeyComment #7
Sam152 CreditAttribution: Sam152 commentedIf anyone finds this I solved it like this. I'd also be interested if there were any cleaner solutions.
Comment #8
cm_is CreditAttribution: cm_is commentedwould be interested. where to put the code from #7 ?
Comment #9
ndenhild CreditAttribution: ndenhild commentedProbably in template.php but i doesn't work for me!
Comment #10
ndenhild CreditAttribution: ndenhild commented@cm_is I have a work around.
Change youre view from renderd enity to using fields.
Then add the field Indexed node: rendered entity ánd the field product category. Hide this latest field. Place the product category as title trought the available token.
There you have it!
Comment #11
philipz CreditAttribution: philipz commentedI've done this basing on #7 but little simpler code. Put this in a module. This might need some tweaking if there are facets other than taxonomy terms.
Comment #12
leahtard CreditAttribution: leahtard commented#11 was just what I needed. Thanks!
Comment #13
youlikeit CreditAttribution: youlikeit commentedHi #11 worked for me just fine but looking in the logs I get
Notice: Trying to get property of non-object in modulename_views_pre_render() (line 13
and my cache is growing much to fast
pleas advise
Comment #14
ShaneOnABike CreditAttribution: ShaneOnABike commentedThanks for this! Just to include an upgrade that includes views exposed filters if you have any ;) I also included a check to make sure that if there are no results just eliminate setting the title.