theme.api.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 Form API, such as adding form elements, changing
  72. * default values and removing form elements. See the Form 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 for templates.
  94. *
  95. * This hook allows modules to preprocess theme variables for theme templates.
  96. * It is called for all theme hooks implemented as templates, but not for theme
  97. * hooks implemented as functions. hook_preprocess_HOOK() can be used to
  98. * preprocess variables for a specific theme hook, whether implemented as a
  99. * template or function.
  100. *
  101. * For more detailed information, see theme().
  102. *
  103. * @param $variables
  104. * The variables array (modify in place).
  105. * @param $hook
  106. * The name of the theme hook.
  107. */
  108. function hook_preprocess(&$variables, $hook) {
  109. static $hooks;
  110. // Add contextual links to the variables, if the user has permission.
  111. if (!user_access('access contextual links')) {
  112. return;
  113. }
  114. if (!isset($hooks)) {
  115. $hooks = theme_get_registry();
  116. }
  117. // Determine the primary theme function argument.
  118. if (isset($hooks[$hook]['variables'])) {
  119. $keys = array_keys($hooks[$hook]['variables']);
  120. $key = $keys[0];
  121. }
  122. else {
  123. $key = $hooks[$hook]['render element'];
  124. }
  125. if (isset($variables[$key])) {
  126. $element = $variables[$key];
  127. }
  128. if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) {
  129. $variables['title_suffix']['contextual_links'] = contextual_links_view($element);
  130. if (!empty($variables['title_suffix']['contextual_links'])) {
  131. $variables['classes_array'][] = 'contextual-links-region';
  132. }
  133. }
  134. }
  135. /**
  136. * Preprocess theme variables for a specific theme hook.
  137. *
  138. * This hook allows modules to preprocess theme variables for a specific theme
  139. * hook. It should only be used if a module needs to override or add to the
  140. * theme preprocessing for a theme hook it didn't define.
  141. *
  142. * For more detailed information, see theme().
  143. *
  144. * @param $variables
  145. * The variables array (modify in place).
  146. */
  147. function hook_preprocess_HOOK(&$variables) {
  148. // This example is from rdf_preprocess_image(). It adds an RDF attribute
  149. // to the image hook's variables.
  150. $variables['attributes']['typeof'] = array('foaf:Image');
  151. }
  152. /**
  153. * Process theme variables for templates.
  154. *
  155. * This hook allows modules to process theme variables for theme templates. It
  156. * is called for all theme hooks implemented as templates, but not for theme
  157. * hooks implemented as functions. hook_process_HOOK() can be used to process
  158. * variables for a specific theme hook, whether implemented as a template or
  159. * function.
  160. *
  161. * For more detailed information, see theme().
  162. *
  163. * @param $variables
  164. * The variables array (modify in place).
  165. * @param $hook
  166. * The name of the theme hook.
  167. */
  168. function hook_process(&$variables, $hook) {
  169. // Wraps variables in RDF wrappers.
  170. if (!empty($variables['rdf_template_variable_attributes_array'])) {
  171. foreach ($variables['rdf_template_variable_attributes_array'] as $variable_name => $attributes) {
  172. $context = array(
  173. 'hook' => $hook,
  174. 'variable_name' => $variable_name,
  175. 'variables' => $variables,
  176. );
  177. $variables[$variable_name] = theme('rdf_template_variable_wrapper', array('content' => $variables[$variable_name], 'attributes' => $attributes, 'context' => $context));
  178. }
  179. }
  180. }
  181. /**
  182. * Process theme variables for a specific theme hook.
  183. *
  184. * This hook allows modules to process theme variables for a specific theme
  185. * hook. It should only be used if a module needs to override or add to the
  186. * theme processing for a theme hook it didn't define.
  187. *
  188. * For more detailed information, see theme().
  189. *
  190. * @param $variables
  191. * The variables array (modify in place).
  192. */
  193. function hook_process_HOOK(&$variables) {
  194. // @todo There are no use-cases in Drupal core for this hook. Find one from a
  195. // contributed module, or come up with a good example. Coming up with a good
  196. // example might be tough, since the intent is for nearly everything to be
  197. // achievable via preprocess functions, and for process functions to only be
  198. // used when requiring the later execution time.
  199. }
  200. /**
  201. * Respond to themes being enabled.
  202. *
  203. * @param array $theme_list
  204. * Array containing the names of the themes being enabled.
  205. *
  206. * @see theme_enable()
  207. */
  208. function hook_themes_enabled($theme_list) {
  209. foreach ($theme_list as $theme) {
  210. block_theme_initialize($theme);
  211. }
  212. }
  213. /**
  214. * Respond to themes being disabled.
  215. *
  216. * @param array $theme_list
  217. * Array containing the names of the themes being disabled.
  218. *
  219. * @see theme_disable()
  220. */
  221. function hook_themes_disabled($theme_list) {
  222. // Clear all update module caches.
  223. _update_cache_clear();
  224. }