901449696caf17b3e3f7571c05f483ea8a653b49
[yaffs-website] / 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   $items['theme_test_registered_by_module'] = [
70     'render element' => 'content',
71     'base hook' => 'container',
72   ];
73   return $items;
74 }
75
76 /**
77  * Implements hook_preprocess_HOOK() for HTML document templates.
78  */
79 function theme_test_preprocess_html(&$variables) {
80   $variables['html_attributes']['theme_test_html_attribute'] = 'theme test html attribute value';
81   $variables['attributes']['theme_test_body_attribute'] = 'theme test body attribute value';
82
83   $variables['attributes']['theme_test_page_variable'] = 'Page variable is an array.';
84 }
85
86 /**
87  * Implements hook_page_bottom().
88  */
89 function theme_test_page_bottom(array &$page_bottom) {
90   $page_bottom['theme_test_page_bottom'] = ['#markup' => 'theme test page bottom markup'];
91 }
92
93 /**
94  * Implements template_preprocess_HOOK() for theme_test_function_suggestions theme functions.
95  */
96 function template_preprocess_theme_test_function_suggestions(&$variables) {
97 }
98
99 /**
100  * Theme function for hook theme_test_foo.
101  */
102 function theme_theme_test_foo($variables) {
103   return $variables['foo'];
104 }
105
106 /**
107  * Theme function for hook theme_test_function_template_override.
108  */
109 function theme_theme_test_function_template_override($variables) {
110   return 'theme_test_function_template_override test failed.';
111 }
112
113 /**
114  * Implements hook_theme_suggestions_HOOK().
115  */
116 function theme_test_theme_suggestions_theme_test_preprocess_suggestions($variables) {
117   return ['theme_test_preprocess_suggestions__' . $variables['foo']];
118 }
119
120 /**
121  * Implements hook_preprocess_HOOK().
122  */
123 function theme_test_preprocess_theme_test_preprocess_suggestions(&$variables) {
124   $variables['foo'] = 'Theme hook implementor=theme_theme_test_preprocess_suggestions().';
125 }
126
127 /**
128  * Tests a module overriding a default hook with a suggestion.
129  */
130 function theme_test_preprocess_theme_test_preprocess_suggestions__monkey(&$variables) {
131   $variables['foo'] = 'Monkey';
132 }
133
134 /**
135  * Prepares variables for test render element templates.
136  *
137  * Default template: theme-test-render-element.html.twig.
138  *
139  * @param array $variables
140  *   An associative array containing:
141  *   - elements: An associative array containing the properties of the element.
142  */
143 function template_preprocess_theme_test_render_element(&$variables) {
144   $variables['attributes']['data-variables-are-preprocessed'] = TRUE;
145 }
146
147 /**
148  * Theme function for testing rendering of child elements via drupal_render().
149  *
150  * Theme hooks defining a 'render element' add an internal '#render_children'
151  * property. When this property is found, drupal_render() avoids calling
152  * the 'theme.manager' service 'render' method on the top-level element to
153  * prevent infinite recursion.
154  *
155  * @param array $variables
156  *   An associative array containing:
157  *   - element: An associative array containing the properties of the element.
158  */
159 function theme_theme_test_render_element_children($variables) {
160   return \Drupal::service('renderer')->render($variables['element']);
161 }
162
163 /**
164  * Returns HTML for a theme function suggestion test.
165  */
166 function theme_theme_test_function_suggestions($variables) {
167   return 'Original theme function.';
168 }
169
170 /**
171  * Implements hook_theme_suggestions_HOOK().
172  */
173 function theme_test_theme_suggestions_theme_test_suggestion_provided(array $variables) {
174   return ['theme_test_suggestion_provided__' . 'foo'];
175 }
176
177 /**
178  * Implements hook_theme_suggestions_alter().
179  */
180 function theme_test_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
181   \Drupal::messenger()->addStatus(__FUNCTION__ . '() executed for ' . $hook . '.');
182 }
183
184 /**
185  * Implements hook_theme_suggestions_HOOK_alter().
186  */
187 function theme_test_theme_suggestions_theme_test_suggestions_alter(array &$suggestions, array $variables) {
188   \Drupal::messenger()->addStatus(__FUNCTION__ . '() executed.');
189 }
190
191 /**
192  * Returns HTML for a theme function include test.
193  */
194 function theme_theme_test_suggestions_include($variables) {
195   return 'Original function before altering theme suggestions.';
196 }
197
198 /**
199  * Implements hook_system_info_alter().
200  *
201  * @see \Drupal\system\Tests\Theme\ThemeInfoTest::testChanges()
202  */
203 function theme_test_system_info_alter(array &$info, Extension $file, $type) {
204   if ($type == 'theme' && $file->getName() == 'test_theme' && \Drupal::state()->get('theme_test.modify_info_files')) {
205     // Add a library to see if the system picks it up.
206     $info += ['libraries' => []];
207     $info['libraries'][] = 'core/backbone';
208   }
209 }
210
211 /**
212  * Implements hook_theme_suggestions_HOOK().
213  */
214 function theme_test_theme_suggestions_node(array $variables) {
215   $xss = '<script type="text/javascript">alert(\'yo\');</script>';
216   $suggestions[] = 'node__' . $xss;
217
218   return $suggestions;
219 }
220
221 /**
222  * Implements template_preprocess_HOOK() for theme_test_registered_by_module.
223  */
224 function template_preprocess_theme_test_registered_by_module() {
225 }