common_test.module 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. * Implements hook_init().
  89. */
  90. function common_test_init() {
  91. if (variable_get('common_test_redirect_current_path', FALSE)) {
  92. drupal_goto(current_path());
  93. }
  94. if (variable_get('common_test_link_to_current_path', FALSE)) {
  95. drupal_set_message(l('link which should point to the current path', current_path()));
  96. }
  97. }
  98. /**
  99. * Print destination query parameter.
  100. */
  101. function common_test_destination() {
  102. $destination = drupal_get_destination();
  103. print "The destination: " . check_plain($destination['destination']);
  104. }
  105. /**
  106. * Applies #printed to an element to help test #pre_render.
  107. */
  108. function common_test_drupal_render_printing_pre_render($elements) {
  109. $elements['#printed'] = TRUE;
  110. return $elements;
  111. }
  112. /**
  113. * Implements hook_TYPE_alter().
  114. */
  115. function common_test_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  116. // Alter first argument.
  117. if (is_array($data)) {
  118. $data['foo'] = 'Drupal';
  119. }
  120. elseif (is_object($data)) {
  121. $data->foo = 'Drupal';
  122. }
  123. // Alter second argument, if present.
  124. if (isset($arg2)) {
  125. if (is_array($arg2)) {
  126. $arg2['foo'] = 'Drupal';
  127. }
  128. elseif (is_object($arg2)) {
  129. $arg2->foo = 'Drupal';
  130. }
  131. }
  132. // Try to alter third argument, if present.
  133. if (isset($arg3)) {
  134. if (is_array($arg3)) {
  135. $arg3['foo'] = 'Drupal';
  136. }
  137. elseif (is_object($arg3)) {
  138. $arg3->foo = 'Drupal';
  139. }
  140. }
  141. }
  142. /**
  143. * Implements hook_TYPE_alter() on behalf of Bartik theme.
  144. *
  145. * Same as common_test_drupal_alter_alter(), but here, we verify that themes
  146. * can also alter and come last.
  147. */
  148. function bartik_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  149. // Alter first argument.
  150. if (is_array($data)) {
  151. $data['foo'] .= ' theme';
  152. }
  153. elseif (is_object($data)) {
  154. $data->foo .= ' theme';
  155. }
  156. // Alter second argument, if present.
  157. if (isset($arg2)) {
  158. if (is_array($arg2)) {
  159. $arg2['foo'] .= ' theme';
  160. }
  161. elseif (is_object($arg2)) {
  162. $arg2->foo .= ' theme';
  163. }
  164. }
  165. // Try to alter third argument, if present.
  166. if (isset($arg3)) {
  167. if (is_array($arg3)) {
  168. $arg3['foo'] .= ' theme';
  169. }
  170. elseif (is_object($arg3)) {
  171. $arg3->foo .= ' theme';
  172. }
  173. }
  174. }
  175. /**
  176. * Implements hook_TYPE_alter() on behalf of block module.
  177. *
  178. * This is for verifying that drupal_alter(array(TYPE1, TYPE2), ...) allows
  179. * hook_module_implements_alter() to affect the order in which module
  180. * implementations are executed.
  181. */
  182. function block_drupal_alter_foo_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  183. $data['foo'] .= ' block';
  184. }
  185. /**
  186. * Implements hook_module_implements_alter().
  187. *
  188. * @see block_drupal_alter_foo_alter()
  189. */
  190. function common_test_module_implements_alter(&$implementations, $hook) {
  191. // For drupal_alter(array('drupal_alter', 'drupal_alter_foo'), ...), make the
  192. // block module implementations run after all the other modules. Note that
  193. // when drupal_alter() is called with an array of types, the first type is
  194. // considered primary and controls the module order.
  195. if ($hook == 'drupal_alter_alter' && isset($implementations['block'])) {
  196. $group = $implementations['block'];
  197. unset($implementations['block']);
  198. $implementations['block'] = $group;
  199. }
  200. }
  201. /**
  202. * Implements hook_theme().
  203. */
  204. function common_test_theme() {
  205. return array(
  206. 'common_test_foo' => array(
  207. 'variables' => array('foo' => 'foo', 'bar' => 'bar'),
  208. ),
  209. );
  210. }
  211. /**
  212. * Theme function for testing drupal_render() theming.
  213. */
  214. function theme_common_test_foo($variables) {
  215. return $variables['foo'] . $variables['bar'];
  216. }
  217. /**
  218. * Implements hook_library_alter().
  219. */
  220. function common_test_library_alter(&$libraries, $module) {
  221. if ($module == 'system' && isset($libraries['farbtastic'])) {
  222. // Change the title of Farbtastic to "Farbtastic: Altered Library".
  223. $libraries['farbtastic']['title'] = 'Farbtastic: Altered Library';
  224. // Make Farbtastic depend on jQuery Form to test library dependencies.
  225. $libraries['farbtastic']['dependencies'][] = array('system', 'form');
  226. }
  227. }
  228. /**
  229. * Implements hook_library().
  230. *
  231. * Adds Farbtastic in a different version.
  232. */
  233. function common_test_library() {
  234. $libraries['farbtastic'] = array(
  235. 'title' => 'Custom Farbtastic Library',
  236. 'website' => 'http://code.google.com/p/farbtastic/',
  237. 'version' => '5.3',
  238. 'js' => array(
  239. 'misc/farbtastic/farbtastic.js' => array(),
  240. ),
  241. 'css' => array(
  242. 'misc/farbtastic/farbtastic.css' => array(),
  243. ),
  244. );
  245. return $libraries;
  246. }
  247. /**
  248. * Adds a JavaScript file and a CSS file with a query string appended.
  249. */
  250. function common_test_js_and_css_querystring() {
  251. drupal_add_js(drupal_get_path('module', 'node') . '/node.js');
  252. drupal_add_css(drupal_get_path('module', 'node') . '/node.css');
  253. // A relative URI may have a query string.
  254. drupal_add_css('/' . drupal_get_path('module', 'node') . '/node-fake.css?arg1=value1&arg2=value2');
  255. return '';
  256. }
  257. /**
  258. * Implements hook_cron().
  259. *
  260. * System module should handle if a module does not catch an exception and keep
  261. * cron going.
  262. *
  263. * @see common_test_cron_helper()
  264. *
  265. */
  266. function common_test_cron() {
  267. throw new Exception(t('Uncaught exception'));
  268. }