1. 8.3.x core/modules/system/tests/modules/common_test/common_test.module
  2. 8.0.x core/modules/system/tests/modules/common_test/common_test.module
  3. 8.1.x core/modules/system/tests/modules/common_test/common_test.module
  4. 8.2.x core/modules/system/tests/modules/common_test/common_test.module
  5. 8.4.x core/modules/system/tests/modules/common_test/common_test.module
  6. 7.x modules/simpletest/tests/common_test.module

Helper module for the Common tests.

File

core/modules/system/tests/modules/common_test/common_test.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Helper module for the Common tests.
  5. */
  6. use \Drupal\Core\Asset\AttachedAssetsInterface;
  7. /**
  8. * Applies #printed to an element to help test #pre_render.
  9. */
  10. function common_test_drupal_render_printing_pre_render($elements) {
  11. $elements['#printed'] = TRUE;
  12. return $elements;
  13. }
  14. /**
  15. * Implements hook_TYPE_alter().
  16. */
  17. function common_test_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  18. // Alter first argument.
  19. if (is_array($data)) {
  20. $data['foo'] = 'Drupal';
  21. }
  22. elseif (is_object($data)) {
  23. $data->foo = 'Drupal';
  24. }
  25. // Alter second argument, if present.
  26. if (isset($arg2)) {
  27. if (is_array($arg2)) {
  28. $arg2['foo'] = 'Drupal';
  29. }
  30. elseif (is_object($arg2)) {
  31. $arg2->foo = 'Drupal';
  32. }
  33. }
  34. // Try to alter third argument, if present.
  35. if (isset($arg3)) {
  36. if (is_array($arg3)) {
  37. $arg3['foo'] = 'Drupal';
  38. }
  39. elseif (is_object($arg3)) {
  40. $arg3->foo = 'Drupal';
  41. }
  42. }
  43. }
  44. /**
  45. * Implements hook_TYPE_alter() on behalf of Bartik theme.
  46. *
  47. * Same as common_test_drupal_alter_alter(), but here, we verify that themes
  48. * can also alter and come last.
  49. */
  50. function bartik_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  51. // Alter first argument.
  52. if (is_array($data)) {
  53. $data['foo'] .= ' theme';
  54. }
  55. elseif (is_object($data)) {
  56. $data->foo .= ' theme';
  57. }
  58. // Alter second argument, if present.
  59. if (isset($arg2)) {
  60. if (is_array($arg2)) {
  61. $arg2['foo'] .= ' theme';
  62. }
  63. elseif (is_object($arg2)) {
  64. $arg2->foo .= ' theme';
  65. }
  66. }
  67. // Try to alter third argument, if present.
  68. if (isset($arg3)) {
  69. if (is_array($arg3)) {
  70. $arg3['foo'] .= ' theme';
  71. }
  72. elseif (is_object($arg3)) {
  73. $arg3->foo .= ' theme';
  74. }
  75. }
  76. }
  77. /**
  78. * Implements hook_TYPE_alter() on behalf of block module.
  79. *
  80. * This is to verify that
  81. * \Drupal::moduleHandler()->alter(array(TYPE1, TYPE2), ...) allows
  82. * hook_module_implements_alter() to affect the order in which module
  83. * implementations are executed.
  84. */
  85. function block_drupal_alter_foo_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  86. $data['foo'] .= ' block';
  87. }
  88. /**
  89. * Implements hook_module_implements_alter().
  90. *
  91. * @see block_drupal_alter_foo_alter()
  92. */
  93. function common_test_module_implements_alter(&$implementations, $hook) {
  94. // For
  95. // \Drupal::moduleHandler()->alter(array('drupal_alter', 'drupal_alter_foo'), ...),
  96. // make the block module implementations run after all the other modules. Note
  97. // that when \Drupal::moduleHandler->alter() is called with an array of types,
  98. // the first type is considered primary and controls the module order.
  99. if ($hook == 'drupal_alter_alter' && isset($implementations['block'])) {
  100. $group = $implementations['block'];
  101. unset($implementations['block']);
  102. $implementations['block'] = $group;
  103. }
  104. }
  105. /**
  106. * Implements hook_theme().
  107. */
  108. function common_test_theme() {
  109. return array(
  110. 'common_test_foo' => array(
  111. 'variables' => array('foo' => 'foo', 'bar' => 'bar'),
  112. ),
  113. 'common_test_render_element' => array(
  114. 'render element' => 'foo',
  115. ),
  116. 'common_test_empty' => array(
  117. 'variables' => array('foo' => 'foo'),
  118. 'function' => 'theme_common_test_empty',
  119. ),
  120. );
  121. }
  122. /**
  123. * Provides a theme function for drupal_render().
  124. */
  125. function theme_common_test_foo($variables) {
  126. return $variables['foo'] . $variables['bar'];
  127. }
  128. /**
  129. * Always returns an empty string.
  130. */
  131. function theme_common_test_empty($variables) {
  132. return '';
  133. }
  134. /**
  135. * Implements MODULE_preprocess().
  136. *
  137. * @see RenderTest::testDrupalRenderThemePreprocessAttached()
  138. */
  139. function common_test_preprocess(&$variables, $hook) {
  140. if (!\Drupal::state()->get('theme_preprocess_attached_test', FALSE)) {
  141. return;
  142. }
  143. $variables['#attached']['library'][] = 'test/generic_preprocess';
  144. }
  145. /**
  146. * Implements MODULE_preprocess_HOOK().
  147. *
  148. * @see RenderTest::testDrupalRenderThemePreprocessAttached()
  149. */
  150. function common_test_preprocess_common_test_render_element(&$variables) {
  151. if (!\Drupal::state()->get('theme_preprocess_attached_test', FALSE)) {
  152. return;
  153. }
  154. $variables['#attached']['library'][] = 'test/specific_preprocess';
  155. }
  156. /**
  157. * Implements hook_library_info_build().
  158. */
  159. function common_test_library_info_build() {
  160. $libraries = [];
  161. if (\Drupal::state()->get('common_test.library_info_build_test')) {
  162. $libraries['dynamic_library'] = [
  163. 'version' => '1.0',
  164. 'css' => [
  165. 'base' => [
  166. 'common_test.css' => [],
  167. ],
  168. ],
  169. ];
  170. }
  171. return $libraries;
  172. }
  173. /**
  174. * Implements hook_library_info_alter().
  175. */
  176. function common_test_library_info_alter(&$libraries, $module) {
  177. if ($module == 'core' && isset($libraries['jquery.farbtastic'])) {
  178. // Change the version of Farbtastic to 0.0.
  179. $libraries['jquery.farbtastic']['version'] = '0.0';
  180. // Make Farbtastic depend on jQuery Form to test library dependencies.
  181. $libraries['jquery.farbtastic']['dependencies'][] = 'core/jquery.form';
  182. }
  183. // Alter the dynamically registered library definition.
  184. if ($module == 'common_test' && isset($libraries['dynamic_library'])) {
  185. $libraries['dynamic_library']['dependencies'] = [
  186. 'core/jquery',
  187. ];
  188. }
  189. }
  190. /**
  191. * Implements hook_cron().
  192. *
  193. * System module should handle if a module does not catch an exception and keep
  194. * cron going.
  195. *
  196. * @see common_test_cron_helper()
  197. */
  198. function common_test_cron() {
  199. throw new Exception(t('Uncaught exception'));
  200. }
  201. /**
  202. * Implements hook_page_attachments().
  203. *
  204. * @see \Drupal\system\Tests\Common\PageRenderTest::assertPageRenderHookExceptions()
  205. */
  206. function common_test_page_attachments(array &$page) {
  207. $page['#attached']['library'][] = 'core/foo';
  208. $page['#attached']['library'][] = 'core/bar';
  209. $page['#cache']['tags'] = ['example'];
  210. $page['#cache']['contexts'] = ['user.permissions'];
  211. if (\Drupal::state()->get('common_test.hook_page_attachments.descendant_attached', FALSE)) {
  212. $page['content']['#attached']['library'][] = 'core/jquery';
  213. }
  214. if (\Drupal::state()->get('common_test.hook_page_attachments.render_array', FALSE)) {
  215. $page['something'] = [
  216. '#markup' => 'test',
  217. ];
  218. }
  219. }
  220. /**
  221. * Implements hook_page_attachments_alter().
  222. *
  223. * @see \Drupal\system\Tests\Common\PageRenderTest::assertPageRenderHookExceptions()
  224. */
  225. function common_test_page_attachments_alter(array &$page) {
  226. // Remove a library that was added in common_test_page_attachments(), to test
  227. // that this hook can do what it claims to do.
  228. if (isset($page['#attached']['library']) && ($index = array_search('core/bar', $page['#attached']['library'])) && $index !== FALSE) {
  229. unset($page['#attached']['library'][$index]);
  230. }
  231. $page['#attached']['library'][] = 'core/baz';
  232. $page['#cache']['tags'] = ['example'];
  233. $page['#cache']['contexts'] = ['user.permissions'];
  234. if (\Drupal::state()->get('common_test.hook_page_attachments_alter.descendant_attached', FALSE)) {
  235. $page['content']['#attached']['library'][] = 'core/jquery';
  236. }
  237. if (\Drupal::state()->get('common_test.hook_page_attachments_alter.render_array', FALSE)) {
  238. $page['something'] = [
  239. '#markup' => 'test',
  240. ];
  241. }
  242. }
  243. /**
  244. * Implements hook_js_settings_alter().
  245. *
  246. * @see \Drupal\system\Tests\Common\JavaScriptTest::testHeaderSetting()
  247. */
  248. function common_test_js_settings_alter(&$settings, AttachedAssetsInterface $assets) {
  249. // Modify an existing setting.
  250. if (array_key_exists('pluralDelimiter', $settings)) {
  251. $settings['pluralDelimiter'] = '☃';
  252. }
  253. // Add a setting.
  254. $settings['foo'] = 'bar';
  255. }

Functions

Namesort descending Description
bartik_drupal_alter_alter Implements hook_TYPE_alter() on behalf of Bartik theme.
block_drupal_alter_foo_alter Implements hook_TYPE_alter() on behalf of block module.
common_test_cron Implements hook_cron().
common_test_drupal_alter_alter Implements hook_TYPE_alter().
common_test_drupal_render_printing_pre_render Applies #printed to an element to help test #pre_render.
common_test_js_settings_alter Implements hook_js_settings_alter().
common_test_library_info_alter Implements hook_library_info_alter().
common_test_library_info_build Implements hook_library_info_build().
common_test_module_implements_alter Implements hook_module_implements_alter().
common_test_page_attachments Implements hook_page_attachments().
common_test_page_attachments_alter Implements hook_page_attachments_alter().
common_test_preprocess Implements MODULE_preprocess().
common_test_preprocess_common_test_render_element Implements MODULE_preprocess_HOOK().
common_test_theme Implements hook_theme().
theme_common_test_empty Always returns an empty string.
theme_common_test_foo Provides a theme function for drupal_render().