search_api.inc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * @file
  4. * Path generation for Search API.
  5. *
  6. * Available vars:
  7. * $keywords: user input
  8. * $types: content types (machine names[])
  9. * $terms: taxonomy terms (tids[])
  10. * $page: the Search API page
  11. *
  12. * To return:
  13. * the complete search path
  14. *
  15. */
  16. function _custom_search_search_api_search($variables) {
  17. $type = $variables['page']->path . '/' . $variables['keywords'];
  18. $keys = array();
  19. if (count($variables['types']) && !in_array('all', $variables['types'])) {
  20. foreach ($variables['types'] as $key => $value) $keys["filter[type][$key]"] = "\"$value\"";
  21. }
  22. if (module_exists('taxonomy') && count($variables['terms'])) {
  23. // Get index fields info, and keeps only the taxonomy ones
  24. $index = search_api_index_load($variables['page']->index_id);
  25. $fields = array();
  26. foreach ($index->options['fields'] as $field => $data) {
  27. if (isset($data['entity_type']) && $data['entity_type'] == 'taxonomy_term') {
  28. $field_info = field_info_field($field);
  29. $fields[$field_info['settings']['allowed_values'][0]['vocabulary']] = $field;
  30. }
  31. }
  32. // Adds terms
  33. foreach ($variables['terms'] as $key => $value) {
  34. $term = taxonomy_term_load($value);
  35. if (array_key_exists($term->vocabulary_machine_name, $fields))
  36. $keys['filter[' . $fields[$term->vocabulary_machine_name] . '][' . $key . ']'] = "\"$value\"";
  37. }
  38. }
  39. return array('path' => $type, 'query' => $keys);
  40. }