Hi,
Is there any documentation for facets D8?
I get value from URL which is taxonomy id. On page /regions, I have a facet block of Regions taxonomy. Let's say URL is /regions/5 I want to set active item in facet "Africa" which has taxonomy id 5 and run a search query. All this needs to happen before view is rendered.
I spend all day debugging this module and so far I didn't find anything useful except that in UrlProcessorPluginBase.php on line 69 there is $this->request = clone request. And if you debug that line there is set [parameter => [f => [0 => 'region:5']]].

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

matjaz_zavski created an issue. See original summary.

maaty388’s picture

Title: Set active facet item » How to set facet item as active programatically?
Issue summary: View changes
maaty388’s picture

Priority: Normal » Major
borisson_’s picture

Priority: Major » Normal

There's currently no documentation on drupal.org on how to do this, but the facet has ::setActiveItems. That should be passed an array of items that need to be active.

We use hook_search_api_query_alter to alter the actual items to be active before the query runs, so if your hook runs before that one everything should work.

borisson_’s picture

@matjaz_zavski did you find a solution? It would be great to provide some documentation for this (even if it was just in the issue queue).

maaty388’s picture

I actually did yeah but I don't know if this is a correct way to resolve it.
I actually used hook_preprocess_taxonomy_term and set path of taxonomy item to ?f[0]=[taxonomy_name]:[tid] it is actually working fine (if you don't mind having weird URL), but I found that facets pretty paths exist but this module is not working correctly yet.
So could someone write documentation that filter is getting applied based on URL?

maaty388’s picture

Status: Active » Closed (works as designed)
Paulmicha’s picture

Version: 8.x-1.0-alpha11 » 8.x-1.0-beta2
Status: Closed (works as designed) » Active

Given the goal "set facet item as active programmatically" and the status "works as designed", I'd say the current (suggested) workaround to "set [...] path to ?f[0]=[taxonomy_name]:[tid]" doesn't cut it.

Example : you want to force a default value that's dynamic (e.g. set the current year to active only if none is selected on a date filter facet).
→ Can't change links pointing to that page, as they would become outdated every year.

Here are my current failed attempts :

Implementing hook_preprocess_block() and getting the facet object like $facet = Facet::load($variables['derivative_plugin_id']);
→ nothing comes out of $facet->getActiveItems() when an item is indeed selected, and $facet->setActiveItems() has NO effect whatsoever (and I cleared caches to make sure in every test).

Implementing hook_search_api_query_alter() : setting the values from there does work using $query->addCondition() for the results, however the corresponding item in the rendered facet block does not appear as "active".

@borisson_ When you say "We use hook_search_api_query_alter() to alter the actual items to be active before the query runs", how do you actually access the Facet items from there ?

archnode’s picture

I made a small sandbox project that should satisfy this use case: Facets taxonomy path processor

This adds a processor that activates the taxonomy facet if the term is in the path. You need to activate this in your facet configuration. If it doesn't work for you it should at least be a good starting point.

@borisson If this is something you would like to add to the module I can provide a patch.

borisson_’s picture

No, I think keeping this is a seperate module is better, I would like to keep the functionality here as small as possible, we've already created enough code to maintain :)

If you'd like, I can mention this in the README.txt, we have a list of EXTENSION MODULES in there, to link to those modules.

You could open a patch with a change to just the readme.

archnode’s picture

Priority: Normal » Minor
Status: Active » Needs review
FileSize
418 bytes

Sure thing! I made a release and the patch attached adds the project to the readme.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Looks great. Thanks!

borisson_’s picture

Title: How to set facet item as active programatically? » Add facets_taxonomy_path_processor module to readme file

borisson_’s picture

Component: Code » Documentation
Category: Support request » Task
Status: Reviewed & tested by the community » Fixed

Committed and pushed. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.