search_api_views.module 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * @file
  4. * Integrates the Search API with Views.
  5. */
  6. /**
  7. * Implements hook_views_api().
  8. */
  9. function search_api_views_views_api() {
  10. return array(
  11. 'api' => '3.0-alpha1',
  12. );
  13. }
  14. /**
  15. * Implements hook_search_api_index_insert().
  16. */
  17. function search_api_views_search_api_index_insert() {
  18. // Make the new index available for views.
  19. views_invalidate_cache();
  20. }
  21. /**
  22. * Implements hook_search_api_index_update().
  23. */
  24. function search_api_views_search_api_index_update(SearchApiIndex $index) {
  25. // Check whether index was disabled.
  26. if (!$index->enabled && $index->original->enabled) {
  27. _search_api_views_index_unavailable($index);
  28. }
  29. // Check whether the indexed fields changed.
  30. $old_fields = $index->original->options + array('fields' => array());
  31. $old_fields = $old_fields['fields'];
  32. $new_fields = $index->options + array('fields' => array());
  33. $new_fields = $new_fields['fields'];
  34. if ($old_fields != $new_fields) {
  35. views_invalidate_cache();
  36. }
  37. }
  38. /**
  39. * Implements hook_search_api_index_delete().
  40. */
  41. function search_api_views_search_api_index_delete(SearchApiIndex $index) {
  42. _search_api_views_index_unavailable($index);
  43. }
  44. /**
  45. * Function for reacting to a disabled or deleted search index.
  46. */
  47. function _search_api_views_index_unavailable(SearchApiIndex $index) {
  48. $names = array();
  49. $table = 'search_api_index_' . $index->machine_name;
  50. foreach (views_get_all_views() as $name => $view) {
  51. if (empty($view->disabled) && $view->base_table == $table) {
  52. $names[] = $name;
  53. // @todo: if ($index_deleted) $view->delete()?
  54. }
  55. }
  56. if ($names) {
  57. views_invalidate_cache();
  58. drupal_set_message(t('The following views were using the index %name: @views. You should disable or delete them.', array('%name' => $index->name, '@views' => implode(', ', $names))), 'warning');
  59. }
  60. }