search_api.rules.inc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. 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. }