theme_test.module 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. return $themes;
  29. }
  30. /**
  31. * Implements hook_menu().
  32. */
  33. function theme_test_menu() {
  34. $items['theme-test/suggestion'] = array(
  35. 'title' => 'Suggestion',
  36. 'page callback' => '_theme_test_suggestion',
  37. 'access arguments' => array('access content'),
  38. 'theme callback' => '_theme_custom_theme',
  39. 'type' => MENU_CALLBACK,
  40. );
  41. $items['theme-test/alter'] = array(
  42. 'title' => 'Suggestion',
  43. 'page callback' => '_theme_test_alter',
  44. 'access arguments' => array('access content'),
  45. 'theme callback' => '_theme_custom_theme',
  46. 'type' => MENU_CALLBACK,
  47. );
  48. $items['theme-test/hook-init'] = array(
  49. 'page callback' => 'theme_test_hook_init_page_callback',
  50. 'access callback' => TRUE,
  51. 'type' => MENU_CALLBACK,
  52. );
  53. $items['theme-test/drupal-add-region-content'] = array(
  54. 'page callback' => '_theme_test_drupal_add_region_content',
  55. 'access callback' => TRUE,
  56. 'type' => MENU_CALLBACK,
  57. );
  58. return $items;
  59. }
  60. /**
  61. * Implements hook_init().
  62. */
  63. function theme_test_init() {
  64. if (arg(0) == 'theme-test' && arg(1) == 'hook-init') {
  65. // First, force the theme registry to be rebuilt on this page request. This
  66. // allows us to test a full initialization of the theme system in the code
  67. // below.
  68. drupal_theme_rebuild();
  69. // Next, initialize the theme system by storing themed text in a global
  70. // variable. We will use this later in theme_test_hook_init_page_callback()
  71. // to test that even when the theme system is initialized this early, it is
  72. // still capable of returning output and theming the page as a whole.
  73. $GLOBALS['theme_test_output'] = theme('more_link', array('url' => 'user', 'title' => 'Themed output generated in hook_init()'));
  74. }
  75. }
  76. /**
  77. * Implements hook_exit().
  78. */
  79. function theme_test_exit() {
  80. if (arg(0) == 'user') {
  81. // Register a fake registry loading callback. If it gets called by
  82. // theme_get_registry(), the registry has not been initialized yet.
  83. _theme_registry_callback('_theme_test_load_registry', array());
  84. print theme_get_registry() ? 'registry initialized' : 'registry not initialized';
  85. }
  86. }
  87. /**
  88. * Fake registry loading callback.
  89. */
  90. function _theme_test_load_registry() {
  91. return array();
  92. }
  93. /**
  94. * Menu callback for testing themed output generated in hook_init().
  95. */
  96. function theme_test_hook_init_page_callback() {
  97. return $GLOBALS['theme_test_output'];
  98. }
  99. /**
  100. * Custom theme callback.
  101. */
  102. function _theme_custom_theme() {
  103. return 'test_theme';
  104. }
  105. /**
  106. * Page callback, calls drupal_alter().
  107. *
  108. * This is for testing that the theme can have hook_*_alter() implementations
  109. * that run during page callback execution, even before theme() is called for
  110. * the first time.
  111. */
  112. function _theme_test_alter() {
  113. $data = 'foo';
  114. drupal_alter('theme_test_alter', $data);
  115. return "The altered data is $data.";
  116. }
  117. /**
  118. * Page callback, calls a theme hook suggestion.
  119. */
  120. function _theme_test_suggestion() {
  121. return theme(array('theme_test__suggestion', 'theme_test'), array());
  122. }
  123. /**
  124. * Page callback, calls drupal_add_region_content.
  125. */
  126. function _theme_test_drupal_add_region_content() {
  127. drupal_add_region_content('content', 'World');
  128. return 'Hello';
  129. }
  130. /**
  131. * Theme function for testing theme('theme_test_foo').
  132. */
  133. function theme_theme_test_foo($variables) {
  134. return $variables['foo'];
  135. }