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'] = [
73 * Implements hook_preprocess_HOOK() for HTML document templates.
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';
79 $variables['attributes']['theme_test_page_variable'] = 'Page variable is an array.';
83 * Implements hook_page_bottom().
85 function theme_test_page_bottom(array &$page_bottom) {
86 $page_bottom['theme_test_page_bottom'] = ['#markup' => 'theme test page bottom markup'];
90 * Implements template_preprocess_HOOK() for theme_test_function_suggestions theme functions.
92 function template_preprocess_theme_test_function_suggestions(&$variables) {
96 * Theme function for hook theme_test_foo.
98 function theme_theme_test_foo($variables) {
99 return $variables['foo'];
103 * Theme function for hook theme_test_function_template_override.
105 function theme_theme_test_function_template_override($variables) {
106 return 'theme_test_function_template_override test failed.';
110 * Implements hook_theme_suggestions_HOOK().
112 function theme_test_theme_suggestions_theme_test_preprocess_suggestions($variables) {
113 return ['theme_test_preprocess_suggestions__' . $variables['foo']];
117 * Implements hook_preprocess_HOOK().
119 function theme_test_preprocess_theme_test_preprocess_suggestions(&$variables) {
120 $variables['foo'] = 'Theme hook implementor=theme_theme_test_preprocess_suggestions().';
124 * Tests a module overriding a default hook with a suggestion.
126 function theme_test_preprocess_theme_test_preprocess_suggestions__monkey(&$variables) {
127 $variables['foo'] = 'Monkey';
131 * Prepares variables for test render element templates.
133 * Default template: theme-test-render-element.html.twig.
135 * @param array $variables
136 * An associative array containing:
137 * - elements: An associative array containing the properties of the element.
139 function template_preprocess_theme_test_render_element(&$variables) {
140 $variables['attributes']['data-variables-are-preprocessed'] = TRUE;
144 * Theme function for testing rendering of child elements via drupal_render().
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.
151 * @param array $variables
152 * An associative array containing:
153 * - element: An associative array containing the properties of the element.
155 function theme_theme_test_render_element_children($variables) {
156 return drupal_render($variables['element']);
160 * Returns HTML for a theme function suggestion test.
162 function theme_theme_test_function_suggestions($variables) {
163 return 'Original theme function.';
167 * Implements hook_theme_suggestions_HOOK().
169 function theme_test_theme_suggestions_theme_test_suggestion_provided(array $variables) {
170 return ['theme_test_suggestion_provided__' . 'foo'];
174 * Implements hook_theme_suggestions_alter().
176 function theme_test_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
177 drupal_set_message(__FUNCTION__ . '() executed.');
181 * Implements hook_theme_suggestions_HOOK_alter().
183 function theme_test_theme_suggestions_theme_test_suggestions_alter(array &$suggestions, array $variables) {
184 drupal_set_message(__FUNCTION__ . '() executed.');
188 * Returns HTML for a theme function include test.
190 function theme_theme_test_suggestions_include($variables) {
191 return 'Original function before altering theme suggestions.';
195 * Implements hook_system_info_alter().
197 * @see \Drupal\system\Tests\Theme\ThemeInfoTest::testChanges()
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';
208 * Implements hook_theme_suggestions_HOOK().
210 function theme_test_theme_suggestions_node(array $variables) {
211 $xss = '<script type="text/javascript">alert(\'yo\');</script>';
212 $suggestions[] = 'node__' . $xss;