search_api.rules.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. $type = $wrapper->type();
  51. $item_ids = array($wrapper->getIdentifier());
  52. if (empty($index) && !$index_immediately) {
  53. search_api_track_item_change($type, $item_ids);
  54. return;
  55. }
  56. if ($index) {
  57. $indexes = array($index);
  58. }
  59. else {
  60. $conditions = array(
  61. 'enabled' => 1,
  62. 'item_type' => $type,
  63. 'read_only' => 0,
  64. );
  65. $indexes = search_api_index_load_multiple(FALSE, $conditions);
  66. if (!$indexes) {
  67. return;
  68. }
  69. }
  70. if ($index_immediately) {
  71. foreach ($indexes as $index) {
  72. $indexed = search_api_index_specific_items_delayed($index, $item_ids);
  73. }
  74. }
  75. else {
  76. search_api_get_datasource_controller($type)->trackItemChange($item_ids, $indexes);
  77. }
  78. }
  79. function _search_api_rules_action_index_help() {
  80. return t('Queues an item for reindexing. If "index immediately" is disabled then the item will be indexed during the next cron run.');
  81. }