97bba7b3414d1aacde8880a675bb85ba8a26efaf
[yaffs-website] / web / core / modules / system / tests / modules / common_test / common_test.module
1 <?php
2
3 /**
4  * @file
5  * Helper module for the Common tests.
6  */
7
8 use \Drupal\Core\Asset\AttachedAssetsInterface;
9
10 /**
11  * Applies #printed to an element to help test #pre_render.
12  */
13 function common_test_drupal_render_printing_pre_render($elements) {
14   $elements['#printed'] = TRUE;
15   return $elements;
16 }
17
18 /**
19  * Implements hook_TYPE_alter().
20  */
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';
25   }
26   elseif (is_object($data)) {
27     $data->foo = 'Drupal';
28   }
29   // Alter second argument, if present.
30   if (isset($arg2)) {
31     if (is_array($arg2)) {
32       $arg2['foo'] = 'Drupal';
33     }
34     elseif (is_object($arg2)) {
35       $arg2->foo = 'Drupal';
36     }
37   }
38   // Try to alter third argument, if present.
39   if (isset($arg3)) {
40     if (is_array($arg3)) {
41       $arg3['foo'] = 'Drupal';
42     }
43     elseif (is_object($arg3)) {
44       $arg3->foo = 'Drupal';
45     }
46   }
47 }
48
49 /**
50  * Implements hook_TYPE_alter() on behalf of Bartik theme.
51  *
52  * Same as common_test_drupal_alter_alter(), but here, we verify that themes
53  * can also alter and come last.
54  */
55 function bartik_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
56   // Alter first argument.
57   if (is_array($data)) {
58     $data['foo'] .= ' theme';
59   }
60   elseif (is_object($data)) {
61     $data->foo .= ' theme';
62   }
63   // Alter second argument, if present.
64   if (isset($arg2)) {
65     if (is_array($arg2)) {
66       $arg2['foo'] .= ' theme';
67     }
68     elseif (is_object($arg2)) {
69       $arg2->foo .= ' theme';
70     }
71   }
72   // Try to alter third argument, if present.
73   if (isset($arg3)) {
74     if (is_array($arg3)) {
75       $arg3['foo'] .= ' theme';
76     }
77     elseif (is_object($arg3)) {
78       $arg3->foo .= ' theme';
79     }
80   }
81 }
82
83 /**
84  * Implements hook_TYPE_alter() on behalf of block module.
85  *
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.
90  */
91 function block_drupal_alter_foo_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
92   $data['foo'] .= ' block';
93 }
94
95 /**
96  * Implements hook_module_implements_alter().
97  *
98  * @see block_drupal_alter_foo_alter()
99  */
100 function common_test_module_implements_alter(&$implementations, $hook) {
101   // For
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;
110   }
111 }
112
113 /**
114  * Implements hook_theme().
115  */
116 function common_test_theme() {
117   return [
118     'common_test_foo' => [
119       'variables' => ['foo' => 'foo', 'bar' => 'bar'],
120     ],
121     'common_test_render_element' => [
122       'render element' => 'foo',
123     ],
124     'common_test_empty' => [
125       'variables' => ['foo' => 'foo'],
126       'function' => 'theme_common_test_empty',
127     ],
128   ];
129 }
130
131 /**
132  * Provides a theme function for drupal_render().
133  */
134 function theme_common_test_foo($variables) {
135   return $variables['foo'] . $variables['bar'];
136 }
137
138 /**
139  * Always returns an empty string.
140  */
141 function theme_common_test_empty($variables) {
142   return '';
143 }
144
145 /**
146  * Implements MODULE_preprocess().
147  *
148  * @see RenderTest::testDrupalRenderThemePreprocessAttached()
149  */
150 function common_test_preprocess(&$variables, $hook) {
151   if (!\Drupal::state()->get('theme_preprocess_attached_test', FALSE)) {
152     return;
153   }
154   $variables['#attached']['library'][] = 'test/generic_preprocess';
155 }
156
157 /**
158  * Implements MODULE_preprocess_HOOK().
159  *
160  * @see RenderTest::testDrupalRenderThemePreprocessAttached()
161  */
162 function common_test_preprocess_common_test_render_element(&$variables) {
163   if (!\Drupal::state()->get('theme_preprocess_attached_test', FALSE)) {
164     return;
165   }
166   $variables['#attached']['library'][] = 'test/specific_preprocess';
167 }
168
169 /**
170  * Implements hook_library_info_build().
171  */
172 function common_test_library_info_build() {
173   $libraries = [];
174   if (\Drupal::state()->get('common_test.library_info_build_test')) {
175     $libraries['dynamic_library'] = [
176       'version' => '1.0',
177       'css' => [
178         'base' => [
179           'common_test.css' => [],
180         ],
181       ],
182     ];
183   }
184   return $libraries;
185 }
186
187 /**
188  * Implements hook_library_info_alter().
189  */
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';
196   }
197
198   // Alter the dynamically registered library definition.
199   if ($module == 'common_test' && isset($libraries['dynamic_library'])) {
200     $libraries['dynamic_library']['dependencies'] = [
201       'core/jquery',
202     ];
203   }
204 }
205
206 /**
207  * Implements hook_cron().
208  *
209  * System module should handle if a module does not catch an exception and keep
210  * cron going.
211  *
212  * @see common_test_cron_helper()
213  */
214 function common_test_cron() {
215   throw new Exception(t('Uncaught exception'));
216 }
217
218 /**
219  * Implements hook_page_attachments().
220  *
221  * @see \Drupal\system\Tests\Common\PageRenderTest::assertPageRenderHookExceptions()
222  */
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'];
228
229   if (\Drupal::state()->get('common_test.hook_page_attachments.descendant_attached', FALSE)) {
230     $page['content']['#attached']['library'][] = 'core/jquery';
231   }
232
233   if (\Drupal::state()->get('common_test.hook_page_attachments.render_array', FALSE)) {
234     $page['something'] = [
235       '#markup' => 'test',
236     ];
237   }
238 }
239
240 /**
241  * Implements hook_page_attachments_alter().
242  *
243  * @see \Drupal\system\Tests\Common\PageRenderTest::assertPageRenderHookExceptions()
244  */
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]);
250   }
251   $page['#attached']['library'][] = 'core/baz';
252   $page['#cache']['tags'] = ['example'];
253   $page['#cache']['contexts'] = ['user.permissions'];
254
255   if (\Drupal::state()->get('common_test.hook_page_attachments_alter.descendant_attached', FALSE)) {
256     $page['content']['#attached']['library'][] = 'core/jquery';
257   }
258
259   if (\Drupal::state()->get('common_test.hook_page_attachments_alter.render_array', FALSE)) {
260     $page['something'] = [
261       '#markup' => 'test',
262     ];
263   }
264 }
265
266 /**
267  * Implements hook_js_settings_alter().
268  *
269  * @see \Drupal\system\Tests\Common\JavaScriptTest::testHeaderSetting()
270  */
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'] = '☃';
275   }
276
277   // Add a setting.
278   $settings['foo'] = 'bar';
279 }