A couple of days ago, we had to use facets with a field type Hierarchical Select which referred to a taxonomy of various levels. Up here, everything was fine, but the client also needed to select more than 2 elements of different levels ...
Taxonomy Name: Colors, with the following terms:
Field Type: Hierarchical Select, could not be configured to save the last element since the user could select a parent element and would have to obtained as a result all the children. Conclusion field must save the list of terms.
Facet could not be configured as OR, since the Hierarchical Select keeps a list of all parents to get to the end, this would cause that selecting term "Blue", the term parent also be selected, for thus also the red and yellow terms.
Facet could not be configured as AND, since the user needs to select more than two elements of the same or different parents.
The way to fix was in code, for this reason we invite you to read this post, where we will write about:
To continue the post, we must have previously installed and configured Apache Solr Search, Search API and Facet API.
One of the needs of the sites that have search for products or services offered (example Amazon) are builders to create and manage their search interfaces which must be clearly for users .
We found five (5) tabs, View, edit, fields, filters and facets. Within the field tab, we will activate the Title field as seen in the image below.
Once saved, we go to the tab facets within this field Title seek
On the right side we see the options that have all fields facets, each of which shows different functions:
To perform this process, you must set the facet for it to be altered. Access the list of facets (admin/config/search/search_api), select the indexer to use and after click edit facets, once there look for the field to be modified and select edit filters facet. Once we ensure that the option to Rewrite facet items via callback function is selected.
The next step is to add the following hook in our module, in this example we get the markup results in uppercase.
function HOOK_facet_items_alter(&$build, &$settings) {
if ($settings->facet == "YOUR_FACET_NAME") {
foreach($build as $key => $item) {
$build[$key]["#markup"] = drupal_strtoupper($item["#markup"]);
}
}
}
For this example, the facet is configured as OR and Hierarchical Select field stores all items.
Into variable $build[$tid]['# query']['f'] are stored the filters, sample field_color:61 (which belongs to red color), this means that our quest to realize that value will as a filter, the red color.
When a child term is selected, the variable $build[$tid]['# query']['f'] by default add the filter father, continuing the above example, if we select the blue, we see that $build[$tid] ['# query']['f'] has field_color:61 and field_color: 60 (the father term, Primary Color), this means that we will have three filters, Red, Primary Colors and new color to select. Big problem because the filter by Primary Colors, also will filter red, blue and yellow.
To select more than 2 elements from different fathers, consider to be removed parents of both terms.
If a color is deselected example: When yellow deselect, verify if another color from the same father and the same hierarchy are selected, if so, we have 2 options.
If there are brothers terms selected should continue filtering on those.
If the term is deselected Finally, we need to pass new filter as the term parent for the user to keep searching.
This is a case to consider different validations to meet customer needs. The HOOK_facet_items_alter(&$build,&$settings) is very useful because it allows us to add or remove terms depending on the needs:
unset($build[$tid]['#query']['f'][$key_parent]);
$build[$tid]['#query']['f'][] = 'field_color:63';
Modules Apache Solr SearchSearch API and Facet API. They offer great flexibility to change/configure its properties, from the graphical user interface and the development of code. There are many sites which we can see results of such searches, pages from real estate sites selling products. Providing end users a convenient way to refine their searches.