search_api_views.module 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. $is_enabled = $index->enabled;
  27. $was_enabled = $index->original->enabled;
  28. if (!$is_enabled && $was_enabled) {
  29. _search_api_views_index_unavailable($index);
  30. return;
  31. }
  32. // Check whether the indexed fields changed.
  33. $old_fields = $index->original->options + array('fields' => array());
  34. $old_fields = $old_fields['fields'];
  35. $new_fields = $index->options + array('fields' => array());
  36. $new_fields = $new_fields['fields'];
  37. // If the index was enabled or its fields changed, invalidate the Views cache.
  38. if ($is_enabled != $was_enabled || $old_fields != $new_fields) {
  39. views_invalidate_cache();
  40. }
  41. }
  42. /**
  43. * Implements hook_search_api_index_delete().
  44. */
  45. function search_api_views_search_api_index_delete(SearchApiIndex $index) {
  46. // Only do this if this is a "real" deletion, no revert.
  47. if (!$index->hasStatus(ENTITY_IN_CODE)) {
  48. _search_api_views_index_unavailable($index);
  49. }
  50. }
  51. /**
  52. * Function for reacting to a disabled or deleted search index.
  53. */
  54. function _search_api_views_index_unavailable(SearchApiIndex $index) {
  55. $names = array();
  56. $table = 'search_api_index_' . $index->machine_name;
  57. foreach (views_get_all_views() as $name => $view) {
  58. if (empty($view->disabled) && $view->base_table == $table) {
  59. $names[] = $name;
  60. // @todo: if ($index_deleted) $view->delete()?
  61. }
  62. }
  63. if ($names) {
  64. views_invalidate_cache();
  65. 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');
  66. }
  67. }