Skip navigation
Help

theme.api.php

  1. drupal
    1. 7 drupal/modules/system/theme.api.php

Functions & methods

NameDescription
hook_form_system_theme_settings_alterAllow themes to alter the theme-specific settings form.
hook_preprocessPreprocess theme variables.
hook_preprocess_HOOKPreprocess theme variables for a specific theme hook.
hook_processProcess theme variables.
hook_process_HOOKProcess theme variables for a specific theme hook.
hook_themes_disabledRespond to themes being disabled.
hook_themes_enabledRespond to themes being enabled.

File

drupal/modules/system/theme.api.php
View source
  1. <?php
  2. /**
  3. * @defgroup themeable Default theme implementations
  4. * @{
  5. * Functions and templates for the user interface to be implemented by themes.
  6. *
  7. * Drupal's presentation layer is a pluggable system known as the theme
  8. * layer. Each theme can take control over most of Drupal's output, and
  9. * has complete control over the CSS.
  10. *
  11. * Inside Drupal, the theme layer is utilized by the use of the theme()
  12. * function, which is passed the name of a component (the theme hook)
  13. * and an array of variables. For example,
  14. * theme('table', array('header' => $header, 'rows' => $rows));
  15. * Additionally, the theme() function can take an array of theme
  16. * hooks, which can be used to provide 'fallback' implementations to
  17. * allow for more specific control of output. For example, the function:
  18. * theme(array('table__foo', 'table'), $variables) would look to see if
  19. * 'table__foo' is registered anywhere; if it is not, it would 'fall back'
  20. * to the generic 'table' implementation. This can be used to attach specific
  21. * theme functions to named objects, allowing the themer more control over
  22. * specific types of output.
  23. *
  24. * As of Drupal 6, every theme hook is required to be registered by the
  25. * module that owns it, so that Drupal can tell what to do with it and
  26. * to make it simple for themes to identify and override the behavior
  27. * for these calls.
  28. *
  29. * The theme hooks are registered via hook_theme(), which returns an
  30. * array of arrays with information about the hook. It describes the
  31. * arguments the function or template will need, and provides
  32. * defaults for the template in case they are not filled in. If the default
  33. * implementation is a function, by convention it is named theme_HOOK().
  34. *
  35. * Each module should provide a default implementation for theme_hooks that
  36. * it registers. This implementation may be either a function or a template;
  37. * if it is a function it must be specified via hook_theme(). By convention,
  38. * default implementations of theme hooks are named theme_HOOK. Default
  39. * template implementations are stored in the module directory.
  40. *
  41. * Drupal's default template renderer is a simple PHP parsing engine that
  42. * includes the template and stores the output. Drupal's theme engines
  43. * can provide alternate template engines, such as XTemplate, Smarty and
  44. * PHPTal. The most common template engine is PHPTemplate (included with
  45. * Drupal and implemented in phptemplate.engine, which uses Drupal's default
  46. * template renderer.
  47. *
  48. * In order to create theme-specific implementations of these hooks, themes can
  49. * implement their own version of theme hooks, either as functions or templates.
  50. * These implementations will be used instead of the default implementation. If
  51. * using a pure .theme without an engine, the .theme is required to implement
  52. * its own version of hook_theme() to tell Drupal what it is implementing;
  53. * themes utilizing an engine will have their well-named theming functions
  54. * automatically registered for them. While this can vary based upon the theme
  55. * engine, the standard set by phptemplate is that theme functions should be
  56. * named THEMENAME_HOOK. For example, for Drupal's default theme (Bartik) to
  57. * implement the 'table' hook, the phptemplate.engine would find
  58. * bartik_table().
  59. *
  60. * The theme system is described and defined in theme.inc.
  61. *
  62. * @see theme()
  63. * @see hook_theme()
  64. *
  65. * @} End of "defgroup themeable".
  66. */
  67. /**
  68. * Allow themes to alter the theme-specific settings form.
  69. *
  70. * With this hook, themes can alter the theme-specific settings form in any way
  71. * allowable by Drupal's Forms API, such as adding form elements, changing
  72. * default values and removing form elements. See the Forms API documentation on
  73. * api.drupal.org for detailed information.
  74. *
  75. * Note that the base theme's form alterations will be run before any sub-theme
  76. * alterations.
  77. *
  78. * @param $form
  79. * Nested array of form elements that comprise the form.
  80. * @param $form_state
  81. * A keyed array containing the current state of the form.
  82. */
  83. function hook_form_system_theme_settings_alter(&$form, &$form_state) {
  84. // Add a checkbox to toggle the breadcrumb trail.
  85. $form['toggle_breadcrumb'] = array(
  86. '#type' => 'checkbox',
  87. '#title' => t('Display the breadcrumb'),
  88. '#default_value' => theme_get_setting('toggle_breadcrumb'),
  89. '#description' => t('Show a trail of links from the homepage to the current page.'),
  90. );
  91. }
  92. /**
  93. * Preprocess theme variables.
  94. *
  95. * This hook allows modules to preprocess theme variables for theme templates.
  96. * It is called for all invocations of theme(), to allow modules to add to
  97. * or override variables for all theme hooks.
  98. *
  99. * For more detailed information, see theme().
  100. *
  101. * @param $variables
  102. * The variables array (modify in place).
  103. * @param $hook
  104. * The name of the theme hook.
  105. */
  106. function hook_preprocess(&$variables, $hook) {
  107. static $hooks;
  108. // Add contextual links to the variables, if the user has permission.
  109. if (!user_access('access contextual links')) {
  110. return;
  111. }
  112. if (!isset($hooks)) {
  113. $hooks = theme_get_registry();
  114. }
  115. // Determine the primary theme function argument.
  116. if (isset($hooks[$hook]['variables'])) {
  117. $keys = array_keys($hooks[$hook]['variables']);
  118. $key = $keys[0];
  119. }
  120. else {
  121. $key = $hooks[$hook]['render element'];
  122. }
  123. if (isset($variables[$key])) {
  124. $element = $variables[$key];
  125. }
  126. if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) {
  127. $variables['title_suffix']['contextual_links'] = contextual_links_view($element);
  128. if (!empty($variables['title_suffix']['contextual_links'])) {
  129. $variables['classes_array'][] = 'contextual-links-region';
  130. }
  131. }
  132. }
  133. /**
  134. * Preprocess theme variables for a specific theme hook.
  135. *
  136. * This hook allows modules to preprocess theme variables for a specific theme
  137. * hook. It should only be used if a module needs to override or add to the
  138. * theme preprocessing for a theme hook it didn't define.
  139. *
  140. * For more detailed information, see theme().
  141. *
  142. * @param $variables
  143. * The variables array (modify in place).
  144. */
  145. function hook_preprocess_HOOK(&$variables) {
  146. // This example is from rdf_preprocess_image(). It adds an RDF attribute
  147. // to the image hook's variables.
  148. $variables['attributes']['typeof'] = array('foaf:Image');
  149. }
  150. /**
  151. * Process theme variables.
  152. *
  153. * This hook allows modules to process theme variables for theme templates.
  154. * It is called for all invocations of theme(), to allow modules to add to
  155. * or override variables for all theme hooks.
  156. *
  157. * For more detailed information, see theme().
  158. *
  159. * @param $variables
  160. * The variables array (modify in place).
  161. * @param $hook
  162. * The name of the theme hook.
  163. */
  164. function hook_process(&$variables, $hook) {
  165. // Wraps variables in RDF wrappers.
  166. if (!empty($variables['rdf_template_variable_attributes_array'])) {
  167. foreach ($variables['rdf_template_variable_attributes_array'] as $variable_name => $attributes) {
  168. $context = array(
  169. 'hook' => $hook,
  170. 'variable_name' => $variable_name,
  171. 'variables' => $variables,
  172. );
  173. $variables[$variable_name] = theme('rdf_template_variable_wrapper', array('content' => $variables[$variable_name], 'attributes' => $attributes, 'context' => $context));
  174. }
  175. }
  176. }
  177. /**
  178. * Process theme variables for a specific theme hook.
  179. *
  180. * This hook allows modules to process theme variables for a specific theme
  181. * hook. It should only be used if a module needs to override or add to the
  182. * theme processing for a theme hook it didn't define.
  183. *
  184. * For more detailed information, see theme().
  185. *
  186. * @param $variables
  187. * The variables array (modify in place).
  188. */
  189. function hook_process_HOOK(&$variables) {
  190. $variables['classes'] .= ' my_added_class';
  191. }
  192. /**
  193. * Respond to themes being enabled.
  194. *
  195. * @param array $theme_list
  196. * Array containing the names of the themes being enabled.
  197. *
  198. * @see theme_enable()
  199. */
  200. function hook_themes_enabled($theme_list) {
  201. foreach ($theme_list as $theme) {
  202. block_theme_initialize($theme);
  203. }
  204. }
  205. /**
  206. * Respond to themes being disabled.
  207. *
  208. * @param array $theme_list
  209. * Array containing the names of the themes being disabled.
  210. *
  211. * @see theme_disable()
  212. */
  213. function hook_themes_disabled($theme_list) {
  214. // Clear all update module caches.
  215. _update_cache_clear();
  216. }