search_api.inc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. function _custom_search_search_api_search($variables) {
  16. $type = $variables['page']->path . '/' . $variables['keywords'];
  17. $keys = array();
  18. if (count($variables['types']) && !in_array('all', $variables['types'])) {
  19. foreach ($variables['types'] as $key => $value) {
  20. $keys["filter[type][$key]"] = "\"$value\"";
  21. }
  22. }
  23. if (module_exists('taxonomy') && count($variables['terms'])) {
  24. // Get index fields info, and keeps only the taxonomy ones.
  25. $index = search_api_index_load($variables['page']->index_id);
  26. $fields = array();
  27. foreach ($index->options['fields'] as $field => $data) {
  28. if (isset($data['entity_type']) && $data['entity_type'] == 'taxonomy_term') {
  29. $field_info = field_info_field($field);
  30. $fields[$field_info['settings']['allowed_values'][0]['vocabulary']] = $field;
  31. }
  32. }
  33. // Adds terms.
  34. foreach ($variables['terms'] as $key => $value) {
  35. $term = taxonomy_term_load($value);
  36. if (array_key_exists($term->vocabulary_machine_name, $fields)) {
  37. $keys['filter[' . $fields[$term->vocabulary_machine_name] . '][' . $key . ']'] = "\"$value\"";
  38. }
  39. }
  40. }
  41. return array('path' => $type, 'query' => $keys);
  42. }