Hi,
I'm trying to build a new facet, or modify an existing one to include the items from others.
What I'm trying to achieve is have a facet block that have a list of content types, the existing 'type' facet, plus the items from one or more existing facets, as 'field_news_type', that have a category in a content type defined by an integer list.
My aproach, that could not be the best, is implement hook_facet_items_alter(&$build, &$settings) from the facetapi_bonus module.
So, I am trying to include new items to the $build array.

Is there a way to programmatically call the query that get the items from a specific facet and include the items from another?.

I'm using facetapi-1.5, search_api-1.13, search_api_solr-1.5.
I'm trying to get a list of built items, form this method FacetapiFacet $facet->getBuild(), but I can't instanciate the object properly.
What I'm doing in my module is:

function mymodule_facet_items_alter(&$build, &$settings) {
 //I need to add more items to the $build array
 if ($settings->facet == "type") {
    $adapter = facetapi_adapter_load('search_api@devel_nodes_index');
    $facets = new FacetapiFacet($adapter, facetapi_facet_load('field_news_type','search_api@devel_nodes_index'));
    $build_list = $facets->getBuild(); // I spect to get a list of items
 }
}

I guess, I'm missing steps and I manually should call another methods to properly setup the adapter and facet objects.

Is there another recommended aproach to do this?

Comments

betoscopio’s picture

Issue summary: View changes
Shuairan’s picture

had the same problem, combining values from two different facets. Maybe this code will help you:

$facets = new FacetapiFacet($adapter, facetapi_facet_load('field_news_type','search_api@devel_nodes_index'));

//process the facet:
$processor = new FacetapiFacetProcessor($facets);
$processor->process();
//now $processor->build has all items

//if you want to build the real facet output, think this is needed to apply filters, buildListItems etc: 
$realm = facetapi_realm_load('block');
$facets->build($realm, $processor);

$build_list = $facets->getBuild(); // there it is (in my case this was exactly the same as $processor->build
betoscopio’s picture

Hey Shuairan, thank you very much, using your code worked for me. I wasn't aware about the FacetapiFacetProcessor object.
Just curios to know, how did you implemented this? in some particular hook?, or just like me using hook_facet_items_alter from the facetapi_bonus module.

Greetings,
Beto.

Shuairan’s picture

btw: "build" is a protected property. $processor->getBuild() instead of $processor->build would be the right thing in the comment for accessing the values without building the real facet with filters etc.

I am using this as an own FacetapiFilter to build a custom-region-based hierarchy for countries. This is nearly the same as your hook_facet_items_alter which comes from facetapi_bonus and works also with FacetapiFilter (FacetapiFilterRewriteItems).

For my case implementing the hook_facetapi_facet_info_alter and changing/defining the 'hierarchy callback' to do this restructuring for the specific field would even be a litte bit nicer... but in all non-hierarchy cases using the hook_facet_items_alter will be fine.

betoscopio’s picture

Status: Active » Reviewed & tested by the community

Thanks Shuairan for the explanation, was very clear to me.

DamienMcKenna’s picture

Version: 7.x-1.5 » 7.x-1.x-dev
Component: Code » Documentation
Status: Reviewed & tested by the community » Active

This might be useful as a documentation improvement?

jstraney’s picture

Thank you. I also have run into this problem and this was instrumental to solving it. Would be very nice to see something this specific in the docs.