search_api.rules.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @file
  4. * Search API Rules integration.
  5. */
  6. /**
  7. * Implements hook_rules_action_info().
  8. */
  9. function search_api_rules_action_info() {
  10. $items['search_api_index'] = array(
  11. 'parameter' => array(
  12. 'entity' => array(
  13. 'type' => 'entity',
  14. 'label' => t('Entity'),
  15. 'description' => t('The item to index.'),
  16. ),
  17. 'index' => array(
  18. 'type' => 'search_api_index',
  19. 'label' => t('Index'),
  20. 'description' => t('The index on which the item should be indexed. Leave blank to index on all indexes for this item type.'),
  21. 'optional' => TRUE,
  22. 'options list' => 'search_api_index_options_list',
  23. ),
  24. 'index_immediately' => array(
  25. 'type' => 'boolean',
  26. 'label' => t('Index immediately'),
  27. 'description' => t('Activate for indexing the item right away, otherwise it will only be marked as dirty and indexed during the next cron run.'),
  28. 'optional' => TRUE,
  29. 'default value' => TRUE,
  30. 'restriction' => 'input',
  31. ),
  32. ),
  33. 'group' => t('Search API'),
  34. 'access callback' => '_search_api_rules_access',
  35. 'label' => t('Index an entity'),
  36. 'base' => '_search_api_rules_action_index',
  37. );
  38. return $items;
  39. }
  40. /**
  41. * Rules access callback for search api actions.
  42. */
  43. function _search_api_rules_access() {
  44. return user_access('administer search_api');
  45. }
  46. /**
  47. * Rules action for indexing an item.
  48. */
  49. function _search_api_rules_action_index(EntityDrupalWrapper $wrapper, SearchApiIndex $index = NULL, $index_immediately = TRUE) {
  50. // If we do not have an index, we need to guess the item type to use.
  51. // @todo Since this can only be used with entities anyways, we can just loop
  52. // over the item type information and use all types with that entity type.
  53. $type = $wrapper->type();
  54. $item_ids = array($wrapper->getIdentifier());
  55. if (empty($index) && !$index_immediately) {
  56. search_api_track_item_change($type, $item_ids);
  57. return;
  58. }
  59. if ($index) {
  60. $type = $index->item_type;
  61. $indexes = array($index);
  62. }
  63. else {
  64. $conditions = array(
  65. 'enabled' => 1,
  66. 'item_type' => $type,
  67. 'read_only' => 0,
  68. );
  69. $indexes = search_api_index_load_multiple(FALSE, $conditions);
  70. if (!$indexes) {
  71. return;
  72. }
  73. }
  74. if ($index_immediately) {
  75. foreach ($indexes as $index) {
  76. search_api_index_specific_items_delayed($index, $item_ids);
  77. }
  78. }
  79. else {
  80. search_api_get_datasource_controller($type)->trackItemChange($item_ids, $indexes);
  81. }
  82. }
  83. function _search_api_rules_action_index_help() {
  84. return t('Queues an item for reindexing. If "index immediately" is disabled then the item will be indexed during the next cron run.');
  85. }