3

Ok so title is pretty epic, but it goes like this: Key elements: views, taxonomy_menu, taxonomy, search api, facets.

I created vocabulary "Products" (Drupal Commerce but I don't think it's important), and there term - "Chemistry" - which should be a root of tree of other more specific terms. I added some products. Next I enabled Taxonomy Menu and created a menu, so far so good, but I wanted to use search api, so I created a view of my search index with path:

/taxonomy/term/%

That way I wanted to override view of my taxonomy, and be able to have facets in it. And it perfectly displays indexed products of terms that have them, but it fails to display all products of CHILDREN terms. So if I have my term "Chemistry" - after clicking it - I would like to be able to see every product of its children terms. I cannot see how can I do this with current approach, as it seems that I doesn't have contextual filters for that (as my view is displaying search index).

What can I do?

  • Did you try the taxonomy term filter with depth ? – Gregory Kapustin Feb 10 '13 at 17:20
  • There is no such filter if you created view of search index - there is a problem. I'm open for workarounds also. – Łukasz Zaroda Feb 10 '13 at 17:21
  • Perhaps taxonomy entity index would provide that filter for you. – Andy Feb 10 '13 at 17:45
  • I was trying to do this exact same thing last week. I settled for adding the items to the parent terms aswell. It's not a true solution but it works, I guess this could also be done automatically with node save and update hooks. – James Feb 11 '13 at 9:47
  • @James You might want to check out the module I mentioned above - it's only beta, but it's a true solution :) – Andy Feb 11 '13 at 14:10
4

If I'm understanding correctly what you're asking, here are some tips.

Under your defined search index, go to Fields. If you have "Products" taxonomy defined, uncheck the taxonomy term, then go to the bottom and open "Add Related Fields" and add "Products", then save changes. When it reloads, you'll now have more taxonomy options. Check "Products >> All parent terms". You should now have additional facets for parent/child terms.

Use in conjunction with Facet API Pretty Paths for even more powerful usage. Take a look at one of my other response about how to use that.

1

Using the following in a custom module will automatically add the parent terms to the node when it is saved.

/*
 * Implements hook_node_presave().
 */
function MYMODULE_node_presave($node) {
  // Get the taxonomy terms.
  if ($terms = field_get_items('node', $node, 'MYVOCABULARY')) {
    $tids = Array();
    foreach ($terms as $term) {
      // Add the term to a list of tids.
      $tids[$term['tid']]['tid'] = $term['tid'];
      // Get the parents of the taxonomy term.
      $parents = taxonomy_get_parents($term['tid']);
      foreach ($parents as $parent) {
        // Add the parents to a list of tids.
        $tids[$parent->tid]['tid'] = $parent->tid;
      }
    }
    // Add the list of tids to the node.
    $node->MYVOCABULARY['und'] = $tids;
  }
}
1

The KyleTaylored direction was right.

But I only have to in admin/config/search/search_api/index/product_display/workflow (product_display is my machine name of Product display index DB) check checkbox Index hierarchy. Reindex content admin/config/search/search_api/index/product_display/status.

Thats all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.