theme_test.module 5.78 KB
Newer Older
1 2
<?php

3 4 5 6
/**
 * Implements hook_theme().
 */
function theme_test_theme($existing, $type, $theme, $path) {
7 8 9 10
  $items['theme_test'] = array(
    'file' => 'theme_test.inc',
    'variables' => array('foo' => ''),
  );
11 12 13
  $items['theme_test_template_test'] = array(
    'template' => 'theme_test.template_test',
  );
14 15 16
  $items['theme_test_template_test_2'] = array(
    'template' => 'theme_test.template_test',
  );
17 18 19
  $items['theme_test_foo'] = array(
    'variables' => array('foo' => NULL),
  );
20 21 22 23
  $items['theme_test_render_element'] = array(
    'render element' => 'elements',
    'template' => 'theme-test-render-element',
  );
24 25 26
  $items['theme_test_render_element_children'] = array(
    'render element' => 'element',
  );
27 28 29
  $items['theme_test_function_template_override'] = array(
    'variables' => array(),
  );
30 31 32
  return $items;
}

33 34 35 36
/**
 * Implements hook_system_theme_info().
 */
function theme_test_system_theme_info() {
37 38 39
  $themes['test_theme'] = drupal_get_path('module', 'system') . '/tests/themes/test_theme/test_theme.info.yml';
  $themes['test_basetheme'] = drupal_get_path('module', 'system') . '/tests/themes/test_basetheme/test_basetheme.info.yml';
  $themes['test_subtheme'] = drupal_get_path('module', 'system') . '/tests/themes/test_subtheme/test_subtheme.info.yml';
40
  $themes['test_theme_phptemplate'] = drupal_get_path('module', 'system') . '/tests/themes/test_theme_phptemplate/test_theme_phptemplate.info.yml';
41 42 43
  return $themes;
}

44 45 46 47 48 49 50
/**
 * Implements hook_menu().
 */
function theme_test_menu() {
  $items['theme-test/suggestion'] = array(
    'title' => 'Suggestion',
    'page callback' => '_theme_test_suggestion',
51
    'access callback' => TRUE,
52 53 54
    'theme callback' => '_theme_custom_theme',
    'type' => MENU_CALLBACK,
  );
55 56 57
  $items['theme-test/alter'] = array(
    'title' => 'Suggestion',
    'page callback' => '_theme_test_alter',
58
    'access callback' => TRUE,
59 60 61
    'theme callback' => '_theme_custom_theme',
    'type' => MENU_CALLBACK,
  );
62 63
  $items['theme-test/request-listener'] = array(
    'page callback' => 'theme_test_request_listener_page_callback',
64 65 66
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
67 68 69 70 71
  $items['theme-test/template-test'] = array(
    'page callback' => 'theme_test_template_test_page_callback',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
72 73 74 75 76
  $items['theme-test/info/stylesheets'] = array(
    'page callback' => 'theme_test_info_stylesheets',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
77 78 79 80
  $items['theme-test/function-template-overridden'] = array(
    'theme callback' => '_theme_custom_theme',
    'route_name' => 'function_template_override',
  );
81 82 83
  return $items;
}

84 85 86 87
/**
 * Fake registry loading callback.
 */
function _theme_test_load_registry() {
88
  print 'registry initialized';
89
  return array();
90 91 92
}

/**
93
 * Menu callback for testing themed output generated in a request listener.
94
 */
95
function theme_test_request_listener_page_callback() {
96 97 98
  return $GLOBALS['theme_test_output'];
}

99 100 101 102 103 104 105
/**
 * Menu callback for testing template overridding based on filename.
 */
function theme_test_template_test_page_callback() {
  return theme('theme_test_template_test');
}

106
/**
107
 * Page callback; Adds stylesheets to test theme .info.yml property processing.
108
 *
109 110
 * @see test_basetheme.info.yml
 * @see test_subtheme.info.yml
111 112 113 114 115 116 117 118 119 120 121 122 123 124
 * @see \Drupal\system\Tests\Theme\ThemeInfoStylesTest
 * @see http://drupal.org/node/967266#comment-3689670
 */
function theme_test_info_stylesheets() {
  $path = drupal_get_path('module', 'theme_test');
  drupal_add_css("$path/css/base-override.css");
  drupal_add_css("$path/css/base-override.sub-remove.css");
  drupal_add_css("$path/css/base-remove.css");
  drupal_add_css("$path/css/base-remove.sub-override.css");
  drupal_add_css("$path/css/sub-override.css");
  drupal_add_css("$path/css/sub-remove.css");
  return '';
}

125 126 127 128 129 130 131
/**
 * Custom theme callback.
 */
function _theme_custom_theme() {
  return 'test_theme';
}

132 133 134 135 136 137 138 139 140 141 142 143 144
/**
 * Page callback, calls drupal_alter().
 *
 * This is for testing that the theme can have hook_*_alter() implementations
 * that run during page callback execution, even before theme() is called for
 * the first time.
 */
function _theme_test_alter() {
  $data = 'foo';
  drupal_alter('theme_test_alter', $data);
  return "The altered data is $data.";
}

145 146 147 148
/**
 * Page callback, calls a theme hook suggestion.
 */
function _theme_test_suggestion() {
149
  return theme(array('theme_test__suggestion', 'theme_test'), array());
150
}
151 152

/**
153
 * Implements hook_preprocess_HOOK() for html.tpl.php.
154 155
 */
function theme_test_preprocess_html(&$variables) {
156 157
  $variables['html_attributes']['theme_test_html_attribute'] = 'theme test html attribute value';
  $variables['attributes']['theme_test_body_attribute'] = 'theme test body attribute value';
158
}
159 160 161 162 163 164 165

/**
 * Theme function for testing theme('theme_test_foo').
 */
function theme_theme_test_foo($variables) {
  return $variables['foo'];
}
166

167 168 169 170 171 172 173
/**
 * Theme function for testing theme('theme_test_function_template_override').
 */
function theme_theme_test_function_template_override($variables) {
  return 'theme_test_function_template_override test failed.';
}

174 175 176 177 178 179 180
/**
 * Process variables for theme-test-render-element.tpl.php.
 */
function template_preprocess_theme_test_render_element(&$variables) {
  $variables['attributes']['data-variables-are-preprocessed'] = TRUE;
}

181 182 183 184 185 186 187 188 189 190 191 192 193 194
/**
 * Theme function for testing rendering of child elements via drupal_render().
 *
 * Theme hooks defining a 'render element' add an internal '#render_children'
 * property. When this property is found, drupal_render() avoids calling theme()
 * on the top-level element to prevent infinite recursion.
 *
 * @param array $variables
 *   An associative array containing:
 *   - element: An associative array containing the properties of the element.
 */
function theme_theme_test_render_element_children($variables) {
  return drupal_render($variables['element']);
}