5 * Helper module for the Common tests.
8 use \Drupal\Core\Asset\AttachedAssetsInterface;
11 * Applies #printed to an element to help test #pre_render.
13 function common_test_drupal_render_printing_pre_render($elements) {
14 $elements['#printed'] = TRUE;
19 * Implements hook_TYPE_alter().
21 function common_test_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
22 // Alter first argument.
23 if (is_array($data)) {
24 $data['foo'] = 'Drupal';
26 elseif (is_object($data)) {
27 $data->foo = 'Drupal';
29 // Alter second argument, if present.
31 if (is_array($arg2)) {
32 $arg2['foo'] = 'Drupal';
34 elseif (is_object($arg2)) {
35 $arg2->foo = 'Drupal';
38 // Try to alter third argument, if present.
40 if (is_array($arg3)) {
41 $arg3['foo'] = 'Drupal';
43 elseif (is_object($arg3)) {
44 $arg3->foo = 'Drupal';
50 * Implements hook_TYPE_alter() on behalf of Bartik theme.
52 * Same as common_test_drupal_alter_alter(), but here, we verify that themes
53 * can also alter and come last.
55 function bartik_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
56 // Alter first argument.
57 if (is_array($data)) {
58 $data['foo'] .= ' theme';
60 elseif (is_object($data)) {
61 $data->foo .= ' theme';
63 // Alter second argument, if present.
65 if (is_array($arg2)) {
66 $arg2['foo'] .= ' theme';
68 elseif (is_object($arg2)) {
69 $arg2->foo .= ' theme';
72 // Try to alter third argument, if present.
74 if (is_array($arg3)) {
75 $arg3['foo'] .= ' theme';
77 elseif (is_object($arg3)) {
78 $arg3->foo .= ' theme';
84 * Implements hook_TYPE_alter() on behalf of block module.
86 * This is to verify that
87 * \Drupal::moduleHandler()->alter(array(TYPE1, TYPE2), ...) allows
88 * hook_module_implements_alter() to affect the order in which module
89 * implementations are executed.
91 function block_drupal_alter_foo_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
92 $data['foo'] .= ' block';
96 * Implements hook_module_implements_alter().
98 * @see block_drupal_alter_foo_alter()
100 function common_test_module_implements_alter(&$implementations, $hook) {
102 // \Drupal::moduleHandler()->alter(array('drupal_alter', 'drupal_alter_foo'), ...),
103 // make the block module implementations run after all the other modules. Note
104 // that when \Drupal::moduleHandler->alter() is called with an array of types,
105 // the first type is considered primary and controls the module order.
106 if ($hook == 'drupal_alter_alter' && isset($implementations['block'])) {
107 $group = $implementations['block'];
108 unset($implementations['block']);
109 $implementations['block'] = $group;
114 * Implements hook_theme().
116 function common_test_theme() {
118 'common_test_foo' => [
119 'variables' => ['foo' => 'foo', 'bar' => 'bar'],
121 'common_test_render_element' => [
122 'render element' => 'foo',
124 'common_test_empty' => [
125 'variables' => ['foo' => 'foo'],
126 'function' => 'theme_common_test_empty',
132 * Provides a theme function for drupal_render().
134 function theme_common_test_foo($variables) {
135 return $variables['foo'] . $variables['bar'];
139 * Always returns an empty string.
141 function theme_common_test_empty($variables) {
146 * Implements MODULE_preprocess().
148 * @see RenderTest::testDrupalRenderThemePreprocessAttached()
150 function common_test_preprocess(&$variables, $hook) {
151 if (!\Drupal::state()->get('theme_preprocess_attached_test', FALSE)) {
154 $variables['#attached']['library'][] = 'test/generic_preprocess';
158 * Implements MODULE_preprocess_HOOK().
160 * @see RenderTest::testDrupalRenderThemePreprocessAttached()
162 function common_test_preprocess_common_test_render_element(&$variables) {
163 if (!\Drupal::state()->get('theme_preprocess_attached_test', FALSE)) {
166 $variables['#attached']['library'][] = 'test/specific_preprocess';
170 * Implements hook_library_info_build().
172 function common_test_library_info_build() {
174 if (\Drupal::state()->get('common_test.library_info_build_test')) {
175 $libraries['dynamic_library'] = [
179 'common_test.css' => [],
188 * Implements hook_library_info_alter().
190 function common_test_library_info_alter(&$libraries, $module) {
191 if ($module == 'core' && isset($libraries['jquery.farbtastic'])) {
192 // Change the version of Farbtastic to 0.0.
193 $libraries['jquery.farbtastic']['version'] = '0.0';
194 // Make Farbtastic depend on jQuery Form to test library dependencies.
195 $libraries['jquery.farbtastic']['dependencies'][] = 'core/jquery.form';
198 // Alter the dynamically registered library definition.
199 if ($module == 'common_test' && isset($libraries['dynamic_library'])) {
200 $libraries['dynamic_library']['dependencies'] = [
207 * Implements hook_cron().
209 * System module should handle if a module does not catch an exception and keep
212 * @see common_test_cron_helper()
214 function common_test_cron() {
215 throw new Exception(t('Uncaught exception'));
219 * Implements hook_page_attachments().
221 * @see \Drupal\system\Tests\Common\PageRenderTest::assertPageRenderHookExceptions()
223 function common_test_page_attachments(array &$page) {
224 $page['#attached']['library'][] = 'core/foo';
225 $page['#attached']['library'][] = 'core/bar';
226 $page['#cache']['tags'] = ['example'];
227 $page['#cache']['contexts'] = ['user.permissions'];
229 if (\Drupal::state()->get('common_test.hook_page_attachments.descendant_attached', FALSE)) {
230 $page['content']['#attached']['library'][] = 'core/jquery';
233 if (\Drupal::state()->get('common_test.hook_page_attachments.render_array', FALSE)) {
234 $page['something'] = [
241 * Implements hook_page_attachments_alter().
243 * @see \Drupal\system\Tests\Common\PageRenderTest::assertPageRenderHookExceptions()
245 function common_test_page_attachments_alter(array &$page) {
246 // Remove a library that was added in common_test_page_attachments(), to test
247 // that this hook can do what it claims to do.
248 if (isset($page['#attached']['library']) && ($index = array_search('core/bar', $page['#attached']['library'])) && $index !== FALSE) {
249 unset($page['#attached']['library'][$index]);
251 $page['#attached']['library'][] = 'core/baz';
252 $page['#cache']['tags'] = ['example'];
253 $page['#cache']['contexts'] = ['user.permissions'];
255 if (\Drupal::state()->get('common_test.hook_page_attachments_alter.descendant_attached', FALSE)) {
256 $page['content']['#attached']['library'][] = 'core/jquery';
259 if (\Drupal::state()->get('common_test.hook_page_attachments_alter.render_array', FALSE)) {
260 $page['something'] = [
267 * Implements hook_js_settings_alter().
269 * @see \Drupal\system\Tests\Common\JavaScriptTest::testHeaderSetting()
271 function common_test_js_settings_alter(&$settings, AttachedAssetsInterface $assets) {
272 // Modify an existing setting.
273 if (array_key_exists('pluralDelimiter', $settings)) {
274 $settings['pluralDelimiter'] = '☃';
278 $settings['foo'] = 'bar';