callback.inc 5.4 KB

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