facetapi_test.module 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @file
  4. * Provides a test adapter and plugins.
  5. */
  6. /**
  7. * Placeholder text on the admin settings form.
  8. */
  9. define('FACETAPI_TEST_FORM_TEXT', t('Facet API test form'));
  10. /**
  11. * Implements hook_menu().
  12. */
  13. function facetapi_test_menu() {
  14. $items = array();
  15. $items['admin/config/search/facetapi_test'] = array(
  16. 'title' => 'Facet API test',
  17. 'description' => 'Settings for the Facet API module.',
  18. 'page callback' => 'drupal_get_form',
  19. 'page arguments' => array('facetapi_test_admin_settings'),
  20. 'access arguments' => array('administer search'),
  21. );
  22. $items['admin/config/search/facetapi_test/settings'] = array(
  23. 'title' => 'Settings',
  24. 'weight' => -10,
  25. 'type' => MENU_DEFAULT_LOCAL_TASK,
  26. );
  27. $items['facetapi_test/search'] = array(
  28. 'title' => 'Search',
  29. 'description' => 'Fake search page callback.',
  30. 'page callback' => 'facetapi_test_search_callback',
  31. 'access arguments' => array('access content'),
  32. 'type' => MENU_NORMAL_ITEM,
  33. );
  34. return $items;
  35. }
  36. /**
  37. * Implements hook_ctools_plugin_api().
  38. */
  39. function facetapi_test_ctools_plugin_api($owner, $api) {
  40. if ($owner == 'current_search' && $api == 'current_search') {
  41. return array('version' => 1);
  42. }
  43. }
  44. /**
  45. * Implements hook_facetapi_searcher_info().
  46. */
  47. function facetapi_test_facetapi_searcher_info() {
  48. $info = array();
  49. $info['facetapi_test'] = array(
  50. 'label' => t('Facet API Test'),
  51. 'adapter' => 'facetapi_test',
  52. 'types' => array('test'),
  53. 'path' => 'admin/config/search/facetapi_test',
  54. 'supports facet missing' => FALSE,
  55. );
  56. $info['facetapi_test2'] = array(
  57. 'label' => t('Facet API Seond Searcher'),
  58. 'adapter' => 'facetapi_test',
  59. 'types' => array('test'),
  60. 'supports facet missing' => FALSE,
  61. );
  62. return $info;
  63. }
  64. /**
  65. * Implements hook_facetapi_translate_string().
  66. *
  67. * Returns a serialized array for testing.
  68. */
  69. function facetapi_test_facetapi_translate_string($name, $string, $langcode = NULL) {
  70. return serialize(array(
  71. 'name' => $name,
  72. 'string' => $string,
  73. 'langcode' => (string) $langcode,
  74. 'return' => 'translated',
  75. ));
  76. }
  77. /**
  78. * Placeholder for the admin settings form.
  79. */
  80. function facetapi_test_admin_settings($form, &$form_state) {
  81. $form['text'] = array('#markup' => FACETAPI_TEST_FORM_TEXT);
  82. return $form;
  83. }
  84. /**
  85. * Implements hook_facetapi_facet_info().
  86. */
  87. function facetapi_test_facetapi_facet_info($searcher_info) {
  88. $facets = array();
  89. if ('test' == $searcher_info['type']) {
  90. $facets['enabled'] = array(
  91. 'label' => t('Enabled facet'),
  92. 'description' => t('Facet that tests enabling.'),
  93. 'dependency plugins' => array('role'),
  94. );
  95. $facets['disabled'] = array(
  96. 'label' => t('Disabled facet'),
  97. 'description' => t('Facet that tests disabling.'),
  98. 'dependency plugins' => array('role'),
  99. );
  100. $facets['colon:test'] = array(
  101. 'label' => t('Colon test'),
  102. 'description' => t('Test facets names with colons.'),
  103. 'dependency plugins' => array('role'),
  104. );
  105. $facets['defaults'] = array(
  106. 'label' => t('Default test'),
  107. 'description' => t('Tests query type and operator defaults stored in settings.'),
  108. 'dependency plugins' => array('role'),
  109. 'query types' => array('nonterm'),
  110. 'default widget' => 'nonterm',
  111. 'allowed operators' => array(
  112. FACETAPI_OPERATOR_AND => FALSE,
  113. FACETAPI_OPERATOR_OR => TRUE
  114. ),
  115. );
  116. $facets['hierarchical'] = array(
  117. 'label' => t('Hierarchical test'),
  118. 'description' => t('Test hierarchical facets.'),
  119. 'dependency plugins' => array('role'),
  120. 'hierarchy callback' => 'facetapi_test_process_hierarchy',
  121. );
  122. }
  123. return $facets;
  124. }
  125. /**
  126. * Hierarchy processing callback.
  127. *
  128. * @param array $values
  129. * An array containing the term ids.
  130. *
  131. * @return
  132. * An associative array keyed by term ID to parent ID.
  133. */
  134. function facetapi_test_process_hierarchy(array $values) {
  135. }
  136. /**
  137. * Fake search page callback.
  138. */
  139. function facetapi_test_search_callback() {
  140. $build = array();
  141. // Load adapter, throw exception on error.
  142. if (!$adapter = facetapi_adapter_load('facetapi_test')) {
  143. throw new Exception(t('Error loading adapter.'));
  144. }
  145. // Set result count if passed through query string.
  146. if (isset($_GET['count'])) {
  147. $adapter->setResultCount((int) $_GET['count']);
  148. }
  149. // Instantiate a dummy query class, initialize facets.
  150. $query = new stdClass();
  151. $adapter->addActiveFilters($query);
  152. // Set keywords if passed through query string.
  153. $keys = isset($_GET['keys']) ? $_GET['keys'] : arg(2);
  154. $adapter->setSearchKeys($keys);
  155. $build['placeholder'] = array(
  156. '#markup' => t('Placeholder'),
  157. );
  158. return $build;
  159. }