callback.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * Interface representing a Search API data-alter callback.
  4. */
  5. interface SearchApiAlterCallbackInterface {
  6. /**
  7. * Construct a data-alter callback.
  8. *
  9. * @param SearchApiIndex $index
  10. * The index whose items will be altered.
  11. * @param array $options
  12. * The callback options set for this index.
  13. */
  14. public function __construct(SearchApiIndex $index, array $options = array());
  15. /**
  16. * Check whether this data-alter callback is applicable for a certain index.
  17. *
  18. * This can be used for hiding the callback on the index's "Workflow" tab. To
  19. * avoid confusion, you should only use criteria that are immutable, such as
  20. * the index's entity type. Also, since this is only used for UI purposes, you
  21. * should not completely rely on this to ensure certain index configurations
  22. * and at least throw an exception with a descriptive error message if this is
  23. * violated on runtime.
  24. *
  25. * @param SearchApiIndex $index
  26. * The index to check for.
  27. *
  28. * @return boolean
  29. * TRUE if the callback can run on the given index; FALSE otherwise.
  30. */
  31. public function supportsIndex(SearchApiIndex $index);
  32. /**
  33. * Display a form for configuring this callback.
  34. *
  35. * @return array
  36. * A form array for configuring this callback, or FALSE if no configuration
  37. * is possible.
  38. */
  39. public function configurationForm();
  40. /**
  41. * Validation callback for the form returned by configurationForm().
  42. *
  43. * @param array $form
  44. * The form returned by configurationForm().
  45. * @param array $values
  46. * The part of the $form_state['values'] array corresponding to this form.
  47. * @param array $form_state
  48. * The complete form state.
  49. */
  50. public function configurationFormValidate(array $form, array &$values, array &$form_state);
  51. /**
  52. * Submit callback for the form returned by configurationForm().
  53. *
  54. * This method should both return the new options and set them internally.
  55. *
  56. * @param array $form
  57. * The form returned by configurationForm().
  58. * @param array $values
  59. * The part of the $form_state['values'] array corresponding to this form.
  60. * @param array $form_state
  61. * The complete form state.
  62. *
  63. * @return array
  64. * The new options array for this callback.
  65. */
  66. public function configurationFormSubmit(array $form, array &$values, array &$form_state);
  67. /**
  68. * Alter items before indexing.
  69. *
  70. * Items which are removed from the array won't be indexed, but will be marked
  71. * as clean for future indexing. This could for instance be used to implement
  72. * some sort of access filter for security purposes (e.g., don't index
  73. * unpublished nodes or comments).
  74. *
  75. * @param array $items
  76. * An array of items to be altered, keyed by item IDs.
  77. */
  78. public function alterItems(array &$items);
  79. /**
  80. * Declare the properties that are (or can be) added to items with this
  81. * callback. If a property with this name already exists for an entity it
  82. * will be overridden, so keep a clear namespace by prefixing the properties
  83. * with the module name if this is not desired.
  84. *
  85. * @see hook_entity_property_info()
  86. *
  87. * @return array
  88. * Information about all additional properties, as specified by
  89. * hook_entity_property_info() (only the inner "properties" array).
  90. */
  91. public function propertyInfo();
  92. }
  93. /**
  94. * Abstract base class for data-alter callbacks, implementing most methods with
  95. * sensible defaults.
  96. * Extending classes will at least have to implement the alterItems() method to
  97. * make this work. If that method adds additional fields to the items,
  98. * propertyInfo() has to be overridden, too.
  99. */
  100. abstract class SearchApiAbstractAlterCallback implements SearchApiAlterCallbackInterface {
  101. /**
  102. * The index whose items will be altered.
  103. *
  104. * @var SearchApiIndex
  105. */
  106. protected $index;
  107. /**
  108. * The configuration options for this callback, if it has any.
  109. *
  110. * @var array
  111. */
  112. protected $options;
  113. /**
  114. * Construct a data-alter callback.
  115. *
  116. * @param SearchApiIndex $index
  117. * The index whose items will be altered.
  118. * @param array $options
  119. * The callback options set for this index.
  120. */
  121. public function __construct(SearchApiIndex $index, array $options = array()) {
  122. $this->index = $index;
  123. $this->options = $options;
  124. }
  125. /**
  126. * Check whether this data-alter callback is applicable for a certain index.
  127. *
  128. * This can be used for hiding the callback on the index's "Workflow" tab. To
  129. * avoid confusion, you should only use criteria that are immutable, such as
  130. * the index's entity type. Also, since this is only used for UI purposes, you
  131. * should not completely rely on this to ensure certain index configurations
  132. * and at least throw an exception with a descriptive error message if this is
  133. * violated on runtime.
  134. *
  135. * The default implementation always returns TRUE.
  136. *
  137. * @param SearchApiIndex $index
  138. * The index to check for.
  139. *
  140. * @return boolean
  141. * TRUE if the callback can run on the given index; FALSE otherwise.
  142. */
  143. public function supportsIndex(SearchApiIndex $index) {
  144. return TRUE;
  145. }
  146. /**
  147. * Display a form for configuring this callback.
  148. *
  149. * @return array
  150. * A form array for configuring this callback, or FALSE if no configuration
  151. * is possible.
  152. */
  153. public function configurationForm() {
  154. return array();
  155. }
  156. /**
  157. * Validation callback for the form returned by configurationForm().
  158. *
  159. * @param array $form
  160. * The form returned by configurationForm().
  161. * @param array $values
  162. * The part of the $form_state['values'] array corresponding to this form.
  163. * @param array $form_state
  164. * The complete form state.
  165. */
  166. public function configurationFormValidate(array $form, array &$values, array &$form_state) { }
  167. /**
  168. * Submit callback for the form returned by configurationForm().
  169. *
  170. * This method should both return the new options and set them internally.
  171. *
  172. * @param array $form
  173. * The form returned by configurationForm().
  174. * @param array $values
  175. * The part of the $form_state['values'] array corresponding to this form.
  176. * @param array $form_state
  177. * The complete form state.
  178. *
  179. * @return array
  180. * The new options array for this callback.
  181. */
  182. public function configurationFormSubmit(array $form, array &$values, array &$form_state) {
  183. $this->options = $values;
  184. return $values;
  185. }
  186. /**
  187. * Declare the properties that are (or can be) added to items with this
  188. * callback. If a property with this name already exists for an entity it
  189. * will be overridden, so keep a clear namespace by prefixing the properties
  190. * with the module name if this is not desired.
  191. *
  192. * @see hook_entity_property_info()
  193. *
  194. * @return array
  195. * Information about all additional properties, as specified by
  196. * hook_entity_property_info() (only the inner "properties" array).
  197. */
  198. public function propertyInfo() {
  199. return array();
  200. }
  201. }