test_theme.theme 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * Implements hook_element_info_alter().
  14. */
  15. function test_theme_element_info_alter(&$info) {
  16. // Decrease the default size of textfields.
  17. if (isset($info['textfield']['#size'])) {
  18. $info['textfield']['#size'] = 40;
  19. }
  20. }
  21. /**
  22. * Implements hook_library_info_alter().
  23. */
  24. function test_theme_library_info_alter(&$libraries, $extension) {
  25. if ($extension === 'test_theme') {
  26. $libraries['kitten']['js']['kittens.js'] = [];
  27. }
  28. }
  29. /**
  30. * Tests a theme implementing an alter hook.
  31. *
  32. * The confusing function name here is due to this being an implementation of
  33. * the alter hook invoked when the 'theme_test' module calls
  34. * \Drupal::moduleHandler->alter('theme_test_alter').
  35. */
  36. function test_theme_theme_test_alter_alter(&$data) {
  37. $data = 'test_theme_theme_test_alter_alter was invoked';
  38. }
  39. /**
  40. * Implements hook_theme_suggestions_alter().
  41. */
  42. function test_theme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  43. \Drupal::messenger()->addStatus(__FUNCTION__ . '() executed.');
  44. // Theme alter hooks run after module alter hooks, so add this theme
  45. // suggestion to the beginning of the array so that the suggestion added by
  46. // the theme_suggestions_test module can be picked up when that module is
  47. // enabled.
  48. if ($hook == 'theme_test_general_suggestions') {
  49. array_unshift($suggestions, 'theme_test_general_suggestions__' . 'theme_override');
  50. }
  51. }
  52. /**
  53. * Implements hook_theme_suggestions_HOOK_alter().
  54. */
  55. function test_theme_theme_suggestions_theme_test_suggestions_alter(array &$suggestions, array $variables) {
  56. \Drupal::messenger()->addStatus(__FUNCTION__ . '() executed.');
  57. // Theme alter hooks run after module alter hooks, so add this theme
  58. // suggestion to the beginning of the array so that the suggestion added by
  59. // the theme_suggestions_test module can be picked up when that module is
  60. // enabled.
  61. array_unshift($suggestions, 'theme_test_suggestions__' . 'theme_override');
  62. }
  63. /**
  64. * Implements hook_theme_registry_alter().
  65. */
  66. function test_theme_theme_registry_alter(&$registry) {
  67. $registry['theme_test_template_test']['variables']['additional'] = 'value';
  68. }
  69. /**
  70. * Tests a theme overriding a default hook with a suggestion.
  71. *
  72. * Implements hook_preprocess_HOOK().
  73. */
  74. function test_theme_preprocess_theme_test_preprocess_suggestions(&$variables) {
  75. $variables['foo'] = 'Theme hook implementor=test_theme_preprocess_theme_test_preprocess_suggestions().';
  76. }
  77. /**
  78. * Tests a theme overriding a default hook with a suggestion.
  79. */
  80. function test_theme_preprocess_theme_test_preprocess_suggestions__suggestion(&$variables) {
  81. $variables['foo'] = 'Suggestion';
  82. }
  83. /**
  84. * Tests a theme overriding a default hook with a suggestion.
  85. */
  86. function test_theme_preprocess_theme_test_preprocess_suggestions__kitten(&$variables) {
  87. $variables['foo'] = 'Kitten';
  88. }
  89. /**
  90. * Tests a theme overriding a default hook with a suggestion.
  91. */
  92. function test_theme_preprocess_theme_test_preprocess_suggestions__kitten__flamingo(&$variables) {
  93. $variables['bar'] = 'Flamingo';
  94. }
  95. /**
  96. * Tests a preprocess function with suggestions.
  97. */
  98. function test_theme_preprocess_theme_test_preprocess_suggestions__kitten__meerkat__tarsier__moose(&$variables) {
  99. $variables['bar'] = 'Moose';
  100. }
  101. /**
  102. * Tests that a class can be loaded within a .theme file.
  103. */
  104. function test_theme_preprocess_theme_test_theme_class(&$variables) {
  105. if (class_exists('\Drupal\test_theme\ThemeClass')) {
  106. $variables['message'] = 'Loading ThemeClass was successful.';
  107. }
  108. else {
  109. $variables['message'] = 'Loading ThemeClass failed.';
  110. }
  111. }