test_theme.theme 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @file
  4. * Theme to help test the Twig engine.
  5. */
  6. /**
  7. * Implements THEME_preprocess_twig_theme_test_php_variables().
  8. */
  9. function test_theme_preprocess_twig_theme_test_php_variables(&$variables) {
  10. $variables['php_values'] = _test_theme_twig_php_values();
  11. }
  12. /**
  13. * Tests a theme overriding a suggestion of a base theme hook.
  14. */
  15. function test_theme_theme_test__suggestion($variables) {
  16. return 'Theme hook implementor=test_theme_theme_test__suggestion(). Foo=' . $variables['foo'];
  17. }
  18. /**
  19. * Implements hook_element_info_alter().
  20. */
  21. function test_theme_element_info_alter(&$info) {
  22. // Decrease the default size of textfields.
  23. if (isset($info['textfield']['#size'])) {
  24. $info['textfield']['#size'] = 40;
  25. }
  26. }
  27. /**
  28. * Implements hook_library_info_alter().
  29. */
  30. function test_theme_library_info_alter(&$libraries, $extension) {
  31. if ($extension === 'test_theme') {
  32. $libraries['kitten']['js']['kittens.js'] = [];
  33. }
  34. }
  35. /**
  36. * Tests a theme implementing an alter hook.
  37. *
  38. * The confusing function name here is due to this being an implementation of
  39. * the alter hook invoked when the 'theme_test' module calls
  40. * \Drupal::moduleHandler->alter('theme_test_alter').
  41. */
  42. function test_theme_theme_test_alter_alter(&$data) {
  43. $data = 'test_theme_theme_test_alter_alter was invoked';
  44. }
  45. /**
  46. * Implements hook_theme_suggestions_alter().
  47. */
  48. function test_theme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  49. \Drupal::messenger()->addStatus(__FUNCTION__ . '() executed.');
  50. // Theme alter hooks run after module alter hooks, so add this theme
  51. // suggestion to the beginning of the array so that the suggestion added by
  52. // the theme_suggestions_test module can be picked up when that module is
  53. // enabled.
  54. if ($hook == 'theme_test_general_suggestions') {
  55. array_unshift($suggestions, 'theme_test_general_suggestions__' . 'theme_override');
  56. }
  57. }
  58. /**
  59. * Implements hook_theme_suggestions_HOOK_alter().
  60. */
  61. function test_theme_theme_suggestions_theme_test_suggestions_alter(array &$suggestions, array $variables) {
  62. \Drupal::messenger()->addStatus(__FUNCTION__ . '() executed.');
  63. // Theme alter hooks run after module alter hooks, so add this theme
  64. // suggestion to the beginning of the array so that the suggestion added by
  65. // the theme_suggestions_test module can be picked up when that module is
  66. // enabled.
  67. array_unshift($suggestions, 'theme_test_suggestions__' . 'theme_override');
  68. }
  69. /**
  70. * Implements hook_theme_suggestions_HOOK_alter().
  71. */
  72. function test_theme_theme_suggestions_theme_test_function_suggestions_alter(array &$suggestions, array $variables) {
  73. // Theme alter hooks run after module alter hooks, so add this theme
  74. // suggestion to the beginning of the array so that the suggestion added by
  75. // the theme_suggestions_test module can be picked up when that module is
  76. // enabled.
  77. array_unshift($suggestions, 'theme_test_function_suggestions__' . 'theme_override');
  78. }
  79. /**
  80. * Returns HTML for a theme function suggestion test.
  81. *
  82. * Implements the theme_test_function_suggestions__theme_override suggestion.
  83. */
  84. function test_theme_theme_test_function_suggestions__theme_override($variables) {
  85. return 'Theme function overridden based on new theme suggestion provided by the test_theme theme.';
  86. }
  87. /**
  88. * Returns HTML for a theme function suggestion test.
  89. *
  90. * Implements the theme_test_function_suggestions__module_override suggestion.
  91. */
  92. function test_theme_theme_test_function_suggestions__module_override($variables) {
  93. return 'Theme function overridden based on new theme suggestion provided by a module.';
  94. }
  95. /**
  96. * Implements hook_theme_registry_alter().
  97. */
  98. function test_theme_theme_registry_alter(&$registry) {
  99. $registry['theme_test_template_test']['variables']['additional'] = 'value';
  100. }
  101. /**
  102. * Tests a theme overriding a suggestion of a base theme hook.
  103. */
  104. function test_theme_theme_test_preprocess_suggestions__kitten__meerkat($variables) {
  105. return 'Theme hook implementor=test_theme_theme_test__suggestion(). Foo=' . $variables['foo'];
  106. }
  107. /**
  108. * Tests a theme overriding a default hook with a suggestion.
  109. *
  110. * Implements hook_preprocess_HOOK().
  111. */
  112. function test_theme_preprocess_theme_test_preprocess_suggestions(&$variables) {
  113. $variables['foo'] = 'Theme hook implementor=test_theme_preprocess_theme_test_preprocess_suggestions().';
  114. }
  115. /**
  116. * Tests a theme overriding a default hook with a suggestion.
  117. */
  118. function test_theme_preprocess_theme_test_preprocess_suggestions__suggestion(&$variables) {
  119. $variables['foo'] = 'Suggestion';
  120. }
  121. /**
  122. * Tests a theme overriding a default hook with a suggestion.
  123. */
  124. function test_theme_preprocess_theme_test_preprocess_suggestions__kitten(&$variables) {
  125. $variables['foo'] = 'Kitten';
  126. }
  127. /**
  128. * Tests a theme overriding a default hook with a suggestion.
  129. */
  130. function test_theme_preprocess_theme_test_preprocess_suggestions__kitten__flamingo(&$variables) {
  131. $variables['bar'] = 'Flamingo';
  132. }
  133. /**
  134. * Tests a preprocess function with suggestions.
  135. */
  136. function test_theme_preprocess_theme_test_preprocess_suggestions__kitten__meerkat__tarsier__moose(&$variables) {
  137. $variables['bar'] = 'Moose';
  138. }