6a701abee915069b845cdfed24610ed43db049c6
[yaffs-website] / web / core / modules / system / tests / modules / theme_test / theme_test.module
1 <?php
2
3 /**
4  * @file
5  * Test module.
6  */
7
8 use Drupal\Core\Extension\Extension;
9
10 /**
11  * Implements hook_theme().
12  */
13 function theme_test_theme($existing, $type, $theme, $path) {
14   $items['theme_test'] = [
15     'file' => 'theme_test.inc',
16     'variables' => ['foo' => ''],
17     'function' => 'theme_theme_test',
18   ];
19   $items['theme_test_template_test'] = [
20     'template' => 'theme_test.template_test',
21   ];
22   $items['theme_test_template_test_2'] = [
23     'template' => 'theme_test.template_test',
24   ];
25   $items['theme_test_suggestion_provided'] = [
26     'variables' => [],
27   ];
28   $items['theme_test_specific_suggestions'] = [
29     'variables' => [],
30   ];
31   $items['theme_test_suggestions'] = [
32     'variables' => [],
33   ];
34   $items['theme_test_general_suggestions'] = [
35     'variables' => [],
36   ];
37   $items['theme_test_function_suggestions'] = [
38     'variables' => [],
39     'function' => 'theme_theme_test_function_suggestions',
40   ];
41   $items['theme_test_suggestions_include'] = [
42     'variables' => [],
43     'function' => 'theme_theme_test_suggestions_include',
44   ];
45   $items['theme_test_foo'] = [
46     'variables' => ['foo' => NULL],
47     'function' => 'theme_theme_test_foo',
48   ];
49   $items['theme_test_render_element'] = [
50     'render element' => 'elements',
51   ];
52   $items['theme_test_render_element_children'] = [
53     'render element' => 'element',
54     'function' => 'theme_theme_test_render_element_children',
55   ];
56   $items['theme_test_function_template_override'] = [
57     'variables' => [],
58     'function' => 'theme_theme_test_function_template_override',
59   ];
60   $info['test_theme_not_existing_function'] = [
61     'function' => 'test_theme_not_existing_function',
62   ];
63   $items['theme_test_preprocess_suggestions'] = [
64     'variables' => [
65       'foo' => '',
66       'bar' => '',
67     ],
68   ];
69   return $items;
70 }
71
72 /**
73  * Implements hook_preprocess_HOOK() for HTML document templates.
74  */
75 function theme_test_preprocess_html(&$variables) {
76   $variables['html_attributes']['theme_test_html_attribute'] = 'theme test html attribute value';
77   $variables['attributes']['theme_test_body_attribute'] = 'theme test body attribute value';
78
79   $variables['attributes']['theme_test_page_variable'] = 'Page variable is an array.';
80 }
81
82 /**
83  * Implements hook_page_bottom().
84  */
85 function theme_test_page_bottom(array &$page_bottom) {
86   $page_bottom['theme_test_page_bottom'] = ['#markup' => 'theme test page bottom markup'];
87 }
88
89 /**
90  * Implements template_preprocess_HOOK() for theme_test_function_suggestions theme functions.
91  */
92 function template_preprocess_theme_test_function_suggestions(&$variables) {
93 }
94
95 /**
96  * Theme function for hook theme_test_foo.
97  */
98 function theme_theme_test_foo($variables) {
99   return $variables['foo'];
100 }
101
102 /**
103  * Theme function for hook theme_test_function_template_override.
104  */
105 function theme_theme_test_function_template_override($variables) {
106   return 'theme_test_function_template_override test failed.';
107 }
108
109 /**
110  * Implements hook_theme_suggestions_HOOK().
111  */
112 function theme_test_theme_suggestions_theme_test_preprocess_suggestions($variables) {
113   return ['theme_test_preprocess_suggestions__' . $variables['foo']];
114 }
115
116 /**
117  * Implements hook_preprocess_HOOK().
118  */
119 function theme_test_preprocess_theme_test_preprocess_suggestions(&$variables) {
120   $variables['foo'] = 'Theme hook implementor=theme_theme_test_preprocess_suggestions().';
121 }
122
123 /**
124  * Tests a module overriding a default hook with a suggestion.
125  */
126 function theme_test_preprocess_theme_test_preprocess_suggestions__monkey(&$variables) {
127   $variables['foo'] = 'Monkey';
128 }
129
130 /**
131  * Prepares variables for test render element templates.
132  *
133  * Default template: theme-test-render-element.html.twig.
134  *
135  * @param array $variables
136  *   An associative array containing:
137  *   - elements: An associative array containing the properties of the element.
138  */
139 function template_preprocess_theme_test_render_element(&$variables) {
140   $variables['attributes']['data-variables-are-preprocessed'] = TRUE;
141 }
142
143 /**
144  * Theme function for testing rendering of child elements via drupal_render().
145  *
146  * Theme hooks defining a 'render element' add an internal '#render_children'
147  * property. When this property is found, drupal_render() avoids calling
148  * the 'theme.manager' service 'render' method on the top-level element to
149  * prevent infinite recursion.
150  *
151  * @param array $variables
152  *   An associative array containing:
153  *   - element: An associative array containing the properties of the element.
154  */
155 function theme_theme_test_render_element_children($variables) {
156   return \Drupal::service('renderer')->render($variables['element']);
157 }
158
159 /**
160  * Returns HTML for a theme function suggestion test.
161  */
162 function theme_theme_test_function_suggestions($variables) {
163   return 'Original theme function.';
164 }
165
166 /**
167  * Implements hook_theme_suggestions_HOOK().
168  */
169 function theme_test_theme_suggestions_theme_test_suggestion_provided(array $variables) {
170   return ['theme_test_suggestion_provided__' . 'foo'];
171 }
172
173 /**
174  * Implements hook_theme_suggestions_alter().
175  */
176 function theme_test_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
177   drupal_set_message(__FUNCTION__ . '() executed.');
178 }
179
180 /**
181  * Implements hook_theme_suggestions_HOOK_alter().
182  */
183 function theme_test_theme_suggestions_theme_test_suggestions_alter(array &$suggestions, array $variables) {
184   drupal_set_message(__FUNCTION__ . '() executed.');
185 }
186
187 /**
188  * Returns HTML for a theme function include test.
189  */
190 function theme_theme_test_suggestions_include($variables) {
191   return 'Original function before altering theme suggestions.';
192 }
193
194 /**
195  * Implements hook_system_info_alter().
196  *
197  * @see \Drupal\system\Tests\Theme\ThemeInfoTest::testChanges()
198  */
199 function theme_test_system_info_alter(array &$info, Extension $file, $type) {
200   if ($type == 'theme' && $file->getName() == 'test_theme' && \Drupal::state()->get('theme_test.modify_info_files')) {
201     // Add a library to see if the system picks it up.
202     $info += ['libraries' => []];
203     $info['libraries'][] = 'core/backbone';
204   }
205 }
206
207 /**
208  * Implements hook_theme_suggestions_HOOK().
209  */
210 function theme_test_theme_suggestions_node(array $variables) {
211   $xss = '<script type="text/javascript">alert(\'yo\');</script>';
212   $suggestions[] = 'node__' . $xss;
213
214   return $suggestions;
215 }