summaryrefslogtreecommitdiffstats
path: root/core/modules/system/tests/modules/theme_test/theme_test.module
blob: f971b5453a485478f22dd9707177fe5fa8fadc7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php

/**
 * @file
 * Test module.
 */

use Drupal\Core\Extension\Extension;

/**
 * Implements hook_theme().
 */
function theme_test_theme($existing, $type, $theme, $path) {
  $items['theme_test'] = [
    'file' => 'theme_test.inc',
    'variables' => ['foo' => ''],
    'function' => 'theme_theme_test',
  ];
  $items['theme_test_template_test'] = [
    'template' => 'theme_test.template_test',
  ];
  $items['theme_test_template_test_2'] = [
    'template' => 'theme_test.template_test',
  ];
  $items['theme_test_suggestion_provided'] = [
    'variables' => [],
  ];
  $items['theme_test_specific_suggestions'] = [
    'variables' => [],
  ];
  $items['theme_test_suggestions'] = [
    'variables' => [],
  ];
  $items['theme_test_general_suggestions'] = [
    'variables' => [],
  ];
  $items['theme_test_function_suggestions'] = [
    'variables' => [],
    'function' => 'theme_theme_test_function_suggestions',
  ];
  $items['theme_test_suggestions_include'] = [
    'variables' => [],
    'function' => 'theme_theme_test_suggestions_include',
  ];
  $items['theme_test_foo'] = [
    'variables' => ['foo' => NULL],
    'function' => 'theme_theme_test_foo',
  ];
  $items['theme_test_render_element'] = [
    'render element' => 'elements',
  ];
  $items['theme_test_render_element_children'] = [
    'render element' => 'element',
    'function' => 'theme_theme_test_render_element_children',
  ];
  $items['theme_test_function_template_override'] = [
    'variables' => [],
    'function' => 'theme_theme_test_function_template_override',
  ];
  $info['test_theme_not_existing_function'] = [
    'function' => 'test_theme_not_existing_function',
  ];
  $items['theme_test_preprocess_suggestions'] = [
    'variables' => [
      'foo' => '',
      'bar' => '',
    ],
  ];
  $items['theme_test_registered_by_module'] = [
    'render element' => 'content',
    'base hook' => 'container',
  ];
  return $items;
}

/**
 * Implements hook_preprocess_HOOK() for HTML document templates.
 */
function theme_test_preprocess_html(&$variables) {
  $variables['html_attributes']['theme_test_html_attribute'] = 'theme test html attribute value';
  $variables['attributes']['theme_test_body_attribute'] = 'theme test body attribute value';

  $variables['attributes']['theme_test_page_variable'] = 'Page variable is an array.';
}

/**
 * Implements hook_page_bottom().
 */
function theme_test_page_bottom(array &$page_bottom) {
  $page_bottom['theme_test_page_bottom'] = ['#markup' => 'theme test page bottom markup'];
}

/**
 * Implements template_preprocess_HOOK() for theme_test_function_suggestions theme functions.
 */
function template_preprocess_theme_test_function_suggestions(&$variables) {
}

/**
 * Theme function for hook theme_test_foo.
 */
function theme_theme_test_foo($variables) {
  return $variables['foo'];
}

/**
 * Theme function for hook theme_test_function_template_override.
 */
function theme_theme_test_function_template_override($variables) {
  return 'theme_test_function_template_override test failed.';
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function theme_test_theme_suggestions_theme_test_preprocess_suggestions($variables) {
  return ['theme_test_preprocess_suggestions__' . $variables['foo']];
}

/**
 * Implements hook_preprocess_HOOK().
 */
function theme_test_preprocess_theme_test_preprocess_suggestions(&$variables) {
  $variables['foo'] = 'Theme hook implementor=theme_theme_test_preprocess_suggestions().';
}

/**
 * Tests a module overriding a default hook with a suggestion.
 */
function theme_test_preprocess_theme_test_preprocess_suggestions__monkey(&$variables) {
  $variables['foo'] = 'Monkey';
}

/**
 * Prepares variables for test render element templates.
 *
 * Default template: theme-test-render-element.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An associative array containing the properties of the element.
 */
function template_preprocess_theme_test_render_element(&$variables) {
  $variables['attributes']['data-variables-are-preprocessed'] = TRUE;
}

/**
 * Theme function for testing rendering of child elements via drupal_render().
 *
 * Theme hooks defining a 'render element' add an internal '#render_children'
 * property. When this property is found, drupal_render() avoids calling
 * the 'theme.manager' service 'render' method on the top-level element to
 * prevent infinite recursion.
 *
 * @param array $variables
 *   An associative array containing:
 *   - element: An associative array containing the properties of the element.
 */
function theme_theme_test_render_element_children($variables) {
  return \Drupal::service('renderer')->render($variables['element']);
}

/**
 * Returns HTML for a theme function suggestion test.
 */
function theme_theme_test_function_suggestions($variables) {
  return 'Original theme function.';
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function theme_test_theme_suggestions_theme_test_suggestion_provided(array $variables) {
  return ['theme_test_suggestion_provided__' . 'foo'];
}

/**
 * Implements hook_theme_suggestions_alter().
 */
function theme_test_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  \Drupal::messenger()->addStatus(__FUNCTION__ . '() executed.');
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function theme_test_theme_suggestions_theme_test_suggestions_alter(array &$suggestions, array $variables) {
  \Drupal::messenger()->addStatus(__FUNCTION__ . '() executed.');
}

/**
 * Returns HTML for a theme function include test.
 */
function theme_theme_test_suggestions_include($variables) {
  return 'Original function before altering theme suggestions.';
}

/**
 * Implements hook_system_info_alter().
 *
 * @see \Drupal\system\Tests\Theme\ThemeInfoTest::testChanges()
 */
function theme_test_system_info_alter(array &$info, Extension $file, $type) {
  if ($type == 'theme' && $file->getName() == 'test_theme' && \Drupal::state()->get('theme_test.modify_info_files')) {
    // Add a library to see if the system picks it up.
    $info += ['libraries' => []];
    $info['libraries'][] = 'core/backbone';
  }
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function theme_test_theme_suggestions_node(array $variables) {
  $xss = '<script type="text/javascript">alert(\'yo\');</script>';
  $suggestions[] = 'node__' . $xss;

  return $suggestions;
}

/**
 * Implements template_preprocess_HOOK() for theme_test_registered_by_module.
 */
function template_preprocess_theme_test_registered_by_module() {
}