annotate modules/system/theme.api.php @ 0:ff03f76ab3fe

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