theme_test.module 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Implements hook_theme().
  4. */
  5. function theme_test_theme($existing, $type, $theme, $path) {
  6. $items['theme_test'] = array(
  7. 'file' => 'theme_test.inc',
  8. 'variables' => array('foo' => ''),
  9. );
  10. $items['theme_test_template_test'] = array(
  11. 'template' => 'theme_test.template_test',
  12. );
  13. $items['theme_test_template_test_2'] = array(
  14. 'template' => 'theme_test.template_test',
  15. );
  16. $items['theme_test_foo'] = array(
  17. 'variables' => array('foo' => NULL),
  18. );
  19. return $items;
  20. }
  21. /**
  22. * Implements hook_system_theme_info().
  23. */
  24. function theme_test_system_theme_info() {
  25. $themes['test_theme'] = drupal_get_path('module', 'theme_test') . '/themes/test_theme/test_theme.info';
  26. $themes['test_basetheme'] = drupal_get_path('module', 'theme_test') . '/themes/test_basetheme/test_basetheme.info';
  27. $themes['test_subtheme'] = drupal_get_path('module', 'theme_test') . '/themes/test_subtheme/test_subtheme.info';
  28. $themes['test_theme_nyan_cat'] = drupal_get_path('module', 'theme_test') . '/themes/test_theme_nyan_cat/test_theme_nyan_cat.info';
  29. return $themes;
  30. }
  31. /**
  32. * Implements hook_system_theme_engine_info().
  33. */
  34. function theme_test_system_theme_engine_info() {
  35. $theme_engines['nyan_cat'] = drupal_get_path('module', 'theme_test') . '/themes/engines/nyan_cat/nyan_cat.engine';
  36. return $theme_engines;
  37. }
  38. /**
  39. * Implements hook_menu().
  40. */
  41. function theme_test_menu() {
  42. $items['theme-test/suggestion'] = array(
  43. 'title' => 'Suggestion',
  44. 'page callback' => '_theme_test_suggestion',
  45. 'access arguments' => array('access content'),
  46. 'theme callback' => '_theme_custom_theme',
  47. 'type' => MENU_CALLBACK,
  48. );
  49. $items['theme-test/alter'] = array(
  50. 'title' => 'Suggestion',
  51. 'page callback' => '_theme_test_alter',
  52. 'access arguments' => array('access content'),
  53. 'theme callback' => '_theme_custom_theme',
  54. 'type' => MENU_CALLBACK,
  55. );
  56. $items['theme-test/hook-init'] = array(
  57. 'page callback' => 'theme_test_hook_init_page_callback',
  58. 'access callback' => TRUE,
  59. 'type' => MENU_CALLBACK,
  60. );
  61. $items['theme-test/drupal-add-region-content'] = array(
  62. 'page callback' => '_theme_test_drupal_add_region_content',
  63. 'access callback' => TRUE,
  64. 'type' => MENU_CALLBACK,
  65. );
  66. $items['theme-test/engine-info-test'] = array(
  67. 'description' => "Serves a simple page rendered using a Nyan Cat theme engine template.",
  68. 'page callback' => '_theme_test_engine_info_test',
  69. 'access callback' => TRUE,
  70. 'type' => MENU_CALLBACK,
  71. );
  72. return $items;
  73. }
  74. /**
  75. * Implements hook_init().
  76. */
  77. function theme_test_init() {
  78. if (arg(0) == 'theme-test' && arg(1) == 'hook-init') {
  79. // First, force the theme registry to be rebuilt on this page request. This
  80. // allows us to test a full initialization of the theme system in the code
  81. // below.
  82. drupal_theme_rebuild();
  83. // Next, initialize the theme system by storing themed text in a global
  84. // variable. We will use this later in theme_test_hook_init_page_callback()
  85. // to test that even when the theme system is initialized this early, it is
  86. // still capable of returning output and theming the page as a whole.
  87. $GLOBALS['theme_test_output'] = theme('more_link', array('url' => 'user', 'title' => 'Themed output generated in hook_init()'));
  88. }
  89. }
  90. /**
  91. * Implements hook_exit().
  92. */
  93. function theme_test_exit() {
  94. if (arg(0) == 'user') {
  95. // Register a fake registry loading callback. If it gets called by
  96. // theme_get_registry(), the registry has not been initialized yet.
  97. _theme_registry_callback('_theme_test_load_registry', array());
  98. print theme_get_registry() ? 'registry initialized' : 'registry not initialized';
  99. }
  100. }
  101. /**
  102. * Fake registry loading callback.
  103. */
  104. function _theme_test_load_registry() {
  105. return array();
  106. }
  107. /**
  108. * Menu callback for testing themed output generated in hook_init().
  109. */
  110. function theme_test_hook_init_page_callback() {
  111. return $GLOBALS['theme_test_output'];
  112. }
  113. /**
  114. * Custom theme callback.
  115. */
  116. function _theme_custom_theme() {
  117. return 'test_theme';
  118. }
  119. /**
  120. * Page callback, calls drupal_alter().
  121. *
  122. * This is for testing that the theme can have hook_*_alter() implementations
  123. * that run during page callback execution, even before theme() is called for
  124. * the first time.
  125. */
  126. function _theme_test_alter() {
  127. $data = 'foo';
  128. drupal_alter('theme_test_alter', $data);
  129. return "The altered data is $data.";
  130. }
  131. /**
  132. * Page callback, calls a theme hook suggestion.
  133. */
  134. function _theme_test_suggestion() {
  135. return theme(array('theme_test__suggestion', 'theme_test'), array());
  136. }
  137. /**
  138. * Page callback, calls drupal_add_region_content.
  139. */
  140. function _theme_test_drupal_add_region_content() {
  141. drupal_add_region_content('content', 'World');
  142. return 'Hello';
  143. }
  144. /**
  145. * Serves a simple page renderered using a Nyan Cat theme engine template.
  146. */
  147. function _theme_test_engine_info_test() {
  148. return array(
  149. '#markup' => theme('theme_test_template_test'),
  150. );
  151. }
  152. /**
  153. * Theme function for testing theme('theme_test_foo').
  154. */
  155. function theme_theme_test_foo($variables) {
  156. return $variables['foo'];
  157. }