common_test.module 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. $items['common-test/html_head_link'] = array(
  54. 'title' => 'Test HTML head link',
  55. 'page callback' => 'common_test_html_head_link',
  56. 'access arguments' => array('access content'),
  57. 'type' => MENU_CALLBACK,
  58. );
  59. return $items;
  60. }
  61. /**
  62. * Redirect using drupal_goto().
  63. */
  64. function common_test_drupal_goto_redirect() {
  65. drupal_goto('common-test/drupal_goto');
  66. }
  67. /**
  68. * Redirect using drupal_goto().
  69. */
  70. function common_test_drupal_goto_redirect_advanced() {
  71. drupal_goto('common-test/drupal_goto', array('query' => array('foo' => '123')), 301);
  72. }
  73. /**
  74. * Landing page for drupal_goto().
  75. */
  76. function common_test_drupal_goto_land() {
  77. print "drupal_goto";
  78. }
  79. /**
  80. * Fail landing page for drupal_goto().
  81. */
  82. function common_test_drupal_goto_land_fail() {
  83. print "drupal_goto_fail";
  84. }
  85. /**
  86. * Implements hook_drupal_goto_alter().
  87. */
  88. function common_test_drupal_goto_alter(&$path, &$options, &$http_response_code) {
  89. if ($path == 'common-test/drupal_goto/fail') {
  90. $path = 'common-test/drupal_goto/redirect';
  91. }
  92. }
  93. /**
  94. * Implements hook_init().
  95. */
  96. function common_test_init() {
  97. if (variable_get('common_test_redirect_current_path', FALSE)) {
  98. drupal_goto(current_path());
  99. }
  100. if (variable_get('common_test_link_to_current_path', FALSE)) {
  101. drupal_set_message(l('link which should point to the current path', current_path()));
  102. }
  103. }
  104. /**
  105. * Print destination query parameter.
  106. */
  107. function common_test_destination() {
  108. $destination = drupal_get_destination();
  109. print "The destination: " . check_plain($destination['destination']);
  110. }
  111. /**
  112. * Applies #printed to an element to help test #pre_render.
  113. */
  114. function common_test_drupal_render_printing_pre_render($elements) {
  115. $elements['#printed'] = TRUE;
  116. return $elements;
  117. }
  118. /**
  119. * Implements hook_TYPE_alter().
  120. */
  121. function common_test_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  122. // Alter first argument.
  123. if (is_array($data)) {
  124. $data['foo'] = 'Drupal';
  125. }
  126. elseif (is_object($data)) {
  127. $data->foo = 'Drupal';
  128. }
  129. // Alter second argument, if present.
  130. if (isset($arg2)) {
  131. if (is_array($arg2)) {
  132. $arg2['foo'] = 'Drupal';
  133. }
  134. elseif (is_object($arg2)) {
  135. $arg2->foo = 'Drupal';
  136. }
  137. }
  138. // Try to alter third argument, if present.
  139. if (isset($arg3)) {
  140. if (is_array($arg3)) {
  141. $arg3['foo'] = 'Drupal';
  142. }
  143. elseif (is_object($arg3)) {
  144. $arg3->foo = 'Drupal';
  145. }
  146. }
  147. }
  148. /**
  149. * Implements hook_TYPE_alter() on behalf of Bartik theme.
  150. *
  151. * Same as common_test_drupal_alter_alter(), but here, we verify that themes
  152. * can also alter and come last.
  153. */
  154. function bartik_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  155. // Alter first argument.
  156. if (is_array($data)) {
  157. $data['foo'] .= ' theme';
  158. }
  159. elseif (is_object($data)) {
  160. $data->foo .= ' theme';
  161. }
  162. // Alter second argument, if present.
  163. if (isset($arg2)) {
  164. if (is_array($arg2)) {
  165. $arg2['foo'] .= ' theme';
  166. }
  167. elseif (is_object($arg2)) {
  168. $arg2->foo .= ' theme';
  169. }
  170. }
  171. // Try to alter third argument, if present.
  172. if (isset($arg3)) {
  173. if (is_array($arg3)) {
  174. $arg3['foo'] .= ' theme';
  175. }
  176. elseif (is_object($arg3)) {
  177. $arg3->foo .= ' theme';
  178. }
  179. }
  180. }
  181. /**
  182. * Implements hook_TYPE_alter() on behalf of block module.
  183. *
  184. * This is for verifying that drupal_alter(array(TYPE1, TYPE2), ...) allows
  185. * hook_module_implements_alter() to affect the order in which module
  186. * implementations are executed.
  187. */
  188. function block_drupal_alter_foo_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  189. $data['foo'] .= ' block';
  190. }
  191. /**
  192. * Implements hook_module_implements_alter().
  193. *
  194. * @see block_drupal_alter_foo_alter()
  195. */
  196. function common_test_module_implements_alter(&$implementations, $hook) {
  197. // For drupal_alter(array('drupal_alter', 'drupal_alter_foo'), ...), make the
  198. // block module implementations run after all the other modules. Note that
  199. // when drupal_alter() is called with an array of types, the first type is
  200. // considered primary and controls the module order.
  201. if ($hook == 'drupal_alter_alter' && isset($implementations['block'])) {
  202. $group = $implementations['block'];
  203. unset($implementations['block']);
  204. $implementations['block'] = $group;
  205. }
  206. }
  207. /**
  208. * Implements hook_theme().
  209. */
  210. function common_test_theme() {
  211. return array(
  212. 'common_test_foo' => array(
  213. 'variables' => array('foo' => 'foo', 'bar' => 'bar'),
  214. ),
  215. );
  216. }
  217. /**
  218. * Theme function for testing drupal_render() theming.
  219. */
  220. function theme_common_test_foo($variables) {
  221. return $variables['foo'] . $variables['bar'];
  222. }
  223. /**
  224. * Implements hook_library_alter().
  225. */
  226. function common_test_library_alter(&$libraries, $module) {
  227. if ($module == 'system' && isset($libraries['farbtastic'])) {
  228. // Change the title of Farbtastic to "Farbtastic: Altered Library".
  229. $libraries['farbtastic']['title'] = 'Farbtastic: Altered Library';
  230. // Make Farbtastic depend on jQuery Form to test library dependencies.
  231. $libraries['farbtastic']['dependencies'][] = array('system', 'form');
  232. }
  233. }
  234. /**
  235. * Implements hook_library().
  236. *
  237. * Adds Farbtastic in a different version.
  238. */
  239. function common_test_library() {
  240. $libraries['farbtastic'] = array(
  241. 'title' => 'Custom Farbtastic Library',
  242. 'website' => 'http://code.google.com/p/farbtastic/',
  243. 'version' => '5.3',
  244. 'js' => array(
  245. 'misc/farbtastic/farbtastic.js' => array(),
  246. ),
  247. 'css' => array(
  248. 'misc/farbtastic/farbtastic.css' => array(),
  249. ),
  250. );
  251. return $libraries;
  252. }
  253. /**
  254. * Adds a JavaScript file and a CSS file with a query string appended.
  255. */
  256. function common_test_js_and_css_querystring() {
  257. drupal_add_js(drupal_get_path('module', 'node') . '/node.js');
  258. drupal_add_css(drupal_get_path('module', 'node') . '/node.css');
  259. // A relative URI may have a query string.
  260. drupal_add_css('/' . drupal_get_path('module', 'node') . '/node-fake.css?arg1=value1&arg2=value2');
  261. return '';
  262. }
  263. /**
  264. * Implements hook_cron().
  265. *
  266. * System module should handle if a module does not catch an exception and keep
  267. * cron going.
  268. *
  269. * @see common_test_cron_helper()
  270. *
  271. */
  272. function common_test_cron() {
  273. throw new Exception(t('Uncaught exception'));
  274. }
  275. /**
  276. * Page callback.
  277. */
  278. function common_test_html_head_link() {
  279. drupal_add_html_head_link(array(
  280. 'href' => '/foo?bar=baz',
  281. 'rel' => 'alternate',
  282. ), TRUE);
  283. drupal_add_html_head_link(array(
  284. 'href' => '/not-added-to-http-headers',
  285. 'rel' => 'alternate',
  286. ), FALSE);
  287. drupal_add_html_head_link(array(
  288. 'href' => '/foo/bar',
  289. 'hreflang' => 'nl',
  290. 'rel' => 'alternate',
  291. ), TRUE);
  292. drupal_add_html_head_link(array(
  293. 'href' => '/foo/bar',
  294. 'hreflang' => 'de',
  295. 'rel' => 'alternate',
  296. ), TRUE);
  297. drupal_add_html_head_link(array(
  298. 'href' => '/foo?bar=baz&baz=false',
  299. 'hreflang' => 'en',
  300. 'rel' => 'alternate',
  301. ), TRUE);
  302. return '';
  303. }