common_test.module 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * @file
  4. * Helper module for the Common tests.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function common_test_menu() {
  10. $items['common-test/drupal_goto'] = array(
  11. 'title' => 'Drupal Goto',
  12. 'page callback' => 'common_test_drupal_goto_land',
  13. 'access arguments' => array('access content'),
  14. 'type' => MENU_CALLBACK,
  15. );
  16. $items['common-test/drupal_goto/fail'] = array(
  17. 'title' => 'Drupal Goto',
  18. 'page callback' => 'common_test_drupal_goto_land_fail',
  19. 'access arguments' => array('access content'),
  20. 'type' => MENU_CALLBACK,
  21. );
  22. $items['common-test/drupal_goto/redirect'] = array(
  23. 'title' => 'Drupal Goto',
  24. 'page callback' => 'common_test_drupal_goto_redirect',
  25. 'access arguments' => array('access content'),
  26. 'type' => MENU_CALLBACK,
  27. );
  28. $items['common-test/drupal_goto/redirect_advanced'] = array(
  29. 'title' => 'Drupal Goto',
  30. 'page callback' => 'common_test_drupal_goto_redirect_advanced',
  31. 'access arguments' => array('access content'),
  32. 'type' => MENU_CALLBACK,
  33. );
  34. $items['common-test/drupal_goto/redirect_fail'] = array(
  35. 'title' => 'Drupal Goto Failure',
  36. 'page callback' => 'drupal_goto',
  37. 'page arguments' => array('common-test/drupal_goto/fail'),
  38. 'access arguments' => array('access content'),
  39. 'type' => MENU_CALLBACK,
  40. );
  41. $items['common-test/destination'] = array(
  42. 'title' => 'Drupal Get Destination',
  43. 'page callback' => 'common_test_destination',
  44. 'access arguments' => array('access content'),
  45. 'type' => MENU_CALLBACK,
  46. );
  47. $items['common-test/query-string'] = array(
  48. 'title' => 'Test querystring',
  49. 'page callback' => 'common_test_js_and_css_querystring',
  50. 'access arguments' => array('access content'),
  51. 'type' => MENU_CALLBACK,
  52. );
  53. return $items;
  54. }
  55. /**
  56. * Redirect using drupal_goto().
  57. */
  58. function common_test_drupal_goto_redirect() {
  59. drupal_goto('common-test/drupal_goto');
  60. }
  61. /**
  62. * Redirect using drupal_goto().
  63. */
  64. function common_test_drupal_goto_redirect_advanced() {
  65. drupal_goto('common-test/drupal_goto', array('query' => array('foo' => '123')), 301);
  66. }
  67. /**
  68. * Landing page for drupal_goto().
  69. */
  70. function common_test_drupal_goto_land() {
  71. print "drupal_goto";
  72. }
  73. /**
  74. * Fail landing page for drupal_goto().
  75. */
  76. function common_test_drupal_goto_land_fail() {
  77. print "drupal_goto_fail";
  78. }
  79. /**
  80. * Implements hook_drupal_goto_alter().
  81. */
  82. function common_test_drupal_goto_alter(&$path, &$options, &$http_response_code) {
  83. if ($path == 'common-test/drupal_goto/fail') {
  84. $path = 'common-test/drupal_goto/redirect';
  85. }
  86. }
  87. /**
  88. * Print destination query parameter.
  89. */
  90. function common_test_destination() {
  91. $destination = drupal_get_destination();
  92. print "The destination: " . check_plain($destination['destination']);
  93. }
  94. /**
  95. * Implements hook_TYPE_alter().
  96. */
  97. function common_test_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  98. // Alter first argument.
  99. if (is_array($data)) {
  100. $data['foo'] = 'Drupal';
  101. }
  102. elseif (is_object($data)) {
  103. $data->foo = 'Drupal';
  104. }
  105. // Alter second argument, if present.
  106. if (isset($arg2)) {
  107. if (is_array($arg2)) {
  108. $arg2['foo'] = 'Drupal';
  109. }
  110. elseif (is_object($arg2)) {
  111. $arg2->foo = 'Drupal';
  112. }
  113. }
  114. // Try to alter third argument, if present.
  115. if (isset($arg3)) {
  116. if (is_array($arg3)) {
  117. $arg3['foo'] = 'Drupal';
  118. }
  119. elseif (is_object($arg3)) {
  120. $arg3->foo = 'Drupal';
  121. }
  122. }
  123. }
  124. /**
  125. * Implements hook_TYPE_alter() on behalf of Bartik theme.
  126. *
  127. * Same as common_test_drupal_alter_alter(), but here, we verify that themes
  128. * can also alter and come last.
  129. */
  130. function bartik_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  131. // Alter first argument.
  132. if (is_array($data)) {
  133. $data['foo'] .= ' theme';
  134. }
  135. elseif (is_object($data)) {
  136. $data->foo .= ' theme';
  137. }
  138. // Alter second argument, if present.
  139. if (isset($arg2)) {
  140. if (is_array($arg2)) {
  141. $arg2['foo'] .= ' theme';
  142. }
  143. elseif (is_object($arg2)) {
  144. $arg2->foo .= ' theme';
  145. }
  146. }
  147. // Try to alter third argument, if present.
  148. if (isset($arg3)) {
  149. if (is_array($arg3)) {
  150. $arg3['foo'] .= ' theme';
  151. }
  152. elseif (is_object($arg3)) {
  153. $arg3->foo .= ' theme';
  154. }
  155. }
  156. }
  157. /**
  158. * Implements hook_TYPE_alter() on behalf of block module.
  159. *
  160. * This is for verifying that drupal_alter(array(TYPE1, TYPE2), ...) allows
  161. * hook_module_implements_alter() to affect the order in which module
  162. * implementations are executed.
  163. */
  164. function block_drupal_alter_foo_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  165. $data['foo'] .= ' block';
  166. }
  167. /**
  168. * Implements hook_module_implements_alter().
  169. *
  170. * @see block_drupal_alter_foo_alter()
  171. */
  172. function common_test_module_implements_alter(&$implementations, $hook) {
  173. // For drupal_alter(array('drupal_alter', 'drupal_alter_foo'), ...), make the
  174. // block module implementations run after all the other modules. Note that
  175. // when drupal_alter() is called with an array of types, the first type is
  176. // considered primary and controls the module order.
  177. if ($hook == 'drupal_alter_alter' && isset($implementations['block'])) {
  178. $group = $implementations['block'];
  179. unset($implementations['block']);
  180. $implementations['block'] = $group;
  181. }
  182. }
  183. /**
  184. * Implements hook_theme().
  185. */
  186. function common_test_theme() {
  187. return array(
  188. 'common_test_foo' => array(
  189. 'variables' => array('foo' => 'foo', 'bar' => 'bar'),
  190. ),
  191. );
  192. }
  193. /**
  194. * Theme function for testing drupal_render() theming.
  195. */
  196. function theme_common_test_foo($variables) {
  197. return $variables['foo'] . $variables['bar'];
  198. }
  199. /**
  200. * Implements hook_library_alter().
  201. */
  202. function common_test_library_alter(&$libraries, $module) {
  203. if ($module == 'system' && isset($libraries['farbtastic'])) {
  204. // Change the title of Farbtastic to "Farbtastic: Altered Library".
  205. $libraries['farbtastic']['title'] = 'Farbtastic: Altered Library';
  206. // Make Farbtastic depend on jQuery Form to test library dependencies.
  207. $libraries['farbtastic']['dependencies'][] = array('system', 'form');
  208. }
  209. }
  210. /**
  211. * Implements hook_library().
  212. *
  213. * Adds Farbtastic in a different version.
  214. */
  215. function common_test_library() {
  216. $libraries['farbtastic'] = array(
  217. 'title' => 'Custom Farbtastic Library',
  218. 'website' => 'http://code.google.com/p/farbtastic/',
  219. 'version' => '5.3',
  220. 'js' => array(
  221. 'misc/farbtastic/farbtastic.js' => array(),
  222. ),
  223. 'css' => array(
  224. 'misc/farbtastic/farbtastic.css' => array(),
  225. ),
  226. );
  227. return $libraries;
  228. }
  229. /**
  230. * Adds a JavaScript file and a CSS file with a query string appended.
  231. */
  232. function common_test_js_and_css_querystring() {
  233. drupal_add_js(drupal_get_path('module', 'node') . '/node.js');
  234. drupal_add_css(drupal_get_path('module', 'node') . '/node.css');
  235. // A relative URI may have a query string.
  236. drupal_add_css('/' . drupal_get_path('module', 'node') . '/node-fake.css?arg1=value1&arg2=value2');
  237. return '';
  238. }
  239. /**
  240. * Implements hook_cron().
  241. *
  242. * System module should handle if a module does not catch an exception and keep
  243. * cron going.
  244. *
  245. * @see common_test_cron_helper()
  246. *
  247. */
  248. function common_test_cron() {
  249. throw new Exception(t('Uncaught exception'));
  250. }