search_api_views.api.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Search Views module.
  5. */
  6. /**
  7. * Alter the query before executing the query.
  8. *
  9. * @param view $view
  10. * The view object about to be processed.
  11. * @param SearchApiViewsQuery $query
  12. * The Search API Views query to be altered.
  13. *
  14. * @see hook_views_query_alter()
  15. */
  16. function hook_search_api_views_query_alter(view &$view, SearchApiViewsQuery &$query) {
  17. // (Example assuming a view with an exposed filter on node title.)
  18. // If the input for the title filter is a positive integer, filter against
  19. // node ID instead of node title.
  20. if ($view->name == 'my_view' && is_numeric($view->exposed_raw_input['title']) && $view->exposed_raw_input['title'] > 0) {
  21. // Traverse through the 'where' part of the query.
  22. foreach ($query->where as &$condition_group) {
  23. foreach ($condition_group['conditions'] as &$condition) {
  24. // If this is the part of the query filtering on title, chang the
  25. // condition to filter on node ID.
  26. if (reset($condition) == 'node.title') {
  27. $condition = array('node.nid', $view->exposed_raw_input['title'],'=');
  28. }
  29. }
  30. }
  31. }
  32. }