8 use Drupal\Core\Extension\Extension;
11 * Implements hook_theme().
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',
19 $items['theme_test_template_test'] = [
20 'template' => 'theme_test.template_test',
22 $items['theme_test_template_test_2'] = [
23 'template' => 'theme_test.template_test',
25 $items['theme_test_suggestion_provided'] = [
28 $items['theme_test_specific_suggestions'] = [
31 $items['theme_test_suggestions'] = [
34 $items['theme_test_general_suggestions'] = [
37 $items['theme_test_function_suggestions'] = [
39 'function' => 'theme_theme_test_function_suggestions',
41 $items['theme_test_suggestions_include'] = [
43 'function' => 'theme_theme_test_suggestions_include',
45 $items['theme_test_foo'] = [
46 'variables' => ['foo' => NULL],
47 'function' => 'theme_theme_test_foo',
49 $items['theme_test_render_element'] = [
50 'render element' => 'elements',
52 $items['theme_test_render_element_children'] = [
53 'render element' => 'element',
54 'function' => 'theme_theme_test_render_element_children',
56 $items['theme_test_function_template_override'] = [
58 'function' => 'theme_theme_test_function_template_override',
60 $info['test_theme_not_existing_function'] = [
61 'function' => 'test_theme_not_existing_function',
63 $items['theme_test_preprocess_suggestions'] = [
69 $items['theme_test_registered_by_module'] = [
70 'render element' => 'content',
71 'base hook' => 'container',
77 * Implements hook_preprocess_HOOK() for HTML document templates.
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';
83 $variables['attributes']['theme_test_page_variable'] = 'Page variable is an array.';
87 * Implements hook_page_bottom().
89 function theme_test_page_bottom(array &$page_bottom) {
90 $page_bottom['theme_test_page_bottom'] = ['#markup' => 'theme test page bottom markup'];
94 * Implements template_preprocess_HOOK() for theme_test_function_suggestions theme functions.
96 function template_preprocess_theme_test_function_suggestions(&$variables) {
100 * Theme function for hook theme_test_foo.
102 function theme_theme_test_foo($variables) {
103 return $variables['foo'];
107 * Theme function for hook theme_test_function_template_override.
109 function theme_theme_test_function_template_override($variables) {
110 return 'theme_test_function_template_override test failed.';
114 * Implements hook_theme_suggestions_HOOK().
116 function theme_test_theme_suggestions_theme_test_preprocess_suggestions($variables) {
117 return ['theme_test_preprocess_suggestions__' . $variables['foo']];
121 * Implements hook_preprocess_HOOK().
123 function theme_test_preprocess_theme_test_preprocess_suggestions(&$variables) {
124 $variables['foo'] = 'Theme hook implementor=theme_theme_test_preprocess_suggestions().';
128 * Tests a module overriding a default hook with a suggestion.
130 function theme_test_preprocess_theme_test_preprocess_suggestions__monkey(&$variables) {
131 $variables['foo'] = 'Monkey';
135 * Prepares variables for test render element templates.
137 * Default template: theme-test-render-element.html.twig.
139 * @param array $variables
140 * An associative array containing:
141 * - elements: An associative array containing the properties of the element.
143 function template_preprocess_theme_test_render_element(&$variables) {
144 $variables['attributes']['data-variables-are-preprocessed'] = TRUE;
148 * Theme function for testing rendering of child elements via drupal_render().
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.
155 * @param array $variables
156 * An associative array containing:
157 * - element: An associative array containing the properties of the element.
159 function theme_theme_test_render_element_children($variables) {
160 return \Drupal::service('renderer')->render($variables['element']);
164 * Returns HTML for a theme function suggestion test.
166 function theme_theme_test_function_suggestions($variables) {
167 return 'Original theme function.';
171 * Implements hook_theme_suggestions_HOOK().
173 function theme_test_theme_suggestions_theme_test_suggestion_provided(array $variables) {
174 return ['theme_test_suggestion_provided__' . 'foo'];
178 * Implements hook_theme_suggestions_alter().
180 function theme_test_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
181 \Drupal::messenger()->addStatus(__FUNCTION__ . '() executed for ' . $hook . '.');
185 * Implements hook_theme_suggestions_HOOK_alter().
187 function theme_test_theme_suggestions_theme_test_suggestions_alter(array &$suggestions, array $variables) {
188 \Drupal::messenger()->addStatus(__FUNCTION__ . '() executed.');
192 * Returns HTML for a theme function include test.
194 function theme_theme_test_suggestions_include($variables) {
195 return 'Original function before altering theme suggestions.';
199 * Implements hook_system_info_alter().
201 * @see \Drupal\system\Tests\Theme\ThemeInfoTest::testChanges()
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';
212 * Implements hook_theme_suggestions_HOOK().
214 function theme_test_theme_suggestions_node(array $variables) {
215 $xss = '<script type="text/javascript">alert(\'yo\');</script>';
216 $suggestions[] = 'node__' . $xss;
222 * Implements template_preprocess_HOOK() for theme_test_registered_by_module.
224 function template_preprocess_theme_test_registered_by_module() {