search_plugin.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @file
  4. * Linkit Search plugin interface.
  5. *
  6. * Provides an interface and classes to implement Linkit search plugins.
  7. */
  8. /**
  9. * Defines a common interface for a Linkit search plugin.
  10. */
  11. interface LinkitSearchPluginInterface {
  12. /**
  13. * Search plugin factory method.
  14. *
  15. * @param $plugin
  16. * A search plugin object.
  17. *
  18. * @param LinkitProfile $profile
  19. * (optional) A LinkitProfile object.
  20. *
  21. * @return
  22. * An instance of the search plugin class or an instance of the
  23. * LinkitSearchPluginBroken class.
  24. */
  25. public static function factory($plugin, LinkitProfile $profile);
  26. /**
  27. * Return a string representing this handler's name in the UI.
  28. */
  29. public function ui_title();
  30. /**
  31. * Return a string representing this handler's description in the UI.
  32. */
  33. public function ui_description();
  34. /**
  35. * Fetch search results based on the $search_string.
  36. *
  37. * @param $search_string
  38. * A string that contains the text to search for.
  39. *
  40. * @return
  41. * An associative array whose values are an
  42. * associative array containing:
  43. * - title: A string to use as the search result label.
  44. * - description: (optional) A string with additional information about the
  45. * result item.
  46. * - path: The URL to the item.
  47. * - group: (optional) A string with the group name for the result item.
  48. * Best practice is to use the plugin name as group name.
  49. * - addClass: (optional) A string with classes to add to the result row..
  50. */
  51. public function fetchResults($search_string);
  52. }
  53. /**
  54. * Base class for Linkit search plugins.
  55. */
  56. abstract class LinkitSearchPlugin implements LinkitSearchPluginInterface {
  57. /**
  58. * The plugin definition for this instance.
  59. *
  60. * @var array
  61. */
  62. protected $plugin;
  63. /**
  64. * The profile instance for this instance.
  65. *
  66. * @var LinkitProfile object
  67. */
  68. protected $profile;
  69. /**
  70. * Initialize this search plugin with the search plugin and the profile.
  71. *
  72. * @param $plugin
  73. * A search plugin object.
  74. *
  75. * @param LinkitProfile $profile
  76. * A LinkitProfile object.
  77. */
  78. public function __construct($plugin, LinkitProfile $profile) {
  79. $this->plugin = $plugin;
  80. $this->profile = $profile;
  81. }
  82. /**
  83. * Implements LinkitSearchPluginInterface::factory().
  84. */
  85. public static function factory($plugin, LinkitProfile $profile) {
  86. ctools_include('plugins');
  87. // Make sure that the handler class exists and that it has this class as one
  88. // of its parents.
  89. if (class_exists($plugin['handler']['class']) && is_subclass_of($plugin['handler']['class'], __CLASS__)) {
  90. return new $plugin['handler']['class']($plugin, $profile);
  91. }
  92. else {
  93. // The plugin handler class is defined but it cannot be found, so lets
  94. // instantiate the LinkitSearchPluginBroken instead.
  95. return new LinkitSearchPluginBroken($plugin, $profile);
  96. }
  97. }
  98. /**
  99. * Implements LinkitSearchPluginInterface::ui_title().
  100. */
  101. public function ui_title() {
  102. if (!isset($this->plugin['ui_title'])) {
  103. return check_plain($this->plugin['module'] . ':' . $this->plugin['name']);
  104. }
  105. return check_plain($this->plugin['ui_title']);
  106. }
  107. /**
  108. * Implements LinkitSearchPluginInterface::ui_description().
  109. */
  110. public function ui_description() {
  111. if (isset($this->plugin['ui_description'])) {
  112. return check_plain($this->plugin['ui_description']);
  113. }
  114. }
  115. /**
  116. * Generate a settings form for this handler.
  117. * Uses the standard Drupal FAPI.
  118. *
  119. * @return
  120. * An array containing any custom form elements to be displayed in the
  121. * profile editing form
  122. */
  123. public function buildSettingsForm() {}
  124. /**
  125. * Determine if the handler is considered 'broken', meaning it's a
  126. * a placeholder used when a handler can't be found.
  127. */
  128. public function broken() { }
  129. }
  130. /**
  131. * A special handler to take the place of missing or broken Linkit search
  132. * plugin handlers.
  133. */
  134. class LinkitSearchPluginBroken extends LinkitSearchPlugin {
  135. /**
  136. * Overrides LinkitSearchPlugin::ui_title().
  137. */
  138. public function ui_title() { return t('Broken/missing handler'); }
  139. /**
  140. * Overrides LinkitSearchPlugin::ui_description().
  141. */
  142. public function ui_description() {}
  143. /**
  144. * Implements LinkitSearchPluginInterface::fetchResults().
  145. */
  146. public function fetchResults($search_string) {}
  147. /**
  148. * Overrides LinkitSearchPlugin::broken().
  149. */
  150. public function broken() { return TRUE; }
  151. }