annotate includes/theme.inc @ 13:134d4b2e75f6

updated quicktabs and google analytics modules
author danieleb <danielebarchiesi@me.com>
date Tue, 29 Oct 2013 13:48:59 +0000
parents ff03f76ab3fe
children
rev   line source
danielebarchiesi@0 1 <?php
danielebarchiesi@0 2
danielebarchiesi@0 3 /**
danielebarchiesi@0 4 * @file
danielebarchiesi@0 5 * The theme system, which controls the output of Drupal.
danielebarchiesi@0 6 *
danielebarchiesi@0 7 * The theme system allows for nearly all output of the Drupal system to be
danielebarchiesi@0 8 * customized by user themes.
danielebarchiesi@0 9 */
danielebarchiesi@0 10
danielebarchiesi@0 11 /**
danielebarchiesi@0 12 * @defgroup content_flags Content markers
danielebarchiesi@0 13 * @{
danielebarchiesi@0 14 * Markers used by theme_mark() and node_mark() to designate content.
danielebarchiesi@0 15 * @see theme_mark(), node_mark()
danielebarchiesi@0 16 */
danielebarchiesi@0 17
danielebarchiesi@0 18 /**
danielebarchiesi@0 19 * Mark content as read.
danielebarchiesi@0 20 */
danielebarchiesi@0 21 define('MARK_READ', 0);
danielebarchiesi@0 22
danielebarchiesi@0 23 /**
danielebarchiesi@0 24 * Mark content as being new.
danielebarchiesi@0 25 */
danielebarchiesi@0 26 define('MARK_NEW', 1);
danielebarchiesi@0 27
danielebarchiesi@0 28 /**
danielebarchiesi@0 29 * Mark content as being updated.
danielebarchiesi@0 30 */
danielebarchiesi@0 31 define('MARK_UPDATED', 2);
danielebarchiesi@0 32
danielebarchiesi@0 33 /**
danielebarchiesi@0 34 * @} End of "Content markers".
danielebarchiesi@0 35 */
danielebarchiesi@0 36
danielebarchiesi@0 37 /**
danielebarchiesi@0 38 * Determines if a theme is available to use.
danielebarchiesi@0 39 *
danielebarchiesi@0 40 * @param $theme
danielebarchiesi@0 41 * Either the name of a theme or a full theme object.
danielebarchiesi@0 42 *
danielebarchiesi@0 43 * @return
danielebarchiesi@0 44 * Boolean TRUE if the theme is enabled or is the site administration theme;
danielebarchiesi@0 45 * FALSE otherwise.
danielebarchiesi@0 46 */
danielebarchiesi@0 47 function drupal_theme_access($theme) {
danielebarchiesi@0 48 if (is_object($theme)) {
danielebarchiesi@0 49 return _drupal_theme_access($theme);
danielebarchiesi@0 50 }
danielebarchiesi@0 51 else {
danielebarchiesi@0 52 $themes = list_themes();
danielebarchiesi@0 53 return isset($themes[$theme]) && _drupal_theme_access($themes[$theme]);
danielebarchiesi@0 54 }
danielebarchiesi@0 55 }
danielebarchiesi@0 56
danielebarchiesi@0 57 /**
danielebarchiesi@0 58 * Helper function for determining access to a theme.
danielebarchiesi@0 59 *
danielebarchiesi@0 60 * @see drupal_theme_access()
danielebarchiesi@0 61 */
danielebarchiesi@0 62 function _drupal_theme_access($theme) {
danielebarchiesi@0 63 $admin_theme = variable_get('admin_theme');
danielebarchiesi@0 64 return !empty($theme->status) || ($admin_theme && $theme->name == $admin_theme);
danielebarchiesi@0 65 }
danielebarchiesi@0 66
danielebarchiesi@0 67 /**
danielebarchiesi@0 68 * Initializes the theme system by loading the theme.
danielebarchiesi@0 69 */
danielebarchiesi@0 70 function drupal_theme_initialize() {
danielebarchiesi@0 71 global $theme, $user, $theme_key;
danielebarchiesi@0 72
danielebarchiesi@0 73 // If $theme is already set, assume the others are set, too, and do nothing
danielebarchiesi@0 74 if (isset($theme)) {
danielebarchiesi@0 75 return;
danielebarchiesi@0 76 }
danielebarchiesi@0 77
danielebarchiesi@0 78 drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
danielebarchiesi@0 79 $themes = list_themes();
danielebarchiesi@0 80
danielebarchiesi@0 81 // Only select the user selected theme if it is available in the
danielebarchiesi@0 82 // list of themes that can be accessed.
danielebarchiesi@0 83 $theme = !empty($user->theme) && drupal_theme_access($user->theme) ? $user->theme : variable_get('theme_default', 'bartik');
danielebarchiesi@0 84
danielebarchiesi@0 85 // Allow modules to override the theme. Validation has already been performed
danielebarchiesi@0 86 // inside menu_get_custom_theme(), so we do not need to check it again here.
danielebarchiesi@0 87 $custom_theme = menu_get_custom_theme();
danielebarchiesi@0 88 $theme = !empty($custom_theme) ? $custom_theme : $theme;
danielebarchiesi@0 89
danielebarchiesi@0 90 // Store the identifier for retrieving theme settings with.
danielebarchiesi@0 91 $theme_key = $theme;
danielebarchiesi@0 92
danielebarchiesi@0 93 // Find all our ancestor themes and put them in an array.
danielebarchiesi@0 94 $base_theme = array();
danielebarchiesi@0 95 $ancestor = $theme;
danielebarchiesi@0 96 while ($ancestor && isset($themes[$ancestor]->base_theme)) {
danielebarchiesi@0 97 $ancestor = $themes[$ancestor]->base_theme;
danielebarchiesi@0 98 $base_theme[] = $themes[$ancestor];
danielebarchiesi@0 99 }
danielebarchiesi@0 100 _drupal_theme_initialize($themes[$theme], array_reverse($base_theme));
danielebarchiesi@0 101
danielebarchiesi@0 102 // Themes can have alter functions, so reset the drupal_alter() cache.
danielebarchiesi@0 103 drupal_static_reset('drupal_alter');
danielebarchiesi@0 104
danielebarchiesi@0 105 // Provide the page with information about the theme that's used, so that a
danielebarchiesi@0 106 // later Ajax request can be rendered using the same theme.
danielebarchiesi@0 107 // @see ajax_base_page_theme()
danielebarchiesi@0 108 $setting['ajaxPageState'] = array(
danielebarchiesi@0 109 'theme' => $theme_key,
danielebarchiesi@0 110 'theme_token' => drupal_get_token($theme_key),
danielebarchiesi@0 111 );
danielebarchiesi@0 112 drupal_add_js($setting, 'setting');
danielebarchiesi@0 113 }
danielebarchiesi@0 114
danielebarchiesi@0 115 /**
danielebarchiesi@0 116 * Initializes the theme system given already loaded information.
danielebarchiesi@0 117 *
danielebarchiesi@0 118 * This function is useful to initialize a theme when no database is present.
danielebarchiesi@0 119 *
danielebarchiesi@0 120 * @param $theme
danielebarchiesi@0 121 * An object with the following information:
danielebarchiesi@0 122 * filename
danielebarchiesi@0 123 * The .info file for this theme. The 'path' to
danielebarchiesi@0 124 * the theme will be in this file's directory. (Required)
danielebarchiesi@0 125 * owner
danielebarchiesi@0 126 * The path to the .theme file or the .engine file to load for
danielebarchiesi@0 127 * the theme. (Required)
danielebarchiesi@0 128 * stylesheet
danielebarchiesi@0 129 * The primary stylesheet for the theme. (Optional)
danielebarchiesi@0 130 * engine
danielebarchiesi@0 131 * The name of theme engine to use. (Optional)
danielebarchiesi@0 132 * @param $base_theme
danielebarchiesi@0 133 * An optional array of objects that represent the 'base theme' if the
danielebarchiesi@0 134 * theme is meant to be derivative of another theme. It requires
danielebarchiesi@0 135 * the same information as the $theme object. It should be in
danielebarchiesi@0 136 * 'oldest first' order, meaning the top level of the chain will
danielebarchiesi@0 137 * be first.
danielebarchiesi@0 138 * @param $registry_callback
danielebarchiesi@0 139 * The callback to invoke to set the theme registry.
danielebarchiesi@0 140 */
danielebarchiesi@0 141 function _drupal_theme_initialize($theme, $base_theme = array(), $registry_callback = '_theme_load_registry') {
danielebarchiesi@0 142 global $theme_info, $base_theme_info, $theme_engine, $theme_path;
danielebarchiesi@0 143 $theme_info = $theme;
danielebarchiesi@0 144 $base_theme_info = $base_theme;
danielebarchiesi@0 145
danielebarchiesi@0 146 $theme_path = dirname($theme->filename);
danielebarchiesi@0 147
danielebarchiesi@0 148 // Prepare stylesheets from this theme as well as all ancestor themes.
danielebarchiesi@0 149 // We work it this way so that we can have child themes override parent
danielebarchiesi@0 150 // theme stylesheets easily.
danielebarchiesi@0 151 $final_stylesheets = array();
danielebarchiesi@0 152
danielebarchiesi@0 153 // Grab stylesheets from base theme
danielebarchiesi@0 154 foreach ($base_theme as $base) {
danielebarchiesi@0 155 if (!empty($base->stylesheets)) {
danielebarchiesi@0 156 foreach ($base->stylesheets as $media => $stylesheets) {
danielebarchiesi@0 157 foreach ($stylesheets as $name => $stylesheet) {
danielebarchiesi@0 158 $final_stylesheets[$media][$name] = $stylesheet;
danielebarchiesi@0 159 }
danielebarchiesi@0 160 }
danielebarchiesi@0 161 }
danielebarchiesi@0 162 }
danielebarchiesi@0 163
danielebarchiesi@0 164 // Add stylesheets used by this theme.
danielebarchiesi@0 165 if (!empty($theme->stylesheets)) {
danielebarchiesi@0 166 foreach ($theme->stylesheets as $media => $stylesheets) {
danielebarchiesi@0 167 foreach ($stylesheets as $name => $stylesheet) {
danielebarchiesi@0 168 $final_stylesheets[$media][$name] = $stylesheet;
danielebarchiesi@0 169 }
danielebarchiesi@0 170 }
danielebarchiesi@0 171 }
danielebarchiesi@0 172
danielebarchiesi@0 173 // And now add the stylesheets properly
danielebarchiesi@0 174 foreach ($final_stylesheets as $media => $stylesheets) {
danielebarchiesi@0 175 foreach ($stylesheets as $stylesheet) {
danielebarchiesi@0 176 drupal_add_css($stylesheet, array('group' => CSS_THEME, 'every_page' => TRUE, 'media' => $media));
danielebarchiesi@0 177 }
danielebarchiesi@0 178 }
danielebarchiesi@0 179
danielebarchiesi@0 180 // Do basically the same as the above for scripts
danielebarchiesi@0 181 $final_scripts = array();
danielebarchiesi@0 182
danielebarchiesi@0 183 // Grab scripts from base theme
danielebarchiesi@0 184 foreach ($base_theme as $base) {
danielebarchiesi@0 185 if (!empty($base->scripts)) {
danielebarchiesi@0 186 foreach ($base->scripts as $name => $script) {
danielebarchiesi@0 187 $final_scripts[$name] = $script;
danielebarchiesi@0 188 }
danielebarchiesi@0 189 }
danielebarchiesi@0 190 }
danielebarchiesi@0 191
danielebarchiesi@0 192 // Add scripts used by this theme.
danielebarchiesi@0 193 if (!empty($theme->scripts)) {
danielebarchiesi@0 194 foreach ($theme->scripts as $name => $script) {
danielebarchiesi@0 195 $final_scripts[$name] = $script;
danielebarchiesi@0 196 }
danielebarchiesi@0 197 }
danielebarchiesi@0 198
danielebarchiesi@0 199 // Add scripts used by this theme.
danielebarchiesi@0 200 foreach ($final_scripts as $script) {
danielebarchiesi@0 201 drupal_add_js($script, array('group' => JS_THEME, 'every_page' => TRUE));
danielebarchiesi@0 202 }
danielebarchiesi@0 203
danielebarchiesi@0 204 $theme_engine = NULL;
danielebarchiesi@0 205
danielebarchiesi@0 206 // Initialize the theme.
danielebarchiesi@0 207 if (isset($theme->engine)) {
danielebarchiesi@0 208 // Include the engine.
danielebarchiesi@0 209 include_once DRUPAL_ROOT . '/' . $theme->owner;
danielebarchiesi@0 210
danielebarchiesi@0 211 $theme_engine = $theme->engine;
danielebarchiesi@0 212 if (function_exists($theme_engine . '_init')) {
danielebarchiesi@0 213 foreach ($base_theme as $base) {
danielebarchiesi@0 214 call_user_func($theme_engine . '_init', $base);
danielebarchiesi@0 215 }
danielebarchiesi@0 216 call_user_func($theme_engine . '_init', $theme);
danielebarchiesi@0 217 }
danielebarchiesi@0 218 }
danielebarchiesi@0 219 else {
danielebarchiesi@0 220 // include non-engine theme files
danielebarchiesi@0 221 foreach ($base_theme as $base) {
danielebarchiesi@0 222 // Include the theme file or the engine.
danielebarchiesi@0 223 if (!empty($base->owner)) {
danielebarchiesi@0 224 include_once DRUPAL_ROOT . '/' . $base->owner;
danielebarchiesi@0 225 }
danielebarchiesi@0 226 }
danielebarchiesi@0 227 // and our theme gets one too.
danielebarchiesi@0 228 if (!empty($theme->owner)) {
danielebarchiesi@0 229 include_once DRUPAL_ROOT . '/' . $theme->owner;
danielebarchiesi@0 230 }
danielebarchiesi@0 231 }
danielebarchiesi@0 232
danielebarchiesi@0 233 if (isset($registry_callback)) {
danielebarchiesi@0 234 _theme_registry_callback($registry_callback, array($theme, $base_theme, $theme_engine));
danielebarchiesi@0 235 }
danielebarchiesi@0 236 }
danielebarchiesi@0 237
danielebarchiesi@0 238 /**
danielebarchiesi@0 239 * Gets the theme registry.
danielebarchiesi@0 240 *
danielebarchiesi@0 241 * @param $complete
danielebarchiesi@0 242 * Optional boolean to indicate whether to return the complete theme registry
danielebarchiesi@0 243 * array or an instance of the ThemeRegistry class. If TRUE, the complete
danielebarchiesi@0 244 * theme registry array will be returned. This is useful if you want to
danielebarchiesi@0 245 * foreach over the whole registry, use array_* functions or inspect it in a
danielebarchiesi@0 246 * debugger. If FALSE, an instance of the ThemeRegistry class will be
danielebarchiesi@0 247 * returned, this provides an ArrayObject which allows it to be accessed
danielebarchiesi@0 248 * with array syntax and isset(), and should be more lightweight
danielebarchiesi@0 249 * than the full registry. Defaults to TRUE.
danielebarchiesi@0 250 *
danielebarchiesi@0 251 * @return
danielebarchiesi@0 252 * The complete theme registry array, or an instance of the ThemeRegistry
danielebarchiesi@0 253 * class.
danielebarchiesi@0 254 */
danielebarchiesi@0 255 function theme_get_registry($complete = TRUE) {
danielebarchiesi@0 256 // Use the advanced drupal_static() pattern, since this is called very often.
danielebarchiesi@0 257 static $drupal_static_fast;
danielebarchiesi@0 258 if (!isset($drupal_static_fast)) {
danielebarchiesi@0 259 $drupal_static_fast['registry'] = &drupal_static('theme_get_registry');
danielebarchiesi@0 260 }
danielebarchiesi@0 261 $theme_registry = &$drupal_static_fast['registry'];
danielebarchiesi@0 262
danielebarchiesi@0 263 // Initialize the theme, if this is called early in the bootstrap, or after
danielebarchiesi@0 264 // static variables have been reset.
danielebarchiesi@0 265 if (!is_array($theme_registry)) {
danielebarchiesi@0 266 drupal_theme_initialize();
danielebarchiesi@0 267 $theme_registry = array();
danielebarchiesi@0 268 }
danielebarchiesi@0 269
danielebarchiesi@0 270 $key = (int) $complete;
danielebarchiesi@0 271
danielebarchiesi@0 272 if (!isset($theme_registry[$key])) {
danielebarchiesi@0 273 list($callback, $arguments) = _theme_registry_callback();
danielebarchiesi@0 274 if (!$complete) {
danielebarchiesi@0 275 $arguments[] = FALSE;
danielebarchiesi@0 276 }
danielebarchiesi@0 277 $theme_registry[$key] = call_user_func_array($callback, $arguments);
danielebarchiesi@0 278 }
danielebarchiesi@0 279
danielebarchiesi@0 280 return $theme_registry[$key];
danielebarchiesi@0 281 }
danielebarchiesi@0 282
danielebarchiesi@0 283 /**
danielebarchiesi@0 284 * Sets the callback that will be used by theme_get_registry().
danielebarchiesi@0 285 *
danielebarchiesi@0 286 * @param $callback
danielebarchiesi@0 287 * The name of the callback function.
danielebarchiesi@0 288 * @param $arguments
danielebarchiesi@0 289 * The arguments to pass to the function.
danielebarchiesi@0 290 */
danielebarchiesi@0 291 function _theme_registry_callback($callback = NULL, array $arguments = array()) {
danielebarchiesi@0 292 static $stored;
danielebarchiesi@0 293 if (isset($callback)) {
danielebarchiesi@0 294 $stored = array($callback, $arguments);
danielebarchiesi@0 295 }
danielebarchiesi@0 296 return $stored;
danielebarchiesi@0 297 }
danielebarchiesi@0 298
danielebarchiesi@0 299 /**
danielebarchiesi@0 300 * Gets the theme_registry cache; if it doesn't exist, builds it.
danielebarchiesi@0 301 *
danielebarchiesi@0 302 * @param $theme
danielebarchiesi@0 303 * The loaded $theme object as returned by list_themes().
danielebarchiesi@0 304 * @param $base_theme
danielebarchiesi@0 305 * An array of loaded $theme objects representing the ancestor themes in
danielebarchiesi@0 306 * oldest first order.
danielebarchiesi@0 307 * @param $theme_engine
danielebarchiesi@0 308 * The name of the theme engine.
danielebarchiesi@0 309 * @param $complete
danielebarchiesi@0 310 * Whether to load the complete theme registry or an instance of the
danielebarchiesi@0 311 * ThemeRegistry class.
danielebarchiesi@0 312 *
danielebarchiesi@0 313 * @return
danielebarchiesi@0 314 * The theme registry array, or an instance of the ThemeRegistry class.
danielebarchiesi@0 315 */
danielebarchiesi@0 316 function _theme_load_registry($theme, $base_theme = NULL, $theme_engine = NULL, $complete = TRUE) {
danielebarchiesi@0 317 if ($complete) {
danielebarchiesi@0 318 // Check the theme registry cache; if it exists, use it.
danielebarchiesi@0 319 $cached = cache_get("theme_registry:$theme->name");
danielebarchiesi@0 320 if (isset($cached->data)) {
danielebarchiesi@0 321 $registry = $cached->data;
danielebarchiesi@0 322 }
danielebarchiesi@0 323 else {
danielebarchiesi@0 324 // If not, build one and cache it.
danielebarchiesi@0 325 $registry = _theme_build_registry($theme, $base_theme, $theme_engine);
danielebarchiesi@0 326 // Only persist this registry if all modules are loaded. This assures a
danielebarchiesi@0 327 // complete set of theme hooks.
danielebarchiesi@0 328 if (module_load_all(NULL)) {
danielebarchiesi@0 329 _theme_save_registry($theme, $registry);
danielebarchiesi@0 330 }
danielebarchiesi@0 331 }
danielebarchiesi@0 332 return $registry;
danielebarchiesi@0 333 }
danielebarchiesi@0 334 else {
danielebarchiesi@0 335 return new ThemeRegistry('theme_registry:runtime:' . $theme->name, 'cache');
danielebarchiesi@0 336 }
danielebarchiesi@0 337 }
danielebarchiesi@0 338
danielebarchiesi@0 339 /**
danielebarchiesi@0 340 * Writes the theme_registry cache into the database.
danielebarchiesi@0 341 */
danielebarchiesi@0 342 function _theme_save_registry($theme, $registry) {
danielebarchiesi@0 343 cache_set("theme_registry:$theme->name", $registry);
danielebarchiesi@0 344 }
danielebarchiesi@0 345
danielebarchiesi@0 346 /**
danielebarchiesi@0 347 * Forces the system to rebuild the theme registry.
danielebarchiesi@0 348 *
danielebarchiesi@0 349 * This function should be called when modules are added to the system, or when
danielebarchiesi@0 350 * a dynamic system needs to add more theme hooks.
danielebarchiesi@0 351 */
danielebarchiesi@0 352 function drupal_theme_rebuild() {
danielebarchiesi@0 353 drupal_static_reset('theme_get_registry');
danielebarchiesi@0 354 cache_clear_all('theme_registry', 'cache', TRUE);
danielebarchiesi@0 355 }
danielebarchiesi@0 356
danielebarchiesi@0 357 /**
danielebarchiesi@0 358 * Builds the run-time theme registry.
danielebarchiesi@0 359 *
danielebarchiesi@0 360 * Extends DrupalCacheArray to allow the theme registry to be accessed as a
danielebarchiesi@0 361 * complete registry, while internally caching only the parts of the registry
danielebarchiesi@0 362 * that are actually in use on the site. On cache misses the complete
danielebarchiesi@0 363 * theme registry is loaded and used to update the run-time cache.
danielebarchiesi@0 364 */
danielebarchiesi@0 365 class ThemeRegistry Extends DrupalCacheArray {
danielebarchiesi@0 366
danielebarchiesi@0 367 /**
danielebarchiesi@0 368 * Whether the partial registry can be persisted to the cache.
danielebarchiesi@0 369 *
danielebarchiesi@0 370 * This is only allowed if all modules and the request method is GET. theme()
danielebarchiesi@0 371 * should be very rarely called on POST requests and this avoids polluting
danielebarchiesi@0 372 * the runtime cache.
danielebarchiesi@0 373 */
danielebarchiesi@0 374 protected $persistable;
danielebarchiesi@0 375
danielebarchiesi@0 376 /**
danielebarchiesi@0 377 * The complete theme registry array.
danielebarchiesi@0 378 */
danielebarchiesi@0 379 protected $completeRegistry;
danielebarchiesi@0 380
danielebarchiesi@0 381 function __construct($cid, $bin) {
danielebarchiesi@0 382 $this->cid = $cid;
danielebarchiesi@0 383 $this->bin = $bin;
danielebarchiesi@0 384 $this->persistable = module_load_all(NULL) && $_SERVER['REQUEST_METHOD'] == 'GET';
danielebarchiesi@0 385
danielebarchiesi@0 386 $data = array();
danielebarchiesi@0 387 if ($this->persistable && $cached = cache_get($this->cid, $this->bin)) {
danielebarchiesi@0 388 $data = $cached->data;
danielebarchiesi@0 389 }
danielebarchiesi@0 390 else {
danielebarchiesi@0 391 // If there is no runtime cache stored, fetch the full theme registry,
danielebarchiesi@0 392 // but then initialize each value to NULL. This allows offsetExists()
danielebarchiesi@0 393 // to function correctly on non-registered theme hooks without triggering
danielebarchiesi@0 394 // a call to resolveCacheMiss().
danielebarchiesi@0 395 $data = $this->initializeRegistry();
danielebarchiesi@0 396 if ($this->persistable) {
danielebarchiesi@0 397 $this->set($data);
danielebarchiesi@0 398 }
danielebarchiesi@0 399 }
danielebarchiesi@0 400 $this->storage = $data;
danielebarchiesi@0 401 }
danielebarchiesi@0 402
danielebarchiesi@0 403 /**
danielebarchiesi@0 404 * Initializes the full theme registry.
danielebarchiesi@0 405 *
danielebarchiesi@0 406 * @return
danielebarchiesi@0 407 * An array with the keys of the full theme registry, but the values
danielebarchiesi@0 408 * initialized to NULL.
danielebarchiesi@0 409 */
danielebarchiesi@0 410 function initializeRegistry() {
danielebarchiesi@0 411 $this->completeRegistry = theme_get_registry();
danielebarchiesi@0 412
danielebarchiesi@0 413 return array_fill_keys(array_keys($this->completeRegistry), NULL);
danielebarchiesi@0 414 }
danielebarchiesi@0 415
danielebarchiesi@0 416 public function offsetExists($offset) {
danielebarchiesi@0 417 // Since the theme registry allows for theme hooks to be requested that
danielebarchiesi@0 418 // are not registered, just check the existence of the key in the registry.
danielebarchiesi@0 419 // Use array_key_exists() here since a NULL value indicates that the theme
danielebarchiesi@0 420 // hook exists but has not yet been requested.
danielebarchiesi@0 421 return array_key_exists($offset, $this->storage);
danielebarchiesi@0 422 }
danielebarchiesi@0 423
danielebarchiesi@0 424 public function offsetGet($offset) {
danielebarchiesi@0 425 // If the offset is set but empty, it is a registered theme hook that has
danielebarchiesi@0 426 // not yet been requested. Offsets that do not exist at all were not
danielebarchiesi@0 427 // registered in hook_theme().
danielebarchiesi@0 428 if (isset($this->storage[$offset])) {
danielebarchiesi@0 429 return $this->storage[$offset];
danielebarchiesi@0 430 }
danielebarchiesi@0 431 elseif (array_key_exists($offset, $this->storage)) {
danielebarchiesi@0 432 return $this->resolveCacheMiss($offset);
danielebarchiesi@0 433 }
danielebarchiesi@0 434 }
danielebarchiesi@0 435
danielebarchiesi@0 436 public function resolveCacheMiss($offset) {
danielebarchiesi@0 437 if (!isset($this->completeRegistry)) {
danielebarchiesi@0 438 $this->completeRegistry = theme_get_registry();
danielebarchiesi@0 439 }
danielebarchiesi@0 440 $this->storage[$offset] = $this->completeRegistry[$offset];
danielebarchiesi@0 441 if ($this->persistable) {
danielebarchiesi@0 442 $this->persist($offset);
danielebarchiesi@0 443 }
danielebarchiesi@0 444 return $this->storage[$offset];
danielebarchiesi@0 445 }
danielebarchiesi@0 446
danielebarchiesi@0 447 public function set($data, $lock = TRUE) {
danielebarchiesi@0 448 $lock_name = $this->cid . ':' . $this->bin;
danielebarchiesi@0 449 if (!$lock || lock_acquire($lock_name)) {
danielebarchiesi@0 450 if ($cached = cache_get($this->cid, $this->bin)) {
danielebarchiesi@0 451 // Use array merge instead of union so that filled in values in $data
danielebarchiesi@0 452 // overwrite empty values in the current cache.
danielebarchiesi@0 453 $data = array_merge($cached->data, $data);
danielebarchiesi@0 454 }
danielebarchiesi@0 455 else {
danielebarchiesi@0 456 $registry = $this->initializeRegistry();
danielebarchiesi@0 457 $data = array_merge($registry, $data);
danielebarchiesi@0 458 }
danielebarchiesi@0 459 cache_set($this->cid, $data, $this->bin);
danielebarchiesi@0 460 if ($lock) {
danielebarchiesi@0 461 lock_release($lock_name);
danielebarchiesi@0 462 }
danielebarchiesi@0 463 }
danielebarchiesi@0 464 }
danielebarchiesi@0 465 }
danielebarchiesi@0 466
danielebarchiesi@0 467 /**
danielebarchiesi@0 468 * Process a single implementation of hook_theme().
danielebarchiesi@0 469 *
danielebarchiesi@0 470 * @param $cache
danielebarchiesi@0 471 * The theme registry that will eventually be cached; It is an associative
danielebarchiesi@0 472 * array keyed by theme hooks, whose values are associative arrays describing
danielebarchiesi@0 473 * the hook:
danielebarchiesi@0 474 * - 'type': The passed-in $type.
danielebarchiesi@0 475 * - 'theme path': The passed-in $path.
danielebarchiesi@0 476 * - 'function': The name of the function generating output for this theme
danielebarchiesi@0 477 * hook. Either defined explicitly in hook_theme() or, if neither 'function'
danielebarchiesi@0 478 * nor 'template' is defined, then the default theme function name is used.
danielebarchiesi@0 479 * The default theme function name is the theme hook prefixed by either
danielebarchiesi@0 480 * 'theme_' for modules or '$name_' for everything else. If 'function' is
danielebarchiesi@0 481 * defined, 'template' is not used.
danielebarchiesi@0 482 * - 'template': The filename of the template generating output for this
danielebarchiesi@0 483 * theme hook. The template is in the directory defined by the 'path' key of
danielebarchiesi@0 484 * hook_theme() or defaults to $path.
danielebarchiesi@0 485 * - 'variables': The variables for this theme hook as defined in
danielebarchiesi@0 486 * hook_theme(). If there is more than one implementation and 'variables' is
danielebarchiesi@0 487 * not specified in a later one, then the previous definition is kept.
danielebarchiesi@0 488 * - 'render element': The renderable element for this theme hook as defined
danielebarchiesi@0 489 * in hook_theme(). If there is more than one implementation and
danielebarchiesi@0 490 * 'render element' is not specified in a later one, then the previous
danielebarchiesi@0 491 * definition is kept.
danielebarchiesi@0 492 * - 'preprocess functions': See theme() for detailed documentation.
danielebarchiesi@0 493 * - 'process functions': See theme() for detailed documentation.
danielebarchiesi@0 494 * @param $name
danielebarchiesi@0 495 * The name of the module, theme engine, base theme engine, theme or base
danielebarchiesi@0 496 * theme implementing hook_theme().
danielebarchiesi@0 497 * @param $type
danielebarchiesi@0 498 * One of 'module', 'theme_engine', 'base_theme_engine', 'theme', or
danielebarchiesi@0 499 * 'base_theme'. Unlike regular hooks that can only be implemented by modules,
danielebarchiesi@0 500 * each of these can implement hook_theme(). _theme_process_registry() is
danielebarchiesi@0 501 * called in aforementioned order and new entries override older ones. For
danielebarchiesi@0 502 * example, if a theme hook is both defined by a module and a theme, then the
danielebarchiesi@0 503 * definition in the theme will be used.
danielebarchiesi@0 504 * @param $theme
danielebarchiesi@0 505 * The loaded $theme object as returned from list_themes().
danielebarchiesi@0 506 * @param $path
danielebarchiesi@0 507 * The directory where $name is. For example, modules/system or
danielebarchiesi@0 508 * themes/bartik.
danielebarchiesi@0 509 *
danielebarchiesi@0 510 * @see theme()
danielebarchiesi@0 511 * @see _theme_build_registry()
danielebarchiesi@0 512 * @see hook_theme()
danielebarchiesi@0 513 * @see list_themes()
danielebarchiesi@0 514 */
danielebarchiesi@0 515 function _theme_process_registry(&$cache, $name, $type, $theme, $path) {
danielebarchiesi@0 516 $result = array();
danielebarchiesi@0 517
danielebarchiesi@0 518 // Processor functions work in two distinct phases with the process
danielebarchiesi@0 519 // functions always being executed after the preprocess functions.
danielebarchiesi@0 520 $variable_process_phases = array(
danielebarchiesi@0 521 'preprocess functions' => 'preprocess',
danielebarchiesi@0 522 'process functions' => 'process',
danielebarchiesi@0 523 );
danielebarchiesi@0 524
danielebarchiesi@0 525 $hook_defaults = array(
danielebarchiesi@0 526 'variables' => TRUE,
danielebarchiesi@0 527 'render element' => TRUE,
danielebarchiesi@0 528 'pattern' => TRUE,
danielebarchiesi@0 529 'base hook' => TRUE,
danielebarchiesi@0 530 );
danielebarchiesi@0 531
danielebarchiesi@0 532 // Invoke the hook_theme() implementation, process what is returned, and
danielebarchiesi@0 533 // merge it into $cache.
danielebarchiesi@0 534 $function = $name . '_theme';
danielebarchiesi@0 535 if (function_exists($function)) {
danielebarchiesi@0 536 $result = $function($cache, $type, $theme, $path);
danielebarchiesi@0 537 foreach ($result as $hook => $info) {
danielebarchiesi@0 538 // When a theme or engine overrides a module's theme function
danielebarchiesi@0 539 // $result[$hook] will only contain key/value pairs for information being
danielebarchiesi@0 540 // overridden. Pull the rest of the information from what was defined by
danielebarchiesi@0 541 // an earlier hook.
danielebarchiesi@0 542
danielebarchiesi@0 543 // Fill in the type and path of the module, theme, or engine that
danielebarchiesi@0 544 // implements this theme function.
danielebarchiesi@0 545 $result[$hook]['type'] = $type;
danielebarchiesi@0 546 $result[$hook]['theme path'] = $path;
danielebarchiesi@0 547
danielebarchiesi@0 548 // If function and file are omitted, default to standard naming
danielebarchiesi@0 549 // conventions.
danielebarchiesi@0 550 if (!isset($info['template']) && !isset($info['function'])) {
danielebarchiesi@0 551 $result[$hook]['function'] = ($type == 'module' ? 'theme_' : $name . '_') . $hook;
danielebarchiesi@0 552 }
danielebarchiesi@0 553
danielebarchiesi@0 554 if (isset($cache[$hook]['includes'])) {
danielebarchiesi@0 555 $result[$hook]['includes'] = $cache[$hook]['includes'];
danielebarchiesi@0 556 }
danielebarchiesi@0 557
danielebarchiesi@0 558 // If the theme implementation defines a file, then also use the path
danielebarchiesi@0 559 // that it defined. Otherwise use the default path. This allows
danielebarchiesi@0 560 // system.module to declare theme functions on behalf of core .include
danielebarchiesi@0 561 // files.
danielebarchiesi@0 562 if (isset($info['file'])) {
danielebarchiesi@0 563 $include_file = isset($info['path']) ? $info['path'] : $path;
danielebarchiesi@0 564 $include_file .= '/' . $info['file'];
danielebarchiesi@0 565 include_once DRUPAL_ROOT . '/' . $include_file;
danielebarchiesi@0 566 $result[$hook]['includes'][] = $include_file;
danielebarchiesi@0 567 }
danielebarchiesi@0 568
danielebarchiesi@0 569 // If the default keys are not set, use the default values registered
danielebarchiesi@0 570 // by the module.
danielebarchiesi@0 571 if (isset($cache[$hook])) {
danielebarchiesi@0 572 $result[$hook] += array_intersect_key($cache[$hook], $hook_defaults);
danielebarchiesi@0 573 }
danielebarchiesi@0 574
danielebarchiesi@0 575 // The following apply only to theming hooks implemented as templates.
danielebarchiesi@0 576 if (isset($info['template'])) {
danielebarchiesi@0 577 // Prepend the current theming path when none is set.
danielebarchiesi@0 578 if (!isset($info['path'])) {
danielebarchiesi@0 579 $result[$hook]['template'] = $path . '/' . $info['template'];
danielebarchiesi@0 580 }
danielebarchiesi@0 581 }
danielebarchiesi@0 582
danielebarchiesi@0 583 // Allow variable processors for all theming hooks, whether the hook is
danielebarchiesi@0 584 // implemented as a template or as a function.
danielebarchiesi@0 585 foreach ($variable_process_phases as $phase_key => $phase) {
danielebarchiesi@0 586 // Check for existing variable processors. Ensure arrayness.
danielebarchiesi@0 587 if (!isset($info[$phase_key]) || !is_array($info[$phase_key])) {
danielebarchiesi@0 588 $info[$phase_key] = array();
danielebarchiesi@0 589 $prefixes = array();
danielebarchiesi@0 590 if ($type == 'module') {
danielebarchiesi@0 591 // Default variable processor prefix.
danielebarchiesi@0 592 $prefixes[] = 'template';
danielebarchiesi@0 593 // Add all modules so they can intervene with their own variable
danielebarchiesi@0 594 // processors. This allows them to provide variable processors even
danielebarchiesi@0 595 // if they are not the owner of the current hook.
danielebarchiesi@0 596 $prefixes += module_list();
danielebarchiesi@0 597 }
danielebarchiesi@0 598 elseif ($type == 'theme_engine' || $type == 'base_theme_engine') {
danielebarchiesi@0 599 // Theme engines get an extra set that come before the normally
danielebarchiesi@0 600 // named variable processors.
danielebarchiesi@0 601 $prefixes[] = $name . '_engine';
danielebarchiesi@0 602 // The theme engine registers on behalf of the theme using the
danielebarchiesi@0 603 // theme's name.
danielebarchiesi@0 604 $prefixes[] = $theme;
danielebarchiesi@0 605 }
danielebarchiesi@0 606 else {
danielebarchiesi@0 607 // This applies when the theme manually registers their own variable
danielebarchiesi@0 608 // processors.
danielebarchiesi@0 609 $prefixes[] = $name;
danielebarchiesi@0 610 }
danielebarchiesi@0 611 foreach ($prefixes as $prefix) {
danielebarchiesi@0 612 // Only use non-hook-specific variable processors for theming hooks
danielebarchiesi@0 613 // implemented as templates. See theme().
danielebarchiesi@0 614 if (isset($info['template']) && function_exists($prefix . '_' . $phase)) {
danielebarchiesi@0 615 $info[$phase_key][] = $prefix . '_' . $phase;
danielebarchiesi@0 616 }
danielebarchiesi@0 617 if (function_exists($prefix . '_' . $phase . '_' . $hook)) {
danielebarchiesi@0 618 $info[$phase_key][] = $prefix . '_' . $phase . '_' . $hook;
danielebarchiesi@0 619 }
danielebarchiesi@0 620 }
danielebarchiesi@0 621 }
danielebarchiesi@0 622 // Check for the override flag and prevent the cached variable
danielebarchiesi@0 623 // processors from being used. This allows themes or theme engines to
danielebarchiesi@0 624 // remove variable processors set earlier in the registry build.
danielebarchiesi@0 625 if (!empty($info['override ' . $phase_key])) {
danielebarchiesi@0 626 // Flag not needed inside the registry.
danielebarchiesi@0 627 unset($result[$hook]['override ' . $phase_key]);
danielebarchiesi@0 628 }
danielebarchiesi@0 629 elseif (isset($cache[$hook][$phase_key]) && is_array($cache[$hook][$phase_key])) {
danielebarchiesi@0 630 $info[$phase_key] = array_merge($cache[$hook][$phase_key], $info[$phase_key]);
danielebarchiesi@0 631 }
danielebarchiesi@0 632 $result[$hook][$phase_key] = $info[$phase_key];
danielebarchiesi@0 633 }
danielebarchiesi@0 634 }
danielebarchiesi@0 635
danielebarchiesi@0 636 // Merge the newly created theme hooks into the existing cache.
danielebarchiesi@0 637 $cache = $result + $cache;
danielebarchiesi@0 638 }
danielebarchiesi@0 639
danielebarchiesi@0 640 // Let themes have variable processors even if they didn't register a
danielebarchiesi@0 641 // template.
danielebarchiesi@0 642 if ($type == 'theme' || $type == 'base_theme') {
danielebarchiesi@0 643 foreach ($cache as $hook => $info) {
danielebarchiesi@0 644 // Check only if not registered by the theme or engine.
danielebarchiesi@0 645 if (empty($result[$hook])) {
danielebarchiesi@0 646 foreach ($variable_process_phases as $phase_key => $phase) {
danielebarchiesi@0 647 if (!isset($info[$phase_key])) {
danielebarchiesi@0 648 $cache[$hook][$phase_key] = array();
danielebarchiesi@0 649 }
danielebarchiesi@0 650 // Only use non-hook-specific variable processors for theming hooks
danielebarchiesi@0 651 // implemented as templates. See theme().
danielebarchiesi@0 652 if (isset($info['template']) && function_exists($name . '_' . $phase)) {
danielebarchiesi@0 653 $cache[$hook][$phase_key][] = $name . '_' . $phase;
danielebarchiesi@0 654 }
danielebarchiesi@0 655 if (function_exists($name . '_' . $phase . '_' . $hook)) {
danielebarchiesi@0 656 $cache[$hook][$phase_key][] = $name . '_' . $phase . '_' . $hook;
danielebarchiesi@0 657 $cache[$hook]['theme path'] = $path;
danielebarchiesi@0 658 }
danielebarchiesi@0 659 // Ensure uniqueness.
danielebarchiesi@0 660 $cache[$hook][$phase_key] = array_unique($cache[$hook][$phase_key]);
danielebarchiesi@0 661 }
danielebarchiesi@0 662 }
danielebarchiesi@0 663 }
danielebarchiesi@0 664 }
danielebarchiesi@0 665 }
danielebarchiesi@0 666
danielebarchiesi@0 667 /**
danielebarchiesi@0 668 * Builds the theme registry cache.
danielebarchiesi@0 669 *
danielebarchiesi@0 670 * @param $theme
danielebarchiesi@0 671 * The loaded $theme object as returned by list_themes().
danielebarchiesi@0 672 * @param $base_theme
danielebarchiesi@0 673 * An array of loaded $theme objects representing the ancestor themes in
danielebarchiesi@0 674 * oldest first order.
danielebarchiesi@0 675 * @param $theme_engine
danielebarchiesi@0 676 * The name of the theme engine.
danielebarchiesi@0 677 */
danielebarchiesi@0 678 function _theme_build_registry($theme, $base_theme, $theme_engine) {
danielebarchiesi@0 679 $cache = array();
danielebarchiesi@0 680 // First, process the theme hooks advertised by modules. This will
danielebarchiesi@0 681 // serve as the basic registry. Since the list of enabled modules is the same
danielebarchiesi@0 682 // regardless of the theme used, this is cached in its own entry to save
danielebarchiesi@0 683 // building it for every theme.
danielebarchiesi@0 684 if ($cached = cache_get('theme_registry:build:modules')) {
danielebarchiesi@0 685 $cache = $cached->data;
danielebarchiesi@0 686 }
danielebarchiesi@0 687 else {
danielebarchiesi@0 688 foreach (module_implements('theme') as $module) {
danielebarchiesi@0 689 _theme_process_registry($cache, $module, 'module', $module, drupal_get_path('module', $module));
danielebarchiesi@0 690 }
danielebarchiesi@0 691 // Only cache this registry if all modules are loaded.
danielebarchiesi@0 692 if (module_load_all(NULL)) {
danielebarchiesi@0 693 cache_set('theme_registry:build:modules', $cache);
danielebarchiesi@0 694 }
danielebarchiesi@0 695 }
danielebarchiesi@0 696
danielebarchiesi@0 697 // Process each base theme.
danielebarchiesi@0 698 foreach ($base_theme as $base) {
danielebarchiesi@0 699 // If the base theme uses a theme engine, process its hooks.
danielebarchiesi@0 700 $base_path = dirname($base->filename);
danielebarchiesi@0 701 if ($theme_engine) {
danielebarchiesi@0 702 _theme_process_registry($cache, $theme_engine, 'base_theme_engine', $base->name, $base_path);
danielebarchiesi@0 703 }
danielebarchiesi@0 704 _theme_process_registry($cache, $base->name, 'base_theme', $base->name, $base_path);
danielebarchiesi@0 705 }
danielebarchiesi@0 706
danielebarchiesi@0 707 // And then the same thing, but for the theme.
danielebarchiesi@0 708 if ($theme_engine) {
danielebarchiesi@0 709 _theme_process_registry($cache, $theme_engine, 'theme_engine', $theme->name, dirname($theme->filename));
danielebarchiesi@0 710 }
danielebarchiesi@0 711
danielebarchiesi@0 712 // Finally, hooks provided by the theme itself.
danielebarchiesi@0 713 _theme_process_registry($cache, $theme->name, 'theme', $theme->name, dirname($theme->filename));
danielebarchiesi@0 714
danielebarchiesi@0 715 // Let modules alter the registry.
danielebarchiesi@0 716 drupal_alter('theme_registry', $cache);
danielebarchiesi@0 717
danielebarchiesi@0 718 // Optimize the registry to not have empty arrays for functions.
danielebarchiesi@0 719 foreach ($cache as $hook => $info) {
danielebarchiesi@0 720 foreach (array('preprocess functions', 'process functions') as $phase) {
danielebarchiesi@0 721 if (empty($info[$phase])) {
danielebarchiesi@0 722 unset($cache[$hook][$phase]);
danielebarchiesi@0 723 }
danielebarchiesi@0 724 }
danielebarchiesi@0 725 }
danielebarchiesi@0 726 return $cache;
danielebarchiesi@0 727 }
danielebarchiesi@0 728
danielebarchiesi@0 729 /**
danielebarchiesi@0 730 * Returns a list of all currently available themes.
danielebarchiesi@0 731 *
danielebarchiesi@0 732 * Retrieved from the database, if available and the site is not in maintenance
danielebarchiesi@0 733 * mode; otherwise compiled freshly from the filesystem.
danielebarchiesi@0 734 *
danielebarchiesi@0 735 * @param $refresh
danielebarchiesi@0 736 * Whether to reload the list of themes from the database. Defaults to FALSE.
danielebarchiesi@0 737 *
danielebarchiesi@0 738 * @return
danielebarchiesi@0 739 * An associative array of the currently available themes. The keys are the
danielebarchiesi@0 740 * themes' machine names and the values are objects having the following
danielebarchiesi@0 741 * properties:
danielebarchiesi@0 742 * - filename: The filepath and name of the .info file.
danielebarchiesi@0 743 * - name: The machine name of the theme.
danielebarchiesi@0 744 * - status: 1 for enabled, 0 for disabled themes.
danielebarchiesi@0 745 * - info: The contents of the .info file.
danielebarchiesi@0 746 * - stylesheets: A two dimensional array, using the first key for the
danielebarchiesi@0 747 * media attribute (e.g. 'all'), the second for the name of the file
danielebarchiesi@0 748 * (e.g. style.css). The value is a complete filepath (e.g.
danielebarchiesi@0 749 * themes/bartik/style.css). Not set if no stylesheets are defined in the
danielebarchiesi@0 750 * .info file.
danielebarchiesi@0 751 * - scripts: An associative array of JavaScripts, using the filename as key
danielebarchiesi@0 752 * and the complete filepath as value. Not set if no scripts are defined in
danielebarchiesi@0 753 * the .info file.
danielebarchiesi@0 754 * - prefix: The base theme engine prefix.
danielebarchiesi@0 755 * - engine: The machine name of the theme engine.
danielebarchiesi@0 756 * - base_theme: If this is a sub-theme, the machine name of the base theme
danielebarchiesi@0 757 * defined in the .info file. Otherwise, the element is not set.
danielebarchiesi@0 758 * - base_themes: If this is a sub-theme, an associative array of the
danielebarchiesi@0 759 * base-theme ancestors of this theme, starting with this theme's base
danielebarchiesi@0 760 * theme, then the base theme's own base theme, etc. Each entry has an
danielebarchiesi@0 761 * array key equal to the theme's machine name, and a value equal to the
danielebarchiesi@0 762 * human-readable theme name; if a theme with matching machine name does
danielebarchiesi@0 763 * not exist in the system, the value will instead be NULL (and since the
danielebarchiesi@0 764 * system would not know whether that theme itself has a base theme, that
danielebarchiesi@0 765 * will end the array of base themes). This is not set if the theme is not
danielebarchiesi@0 766 * a sub-theme.
danielebarchiesi@0 767 * - sub_themes: An associative array of themes on the system that are
danielebarchiesi@0 768 * either direct sub-themes (that is, they declare this theme to be
danielebarchiesi@0 769 * their base theme), direct sub-themes of sub-themes, etc. The keys are
danielebarchiesi@0 770 * the themes' machine names, and the values are the themes' human-readable
danielebarchiesi@0 771 * names. This element is not set if there are no themes on the system that
danielebarchiesi@0 772 * declare this theme as their base theme.
danielebarchiesi@0 773 */
danielebarchiesi@0 774 function list_themes($refresh = FALSE) {
danielebarchiesi@0 775 $list = &drupal_static(__FUNCTION__, array());
danielebarchiesi@0 776
danielebarchiesi@0 777 if ($refresh) {
danielebarchiesi@0 778 $list = array();
danielebarchiesi@0 779 system_list_reset();
danielebarchiesi@0 780 }
danielebarchiesi@0 781
danielebarchiesi@0 782 if (empty($list)) {
danielebarchiesi@0 783 $list = array();
danielebarchiesi@0 784 $themes = array();
danielebarchiesi@0 785 // Extract from the database only when it is available.
danielebarchiesi@0 786 // Also check that the site is not in the middle of an install or update.
danielebarchiesi@0 787 if (!defined('MAINTENANCE_MODE')) {
danielebarchiesi@0 788 try {
danielebarchiesi@0 789 $themes = system_list('theme');
danielebarchiesi@0 790 }
danielebarchiesi@0 791 catch (Exception $e) {
danielebarchiesi@0 792 // If the database is not available, rebuild the theme data.
danielebarchiesi@0 793 $themes = _system_rebuild_theme_data();
danielebarchiesi@0 794 }
danielebarchiesi@0 795 }
danielebarchiesi@0 796 else {
danielebarchiesi@0 797 // Scan the installation when the database should not be read.
danielebarchiesi@0 798 $themes = _system_rebuild_theme_data();
danielebarchiesi@0 799 }
danielebarchiesi@0 800
danielebarchiesi@0 801 foreach ($themes as $theme) {
danielebarchiesi@0 802 foreach ($theme->info['stylesheets'] as $media => $stylesheets) {
danielebarchiesi@0 803 foreach ($stylesheets as $stylesheet => $path) {
danielebarchiesi@0 804 $theme->stylesheets[$media][$stylesheet] = $path;
danielebarchiesi@0 805 }
danielebarchiesi@0 806 }
danielebarchiesi@0 807 foreach ($theme->info['scripts'] as $script => $path) {
danielebarchiesi@0 808 $theme->scripts[$script] = $path;
danielebarchiesi@0 809 }
danielebarchiesi@0 810 if (isset($theme->info['engine'])) {
danielebarchiesi@0 811 $theme->engine = $theme->info['engine'];
danielebarchiesi@0 812 }
danielebarchiesi@0 813 if (isset($theme->info['base theme'])) {
danielebarchiesi@0 814 $theme->base_theme = $theme->info['base theme'];
danielebarchiesi@0 815 }
danielebarchiesi@0 816 // Status is normally retrieved from the database. Add zero values when
danielebarchiesi@0 817 // read from the installation directory to prevent notices.
danielebarchiesi@0 818 if (!isset($theme->status)) {
danielebarchiesi@0 819 $theme->status = 0;
danielebarchiesi@0 820 }
danielebarchiesi@0 821 $list[$theme->name] = $theme;
danielebarchiesi@0 822 }
danielebarchiesi@0 823 }
danielebarchiesi@0 824
danielebarchiesi@0 825 return $list;
danielebarchiesi@0 826 }
danielebarchiesi@0 827
danielebarchiesi@0 828 /**
danielebarchiesi@0 829 * Find all the base themes for the specified theme.
danielebarchiesi@0 830 *
danielebarchiesi@0 831 * Themes can inherit templates and function implementations from earlier themes.
danielebarchiesi@0 832 *
danielebarchiesi@0 833 * @param $themes
danielebarchiesi@0 834 * An array of available themes.
danielebarchiesi@0 835 * @param $key
danielebarchiesi@0 836 * The name of the theme whose base we are looking for.
danielebarchiesi@0 837 * @param $used_keys
danielebarchiesi@0 838 * A recursion parameter preventing endless loops.
danielebarchiesi@0 839 * @return
danielebarchiesi@0 840 * Returns an array of all of the theme's ancestors; the first element's value
danielebarchiesi@0 841 * will be NULL if an error occurred.
danielebarchiesi@0 842 */
danielebarchiesi@0 843 function drupal_find_base_themes($themes, $key, $used_keys = array()) {
danielebarchiesi@0 844 $base_key = $themes[$key]->info['base theme'];
danielebarchiesi@0 845 // Does the base theme exist?
danielebarchiesi@0 846 if (!isset($themes[$base_key])) {
danielebarchiesi@0 847 return array($base_key => NULL);
danielebarchiesi@0 848 }
danielebarchiesi@0 849
danielebarchiesi@0 850 $current_base_theme = array($base_key => $themes[$base_key]->info['name']);
danielebarchiesi@0 851
danielebarchiesi@0 852 // Is the base theme itself a child of another theme?
danielebarchiesi@0 853 if (isset($themes[$base_key]->info['base theme'])) {
danielebarchiesi@0 854 // Do we already know the base themes of this theme?
danielebarchiesi@0 855 if (isset($themes[$base_key]->base_themes)) {
danielebarchiesi@0 856 return $themes[$base_key]->base_themes + $current_base_theme;
danielebarchiesi@0 857 }
danielebarchiesi@0 858 // Prevent loops.
danielebarchiesi@0 859 if (!empty($used_keys[$base_key])) {
danielebarchiesi@0 860 return array($base_key => NULL);
danielebarchiesi@0 861 }
danielebarchiesi@0 862 $used_keys[$base_key] = TRUE;
danielebarchiesi@0 863 return drupal_find_base_themes($themes, $base_key, $used_keys) + $current_base_theme;
danielebarchiesi@0 864 }
danielebarchiesi@0 865 // If we get here, then this is our parent theme.
danielebarchiesi@0 866 return $current_base_theme;
danielebarchiesi@0 867 }
danielebarchiesi@0 868
danielebarchiesi@0 869 /**
danielebarchiesi@0 870 * Generates themed output.
danielebarchiesi@0 871 *
danielebarchiesi@0 872 * All requests for themed output must go through this function (however,
danielebarchiesi@0 873 * calling the theme() function directly is strongly discouraged - see next
danielebarchiesi@0 874 * paragraph). It examines the request and routes it to the appropriate
danielebarchiesi@0 875 * @link themeable theme function or template @endlink, by checking the theme
danielebarchiesi@0 876 * registry.
danielebarchiesi@0 877 *
danielebarchiesi@0 878 * Avoid calling this function directly. It is preferable to replace direct
danielebarchiesi@0 879 * calls to the theme() function with calls to drupal_render() by passing a
danielebarchiesi@0 880 * render array with a #theme key to drupal_render(), which in turn calls
danielebarchiesi@0 881 * theme().
danielebarchiesi@0 882 *
danielebarchiesi@0 883 * @section sec_theme_hooks Theme Hooks
danielebarchiesi@0 884 * Most commonly, the first argument to this function is the name of the theme
danielebarchiesi@0 885 * hook. For instance, to theme a taxonomy term, the theme hook name is
danielebarchiesi@0 886 * 'taxonomy_term'. Modules register theme hooks within a hook_theme()
danielebarchiesi@0 887 * implementation and provide a default implementation via a function named
danielebarchiesi@0 888 * theme_HOOK() (e.g., theme_taxonomy_term()) or via a template file named
danielebarchiesi@0 889 * according to the value of the 'template' key registered with the theme hook
danielebarchiesi@0 890 * (see hook_theme() for details). Default templates are implemented with the
danielebarchiesi@0 891 * PHPTemplate rendering engine and are named the same as the theme hook, with
danielebarchiesi@0 892 * underscores changed to hyphens, so for the 'taxonomy_term' theme hook, the
danielebarchiesi@0 893 * default template is 'taxonomy-term.tpl.php'.
danielebarchiesi@0 894 *
danielebarchiesi@0 895 * @subsection sub_overriding_theme_hooks Overriding Theme Hooks
danielebarchiesi@0 896 * Themes may also register new theme hooks within a hook_theme()
danielebarchiesi@0 897 * implementation, but it is more common for themes to override default
danielebarchiesi@0 898 * implementations provided by modules than to register entirely new theme
danielebarchiesi@0 899 * hooks. Themes can override a default implementation by implementing a
danielebarchiesi@0 900 * function named THEME_HOOK() (for example, the 'bartik' theme overrides the
danielebarchiesi@0 901 * default implementation of the 'menu_tree' theme hook by implementing a
danielebarchiesi@0 902 * bartik_menu_tree() function), or by adding a template file within its folder
danielebarchiesi@0 903 * structure that follows the template naming structure used by the theme's
danielebarchiesi@0 904 * rendering engine (for example, since the Bartik theme uses the PHPTemplate
danielebarchiesi@0 905 * rendering engine, it overrides the default implementation of the 'page' theme
danielebarchiesi@0 906 * hook by containing a 'page.tpl.php' file within its folder structure).
danielebarchiesi@0 907 *
danielebarchiesi@0 908 * @subsection sub_preprocess_templates Preprocessing for Template Files
danielebarchiesi@0 909 * If the implementation is a template file, several functions are called
danielebarchiesi@0 910 * before the template file is invoked, to modify the $variables array. These
danielebarchiesi@0 911 * fall into the "preprocessing" phase and the "processing" phase, and are
danielebarchiesi@0 912 * executed (if they exist), in the following order (note that in the following
danielebarchiesi@0 913 * list, HOOK indicates the theme hook name, MODULE indicates a module name,
danielebarchiesi@0 914 * THEME indicates a theme name, and ENGINE indicates a theme engine name):
danielebarchiesi@0 915 * - template_preprocess(&$variables, $hook): Creates a default set of
danielebarchiesi@0 916 * variables for all theme hooks with template implementations.
danielebarchiesi@0 917 * - template_preprocess_HOOK(&$variables): Should be implemented by the module
danielebarchiesi@0 918 * that registers the theme hook, to set up default variables.
danielebarchiesi@0 919 * - MODULE_preprocess(&$variables, $hook): hook_preprocess() is invoked on all
danielebarchiesi@0 920 * implementing modules.
danielebarchiesi@0 921 * - MODULE_preprocess_HOOK(&$variables): hook_preprocess_HOOK() is invoked on
danielebarchiesi@0 922 * all implementing modules, so that modules that didn't define the theme
danielebarchiesi@0 923 * hook can alter the variables.
danielebarchiesi@0 924 * - ENGINE_engine_preprocess(&$variables, $hook): Allows the theme engine to
danielebarchiesi@0 925 * set necessary variables for all theme hooks with template implementations.
danielebarchiesi@0 926 * - ENGINE_engine_preprocess_HOOK(&$variables): Allows the theme engine to set
danielebarchiesi@0 927 * necessary variables for the particular theme hook.
danielebarchiesi@0 928 * - THEME_preprocess(&$variables, $hook): Allows the theme to set necessary
danielebarchiesi@0 929 * variables for all theme hooks with template implementations.
danielebarchiesi@0 930 * - THEME_preprocess_HOOK(&$variables): Allows the theme to set necessary
danielebarchiesi@0 931 * variables specific to the particular theme hook.
danielebarchiesi@0 932 * - template_process(&$variables, $hook): Creates an additional set of default
danielebarchiesi@0 933 * variables for all theme hooks with template implementations. The variables
danielebarchiesi@0 934 * created in this function are derived from ones created by
danielebarchiesi@0 935 * template_preprocess(), but potentially altered by the other preprocess
danielebarchiesi@0 936 * functions listed above. For example, any preprocess function can add to or
danielebarchiesi@0 937 * modify the $variables['attributes_array'] variable, and after all of them
danielebarchiesi@0 938 * have finished executing, template_process() flattens it into a
danielebarchiesi@0 939 * $variables['attributes'] string for convenient use by templates.
danielebarchiesi@0 940 * - template_process_HOOK(&$variables): Should be implemented by the module
danielebarchiesi@0 941 * that registers the theme hook, if it needs to perform additional variable
danielebarchiesi@0 942 * processing after all preprocess functions have finished.
danielebarchiesi@0 943 * - MODULE_process(&$variables, $hook): hook_process() is invoked on all
danielebarchiesi@0 944 * implementing modules.
danielebarchiesi@0 945 * - MODULE_process_HOOK(&$variables): hook_process_HOOK() is invoked on
danielebarchiesi@0 946 * on all implementing modules, so that modules that didn't define the theme
danielebarchiesi@0 947 * hook can alter the variables.
danielebarchiesi@0 948 * - ENGINE_engine_process(&$variables, $hook): Allows the theme engine to
danielebarchiesi@0 949 * process variables for all theme hooks with template implementations.
danielebarchiesi@0 950 * - ENGINE_engine_process_HOOK(&$variables): Allows the theme engine to process
danielebarchiesi@0 951 * the variables specific to the theme hook.
danielebarchiesi@0 952 * - THEME_process(&$variables, $hook): Allows the theme to process the
danielebarchiesi@0 953 * variables for all theme hooks with template implementations.
danielebarchiesi@0 954 * - THEME_process_HOOK(&$variables): Allows the theme to process the
danielebarchiesi@0 955 * variables specific to the theme hook.
danielebarchiesi@0 956 *
danielebarchiesi@0 957 * @subsection sub_preprocess_theme_funcs Preprocessing for Theme Functions
danielebarchiesi@0 958 * If the implementation is a function, only the theme-hook-specific preprocess
danielebarchiesi@0 959 * and process functions (the ones ending in _HOOK) are called from the
danielebarchiesi@0 960 * list above. This is because theme hooks with function implementations
danielebarchiesi@0 961 * need to be fast, and calling the non-theme-hook-specific preprocess and
danielebarchiesi@0 962 * process functions for them would incur a noticeable performance penalty.
danielebarchiesi@0 963 *
danielebarchiesi@0 964 * @subsection sub_alternate_suggestions Suggesting Alternate Hooks
danielebarchiesi@0 965 * There are two special variables that these preprocess and process functions
danielebarchiesi@0 966 * can set: 'theme_hook_suggestion' and 'theme_hook_suggestions'. These will be
danielebarchiesi@0 967 * merged together to form a list of 'suggested' alternate theme hooks to use,
danielebarchiesi@0 968 * in reverse order of priority. theme_hook_suggestion will always be a higher
danielebarchiesi@0 969 * priority than items in theme_hook_suggestions. theme() will use the
danielebarchiesi@0 970 * highest priority implementation that exists. If none exists, theme() will
danielebarchiesi@0 971 * use the implementation for the theme hook it was called with. These
danielebarchiesi@0 972 * suggestions are similar to and are used for similar reasons as calling
danielebarchiesi@0 973 * theme() with an array as the $hook parameter (see below). The difference
danielebarchiesi@0 974 * is whether the suggestions are determined by the code that calls theme() or
danielebarchiesi@0 975 * by a preprocess or process function.
danielebarchiesi@0 976 *
danielebarchiesi@0 977 * @param $hook
danielebarchiesi@0 978 * The name of the theme hook to call. If the name contains a
danielebarchiesi@0 979 * double-underscore ('__') and there isn't an implementation for the full
danielebarchiesi@0 980 * name, the part before the '__' is checked. This allows a fallback to a
danielebarchiesi@0 981 * more generic implementation. For example, if theme('links__node', ...) is
danielebarchiesi@0 982 * called, but there is no implementation of that theme hook, then the
danielebarchiesi@0 983 * 'links' implementation is used. This process is iterative, so if
danielebarchiesi@0 984 * theme('links__contextual__node', ...) is called, theme() checks for the
danielebarchiesi@0 985 * following implementations, and uses the first one that exists:
danielebarchiesi@0 986 * - links__contextual__node
danielebarchiesi@0 987 * - links__contextual
danielebarchiesi@0 988 * - links
danielebarchiesi@0 989 * This allows themes to create specific theme implementations for named
danielebarchiesi@0 990 * objects and contexts of otherwise generic theme hooks. The $hook parameter
danielebarchiesi@0 991 * may also be an array, in which case the first theme hook that has an
danielebarchiesi@0 992 * implementation is used. This allows for the code that calls theme() to
danielebarchiesi@0 993 * explicitly specify the fallback order in a situation where using the '__'
danielebarchiesi@0 994 * convention is not desired or is insufficient.
danielebarchiesi@0 995 * @param $variables
danielebarchiesi@0 996 * An associative array of variables to merge with defaults from the theme
danielebarchiesi@0 997 * registry, pass to preprocess and process functions for modification, and
danielebarchiesi@0 998 * finally, pass to the function or template implementing the theme hook.
danielebarchiesi@0 999 * Alternatively, this can be a renderable array, in which case, its
danielebarchiesi@0 1000 * properties are mapped to variables expected by the theme hook
danielebarchiesi@0 1001 * implementations.
danielebarchiesi@0 1002 *
danielebarchiesi@0 1003 * @return
danielebarchiesi@0 1004 * An HTML string representing the themed output.
danielebarchiesi@0 1005 *
danielebarchiesi@0 1006 * @see drupal_render()
danielebarchiesi@0 1007 * @see themeable
danielebarchiesi@0 1008 * @see hook_theme()
danielebarchiesi@0 1009 * @see template_preprocess()
danielebarchiesi@0 1010 * @see template_process()
danielebarchiesi@0 1011 */
danielebarchiesi@0 1012 function theme($hook, $variables = array()) {
danielebarchiesi@0 1013 // If called before all modules are loaded, we do not necessarily have a full
danielebarchiesi@0 1014 // theme registry to work with, and therefore cannot process the theme
danielebarchiesi@0 1015 // request properly. See also _theme_load_registry().
danielebarchiesi@0 1016 if (!module_load_all(NULL) && !defined('MAINTENANCE_MODE')) {
danielebarchiesi@0 1017 throw new Exception(t('theme() may not be called until all modules are loaded.'));
danielebarchiesi@0 1018 }
danielebarchiesi@0 1019
danielebarchiesi@0 1020 $hooks = theme_get_registry(FALSE);
danielebarchiesi@0 1021
danielebarchiesi@0 1022 // If an array of hook candidates were passed, use the first one that has an
danielebarchiesi@0 1023 // implementation.
danielebarchiesi@0 1024 if (is_array($hook)) {
danielebarchiesi@0 1025 foreach ($hook as $candidate) {
danielebarchiesi@0 1026 if (isset($hooks[$candidate])) {
danielebarchiesi@0 1027 break;
danielebarchiesi@0 1028 }
danielebarchiesi@0 1029 }
danielebarchiesi@0 1030 $hook = $candidate;
danielebarchiesi@0 1031 }
danielebarchiesi@0 1032
danielebarchiesi@0 1033 // If there's no implementation, check for more generic fallbacks. If there's
danielebarchiesi@0 1034 // still no implementation, log an error and return an empty string.
danielebarchiesi@0 1035 if (!isset($hooks[$hook])) {
danielebarchiesi@0 1036 // Iteratively strip everything after the last '__' delimiter, until an
danielebarchiesi@0 1037 // implementation is found.
danielebarchiesi@0 1038 while ($pos = strrpos($hook, '__')) {
danielebarchiesi@0 1039 $hook = substr($hook, 0, $pos);
danielebarchiesi@0 1040 if (isset($hooks[$hook])) {
danielebarchiesi@0 1041 break;
danielebarchiesi@0 1042 }
danielebarchiesi@0 1043 }
danielebarchiesi@0 1044 if (!isset($hooks[$hook])) {
danielebarchiesi@0 1045 // Only log a message when not trying theme suggestions ($hook being an
danielebarchiesi@0 1046 // array).
danielebarchiesi@0 1047 if (!isset($candidate)) {
danielebarchiesi@0 1048 watchdog('theme', 'Theme hook %hook not found.', array('%hook' => $hook), WATCHDOG_WARNING);
danielebarchiesi@0 1049 }
danielebarchiesi@0 1050 return '';
danielebarchiesi@0 1051 }
danielebarchiesi@0 1052 }
danielebarchiesi@0 1053
danielebarchiesi@0 1054 $info = $hooks[$hook];
danielebarchiesi@0 1055 global $theme_path;
danielebarchiesi@0 1056 $temp = $theme_path;
danielebarchiesi@0 1057 // point path_to_theme() to the currently used theme path:
danielebarchiesi@0 1058 $theme_path = $info['theme path'];
danielebarchiesi@0 1059
danielebarchiesi@0 1060 // Include a file if the theme function or variable processor is held
danielebarchiesi@0 1061 // elsewhere.
danielebarchiesi@0 1062 if (!empty($info['includes'])) {
danielebarchiesi@0 1063 foreach ($info['includes'] as $include_file) {
danielebarchiesi@0 1064 include_once DRUPAL_ROOT . '/' . $include_file;
danielebarchiesi@0 1065 }
danielebarchiesi@0 1066 }
danielebarchiesi@0 1067
danielebarchiesi@0 1068 // If a renderable array is passed as $variables, then set $variables to
danielebarchiesi@0 1069 // the arguments expected by the theme function.
danielebarchiesi@0 1070 if (isset($variables['#theme']) || isset($variables['#theme_wrappers'])) {
danielebarchiesi@0 1071 $element = $variables;
danielebarchiesi@0 1072 $variables = array();
danielebarchiesi@0 1073 if (isset($info['variables'])) {
danielebarchiesi@0 1074 foreach (array_keys($info['variables']) as $name) {
danielebarchiesi@0 1075 if (isset($element["#$name"])) {
danielebarchiesi@0 1076 $variables[$name] = $element["#$name"];
danielebarchiesi@0 1077 }
danielebarchiesi@0 1078 }
danielebarchiesi@0 1079 }
danielebarchiesi@0 1080 else {
danielebarchiesi@0 1081 $variables[$info['render element']] = $element;
danielebarchiesi@0 1082 }
danielebarchiesi@0 1083 }
danielebarchiesi@0 1084
danielebarchiesi@0 1085 // Merge in argument defaults.
danielebarchiesi@0 1086 if (!empty($info['variables'])) {
danielebarchiesi@0 1087 $variables += $info['variables'];
danielebarchiesi@0 1088 }
danielebarchiesi@0 1089 elseif (!empty($info['render element'])) {
danielebarchiesi@0 1090 $variables += array($info['render element'] => array());
danielebarchiesi@0 1091 }
danielebarchiesi@0 1092
danielebarchiesi@0 1093 // Invoke the variable processors, if any. The processors may specify
danielebarchiesi@0 1094 // alternate suggestions for which hook's template/function to use. If the
danielebarchiesi@0 1095 // hook is a suggestion of a base hook, invoke the variable processors of
danielebarchiesi@0 1096 // the base hook, but retain the suggestion as a high priority suggestion to
danielebarchiesi@0 1097 // be used unless overridden by a variable processor function.
danielebarchiesi@0 1098 if (isset($info['base hook'])) {
danielebarchiesi@0 1099 $base_hook = $info['base hook'];
danielebarchiesi@0 1100 $base_hook_info = $hooks[$base_hook];
danielebarchiesi@0 1101 // Include files required by the base hook, since its variable processors
danielebarchiesi@0 1102 // might reside there.
danielebarchiesi@0 1103 if (!empty($base_hook_info['includes'])) {
danielebarchiesi@0 1104 foreach ($base_hook_info['includes'] as $include_file) {
danielebarchiesi@0 1105 include_once DRUPAL_ROOT . '/' . $include_file;
danielebarchiesi@0 1106 }
danielebarchiesi@0 1107 }
danielebarchiesi@0 1108 if (isset($base_hook_info['preprocess functions']) || isset($base_hook_info['process functions'])) {
danielebarchiesi@0 1109 $variables['theme_hook_suggestion'] = $hook;
danielebarchiesi@0 1110 $hook = $base_hook;
danielebarchiesi@0 1111 $info = $base_hook_info;
danielebarchiesi@0 1112 }
danielebarchiesi@0 1113 }
danielebarchiesi@0 1114 if (isset($info['preprocess functions']) || isset($info['process functions'])) {
danielebarchiesi@0 1115 $variables['theme_hook_suggestions'] = array();
danielebarchiesi@0 1116 foreach (array('preprocess functions', 'process functions') as $phase) {
danielebarchiesi@0 1117 if (!empty($info[$phase])) {
danielebarchiesi@0 1118 foreach ($info[$phase] as $processor_function) {
danielebarchiesi@0 1119 if (function_exists($processor_function)) {
danielebarchiesi@0 1120 // We don't want a poorly behaved process function changing $hook.
danielebarchiesi@0 1121 $hook_clone = $hook;
danielebarchiesi@0 1122 $processor_function($variables, $hook_clone);
danielebarchiesi@0 1123 }
danielebarchiesi@0 1124 }
danielebarchiesi@0 1125 }
danielebarchiesi@0 1126 }
danielebarchiesi@0 1127 // If the preprocess/process functions specified hook suggestions, and the
danielebarchiesi@0 1128 // suggestion exists in the theme registry, use it instead of the hook that
danielebarchiesi@0 1129 // theme() was called with. This allows the preprocess/process step to
danielebarchiesi@0 1130 // route to a more specific theme hook. For example, a function may call
danielebarchiesi@0 1131 // theme('node', ...), but a preprocess function can add 'node__article' as
danielebarchiesi@0 1132 // a suggestion, enabling a theme to have an alternate template file for
danielebarchiesi@0 1133 // article nodes. Suggestions are checked in the following order:
danielebarchiesi@0 1134 // - The 'theme_hook_suggestion' variable is checked first. It overrides
danielebarchiesi@0 1135 // all others.
danielebarchiesi@0 1136 // - The 'theme_hook_suggestions' variable is checked in FILO order, so the
danielebarchiesi@0 1137 // last suggestion added to the array takes precedence over suggestions
danielebarchiesi@0 1138 // added earlier.
danielebarchiesi@0 1139 $suggestions = array();
danielebarchiesi@0 1140 if (!empty($variables['theme_hook_suggestions'])) {
danielebarchiesi@0 1141 $suggestions = $variables['theme_hook_suggestions'];
danielebarchiesi@0 1142 }
danielebarchiesi@0 1143 if (!empty($variables['theme_hook_suggestion'])) {
danielebarchiesi@0 1144 $suggestions[] = $variables['theme_hook_suggestion'];
danielebarchiesi@0 1145 }
danielebarchiesi@0 1146 foreach (array_reverse($suggestions) as $suggestion) {
danielebarchiesi@0 1147 if (isset($hooks[$suggestion])) {
danielebarchiesi@0 1148 $info = $hooks[$suggestion];
danielebarchiesi@0 1149 break;
danielebarchiesi@0 1150 }
danielebarchiesi@0 1151 }
danielebarchiesi@0 1152 }
danielebarchiesi@0 1153
danielebarchiesi@0 1154 // Generate the output using either a function or a template.
danielebarchiesi@0 1155 $output = '';
danielebarchiesi@0 1156 if (isset($info['function'])) {
danielebarchiesi@0 1157 if (function_exists($info['function'])) {
danielebarchiesi@0 1158 $output = $info['function']($variables);
danielebarchiesi@0 1159 }
danielebarchiesi@0 1160 }
danielebarchiesi@0 1161 else {
danielebarchiesi@0 1162 // Default render function and extension.
danielebarchiesi@0 1163 $render_function = 'theme_render_template';
danielebarchiesi@0 1164 $extension = '.tpl.php';
danielebarchiesi@0 1165
danielebarchiesi@0 1166 // The theme engine may use a different extension and a different renderer.
danielebarchiesi@0 1167 global $theme_engine;
danielebarchiesi@0 1168 if (isset($theme_engine)) {
danielebarchiesi@0 1169 if ($info['type'] != 'module') {
danielebarchiesi@0 1170 if (function_exists($theme_engine . '_render_template')) {
danielebarchiesi@0 1171 $render_function = $theme_engine . '_render_template';
danielebarchiesi@0 1172 }
danielebarchiesi@0 1173 $extension_function = $theme_engine . '_extension';
danielebarchiesi@0 1174 if (function_exists($extension_function)) {
danielebarchiesi@0 1175 $extension = $extension_function();
danielebarchiesi@0 1176 }
danielebarchiesi@0 1177 }
danielebarchiesi@0 1178 }
danielebarchiesi@0 1179
danielebarchiesi@0 1180 // In some cases, a template implementation may not have had
danielebarchiesi@0 1181 // template_preprocess() run (for example, if the default implementation is
danielebarchiesi@0 1182 // a function, but a template overrides that default implementation). In
danielebarchiesi@0 1183 // these cases, a template should still be able to expect to have access to
danielebarchiesi@0 1184 // the variables provided by template_preprocess(), so we add them here if
danielebarchiesi@0 1185 // they don't already exist. We don't want to run template_preprocess()
danielebarchiesi@0 1186 // twice (it would be inefficient and mess up zebra striping), so we use the
danielebarchiesi@0 1187 // 'directory' variable to determine if it has already run, which while not
danielebarchiesi@0 1188 // completely intuitive, is reasonably safe, and allows us to save on the
danielebarchiesi@0 1189 // overhead of adding some new variable to track that.
danielebarchiesi@0 1190 if (!isset($variables['directory'])) {
danielebarchiesi@0 1191 $default_template_variables = array();
danielebarchiesi@0 1192 template_preprocess($default_template_variables, $hook);
danielebarchiesi@0 1193 $variables += $default_template_variables;
danielebarchiesi@0 1194 }
danielebarchiesi@0 1195
danielebarchiesi@0 1196 // Render the output using the template file.
danielebarchiesi@0 1197 $template_file = $info['template'] . $extension;
danielebarchiesi@0 1198 if (isset($info['path'])) {
danielebarchiesi@0 1199 $template_file = $info['path'] . '/' . $template_file;
danielebarchiesi@0 1200 }
danielebarchiesi@0 1201 $output = $render_function($template_file, $variables);
danielebarchiesi@0 1202 }
danielebarchiesi@0 1203
danielebarchiesi@0 1204 // restore path_to_theme()
danielebarchiesi@0 1205 $theme_path = $temp;
danielebarchiesi@0 1206 return $output;
danielebarchiesi@0 1207 }
danielebarchiesi@0 1208
danielebarchiesi@0 1209 /**
danielebarchiesi@0 1210 * Returns the path to the current themed element.
danielebarchiesi@0 1211 *
danielebarchiesi@0 1212 * It can point to the active theme or the module handling a themed
danielebarchiesi@0 1213 * implementation. For example, when invoked within the scope of a theming call
danielebarchiesi@0 1214 * it will depend on where the theming function is handled. If implemented from
danielebarchiesi@0 1215 * a module, it will point to the module. If implemented from the active theme,
danielebarchiesi@0 1216 * it will point to the active theme. When called outside the scope of a
danielebarchiesi@0 1217 * theming call, it will always point to the active theme.
danielebarchiesi@0 1218 */
danielebarchiesi@0 1219 function path_to_theme() {
danielebarchiesi@0 1220 global $theme_path;
danielebarchiesi@0 1221
danielebarchiesi@0 1222 if (!isset($theme_path)) {
danielebarchiesi@0 1223 drupal_theme_initialize();
danielebarchiesi@0 1224 }
danielebarchiesi@0 1225
danielebarchiesi@0 1226 return $theme_path;
danielebarchiesi@0 1227 }
danielebarchiesi@0 1228
danielebarchiesi@0 1229 /**
danielebarchiesi@0 1230 * Allows themes and/or theme engines to discover overridden theme functions.
danielebarchiesi@0 1231 *
danielebarchiesi@0 1232 * @param $cache
danielebarchiesi@0 1233 * The existing cache of theme hooks to test against.
danielebarchiesi@0 1234 * @param $prefixes
danielebarchiesi@0 1235 * An array of prefixes to test, in reverse order of importance.
danielebarchiesi@0 1236 *
danielebarchiesi@0 1237 * @return $implementations
danielebarchiesi@0 1238 * The functions found, suitable for returning from hook_theme;
danielebarchiesi@0 1239 */
danielebarchiesi@0 1240 function drupal_find_theme_functions($cache, $prefixes) {
danielebarchiesi@0 1241 $implementations = array();
danielebarchiesi@0 1242 $functions = get_defined_functions();
danielebarchiesi@0 1243
danielebarchiesi@0 1244 foreach ($cache as $hook => $info) {
danielebarchiesi@0 1245 foreach ($prefixes as $prefix) {
danielebarchiesi@0 1246 // Find theme functions that implement possible "suggestion" variants of
danielebarchiesi@0 1247 // registered theme hooks and add those as new registered theme hooks.
danielebarchiesi@0 1248 // The 'pattern' key defines a common prefix that all suggestions must
danielebarchiesi@0 1249 // start with. The default is the name of the hook followed by '__'. An
danielebarchiesi@0 1250 // 'base hook' key is added to each entry made for a found suggestion,
danielebarchiesi@0 1251 // so that common functionality can be implemented for all suggestions of
danielebarchiesi@0 1252 // the same base hook. To keep things simple, deep hierarchy of
danielebarchiesi@0 1253 // suggestions is not supported: each suggestion's 'base hook' key
danielebarchiesi@0 1254 // refers to a base hook, not to another suggestion, and all suggestions
danielebarchiesi@0 1255 // are found using the base hook's pattern, not a pattern from an
danielebarchiesi@0 1256 // intermediary suggestion.
danielebarchiesi@0 1257 $pattern = isset($info['pattern']) ? $info['pattern'] : ($hook . '__');
danielebarchiesi@0 1258 if (!isset($info['base hook']) && !empty($pattern)) {
danielebarchiesi@0 1259 $matches = preg_grep('/^' . $prefix . '_' . $pattern . '/', $functions['user']);
danielebarchiesi@0 1260 if ($matches) {
danielebarchiesi@0 1261 foreach ($matches as $match) {
danielebarchiesi@0 1262 $new_hook = substr($match, strlen($prefix) + 1);
danielebarchiesi@0 1263 $arg_name = isset($info['variables']) ? 'variables' : 'render element';
danielebarchiesi@0 1264 $implementations[$new_hook] = array(
danielebarchiesi@0 1265 'function' => $match,
danielebarchiesi@0 1266 $arg_name => $info[$arg_name],
danielebarchiesi@0 1267 'base hook' => $hook,
danielebarchiesi@0 1268 );
danielebarchiesi@0 1269 }
danielebarchiesi@0 1270 }
danielebarchiesi@0 1271 }
danielebarchiesi@0 1272 // Find theme functions that implement registered theme hooks and include
danielebarchiesi@0 1273 // that in what is returned so that the registry knows that the theme has
danielebarchiesi@0 1274 // this implementation.
danielebarchiesi@0 1275 if (function_exists($prefix . '_' . $hook)) {
danielebarchiesi@0 1276 $implementations[$hook] = array(
danielebarchiesi@0 1277 'function' => $prefix . '_' . $hook,
danielebarchiesi@0 1278 );
danielebarchiesi@0 1279 }
danielebarchiesi@0 1280 }
danielebarchiesi@0 1281 }
danielebarchiesi@0 1282
danielebarchiesi@0 1283 return $implementations;
danielebarchiesi@0 1284 }
danielebarchiesi@0 1285
danielebarchiesi@0 1286 /**
danielebarchiesi@0 1287 * Allows themes and/or theme engines to easily discover overridden templates.
danielebarchiesi@0 1288 *
danielebarchiesi@0 1289 * @param $cache
danielebarchiesi@0 1290 * The existing cache of theme hooks to test against.
danielebarchiesi@0 1291 * @param $extension
danielebarchiesi@0 1292 * The extension that these templates will have.
danielebarchiesi@0 1293 * @param $path
danielebarchiesi@0 1294 * The path to search.
danielebarchiesi@0 1295 */
danielebarchiesi@0 1296 function drupal_find_theme_templates($cache, $extension, $path) {
danielebarchiesi@0 1297 $implementations = array();
danielebarchiesi@0 1298
danielebarchiesi@0 1299 // Collect paths to all sub-themes grouped by base themes. These will be
danielebarchiesi@0 1300 // used for filtering. This allows base themes to have sub-themes in its
danielebarchiesi@0 1301 // folder hierarchy without affecting the base themes template discovery.
danielebarchiesi@0 1302 $theme_paths = array();
danielebarchiesi@0 1303 foreach (list_themes() as $theme_info) {
danielebarchiesi@0 1304 if (!empty($theme_info->base_theme)) {
danielebarchiesi@0 1305 $theme_paths[$theme_info->base_theme][$theme_info->name] = dirname($theme_info->filename);
danielebarchiesi@0 1306 }
danielebarchiesi@0 1307 }
danielebarchiesi@0 1308 foreach ($theme_paths as $basetheme => $subthemes) {
danielebarchiesi@0 1309 foreach ($subthemes as $subtheme => $subtheme_path) {
danielebarchiesi@0 1310 if (isset($theme_paths[$subtheme])) {
danielebarchiesi@0 1311 $theme_paths[$basetheme] = array_merge($theme_paths[$basetheme], $theme_paths[$subtheme]);
danielebarchiesi@0 1312 }
danielebarchiesi@0 1313 }
danielebarchiesi@0 1314 }
danielebarchiesi@0 1315 global $theme;
danielebarchiesi@0 1316 $subtheme_paths = isset($theme_paths[$theme]) ? $theme_paths[$theme] : array();
danielebarchiesi@0 1317
danielebarchiesi@0 1318 // Escape the periods in the extension.
danielebarchiesi@0 1319 $regex = '/' . str_replace('.', '\.', $extension) . '$/';
danielebarchiesi@0 1320 // Get a listing of all template files in the path to search.
danielebarchiesi@0 1321 $files = drupal_system_listing($regex, $path, 'name', 0);
danielebarchiesi@0 1322
danielebarchiesi@0 1323 // Find templates that implement registered theme hooks and include that in
danielebarchiesi@0 1324 // what is returned so that the registry knows that the theme has this
danielebarchiesi@0 1325 // implementation.
danielebarchiesi@0 1326 foreach ($files as $template => $file) {
danielebarchiesi@0 1327 // Ignore sub-theme templates for the current theme.
danielebarchiesi@0 1328 if (strpos($file->uri, str_replace($subtheme_paths, '', $file->uri)) !== 0) {
danielebarchiesi@0 1329 continue;
danielebarchiesi@0 1330 }
danielebarchiesi@0 1331 // Chop off the remaining extensions if there are any. $template already
danielebarchiesi@0 1332 // has the rightmost extension removed, but there might still be more,
danielebarchiesi@0 1333 // such as with .tpl.php, which still has .tpl in $template at this point.
danielebarchiesi@0 1334 if (($pos = strpos($template, '.')) !== FALSE) {
danielebarchiesi@0 1335 $template = substr($template, 0, $pos);
danielebarchiesi@0 1336 }
danielebarchiesi@0 1337 // Transform - in filenames to _ to match function naming scheme
danielebarchiesi@0 1338 // for the purposes of searching.
danielebarchiesi@0 1339 $hook = strtr($template, '-', '_');
danielebarchiesi@0 1340 if (isset($cache[$hook])) {
danielebarchiesi@0 1341 $implementations[$hook] = array(
danielebarchiesi@0 1342 'template' => $template,
danielebarchiesi@0 1343 'path' => dirname($file->uri),
danielebarchiesi@0 1344 );
danielebarchiesi@0 1345 }
danielebarchiesi@0 1346 }
danielebarchiesi@0 1347
danielebarchiesi@0 1348 // Find templates that implement possible "suggestion" variants of registered
danielebarchiesi@0 1349 // theme hooks and add those as new registered theme hooks. See
danielebarchiesi@0 1350 // drupal_find_theme_functions() for more information about suggestions and
danielebarchiesi@0 1351 // the use of 'pattern' and 'base hook'.
danielebarchiesi@0 1352 $patterns = array_keys($files);
danielebarchiesi@0 1353 foreach ($cache as $hook => $info) {
danielebarchiesi@0 1354 $pattern = isset($info['pattern']) ? $info['pattern'] : ($hook . '__');
danielebarchiesi@0 1355 if (!isset($info['base hook']) && !empty($pattern)) {
danielebarchiesi@0 1356 // Transform _ in pattern to - to match file naming scheme
danielebarchiesi@0 1357 // for the purposes of searching.
danielebarchiesi@0 1358 $pattern = strtr($pattern, '_', '-');
danielebarchiesi@0 1359
danielebarchiesi@0 1360 $matches = preg_grep('/^' . $pattern . '/', $patterns);
danielebarchiesi@0 1361 if ($matches) {
danielebarchiesi@0 1362 foreach ($matches as $match) {
danielebarchiesi@0 1363 $file = substr($match, 0, strpos($match, '.'));
danielebarchiesi@0 1364 // Put the underscores back in for the hook name and register this
danielebarchiesi@0 1365 // pattern.
danielebarchiesi@0 1366 $arg_name = isset($info['variables']) ? 'variables' : 'render element';
danielebarchiesi@0 1367 $implementations[strtr($file, '-', '_')] = array(
danielebarchiesi@0 1368 'template' => $file,
danielebarchiesi@0 1369 'path' => dirname($files[$match]->uri),
danielebarchiesi@0 1370 $arg_name => $info[$arg_name],
danielebarchiesi@0 1371 'base hook' => $hook,
danielebarchiesi@0 1372 );
danielebarchiesi@0 1373 }
danielebarchiesi@0 1374 }
danielebarchiesi@0 1375 }
danielebarchiesi@0 1376 }
danielebarchiesi@0 1377 return $implementations;
danielebarchiesi@0 1378 }
danielebarchiesi@0 1379
danielebarchiesi@0 1380 /**
danielebarchiesi@0 1381 * Retrieves a setting for the current theme or for a given theme.
danielebarchiesi@0 1382 *
danielebarchiesi@0 1383 * The final setting is obtained from the last value found in the following
danielebarchiesi@0 1384 * sources:
danielebarchiesi@0 1385 * - the default global settings specified in this function
danielebarchiesi@0 1386 * - the default theme-specific settings defined in any base theme's .info file
danielebarchiesi@0 1387 * - the default theme-specific settings defined in the theme's .info file
danielebarchiesi@0 1388 * - the saved values from the global theme settings form
danielebarchiesi@0 1389 * - the saved values from the theme's settings form
danielebarchiesi@0 1390 * To only retrieve the default global theme setting, an empty string should be
danielebarchiesi@0 1391 * given for $theme.
danielebarchiesi@0 1392 *
danielebarchiesi@0 1393 * @param $setting_name
danielebarchiesi@0 1394 * The name of the setting to be retrieved.
danielebarchiesi@0 1395 * @param $theme
danielebarchiesi@0 1396 * The name of a given theme; defaults to the current theme.
danielebarchiesi@0 1397 *
danielebarchiesi@0 1398 * @return
danielebarchiesi@0 1399 * The value of the requested setting, NULL if the setting does not exist.
danielebarchiesi@0 1400 */
danielebarchiesi@0 1401 function theme_get_setting($setting_name, $theme = NULL) {
danielebarchiesi@0 1402 $cache = &drupal_static(__FUNCTION__, array());
danielebarchiesi@0 1403
danielebarchiesi@0 1404 // If no key is given, use the current theme if we can determine it.
danielebarchiesi@0 1405 if (!isset($theme)) {
danielebarchiesi@0 1406 $theme = !empty($GLOBALS['theme_key']) ? $GLOBALS['theme_key'] : '';
danielebarchiesi@0 1407 }
danielebarchiesi@0 1408
danielebarchiesi@0 1409 if (empty($cache[$theme])) {
danielebarchiesi@0 1410 // Set the default values for each global setting.
danielebarchiesi@0 1411 // To add new global settings, add their default values below, and then
danielebarchiesi@0 1412 // add form elements to system_theme_settings() in system.admin.inc.
danielebarchiesi@0 1413 $cache[$theme] = array(
danielebarchiesi@0 1414 'default_logo' => 1,
danielebarchiesi@0 1415 'logo_path' => '',
danielebarchiesi@0 1416 'default_favicon' => 1,
danielebarchiesi@0 1417 'favicon_path' => '',
danielebarchiesi@0 1418 // Use the IANA-registered MIME type for ICO files as default.
danielebarchiesi@0 1419 'favicon_mimetype' => 'image/vnd.microsoft.icon',
danielebarchiesi@0 1420 );
danielebarchiesi@0 1421 // Turn on all default features.
danielebarchiesi@0 1422 $features = _system_default_theme_features();
danielebarchiesi@0 1423 foreach ($features as $feature) {
danielebarchiesi@0 1424 $cache[$theme]['toggle_' . $feature] = 1;
danielebarchiesi@0 1425 }
danielebarchiesi@0 1426
danielebarchiesi@0 1427 // Get the values for the theme-specific settings from the .info files of
danielebarchiesi@0 1428 // the theme and all its base themes.
danielebarchiesi@0 1429 if ($theme) {
danielebarchiesi@0 1430 $themes = list_themes();
danielebarchiesi@0 1431 $theme_object = $themes[$theme];
danielebarchiesi@0 1432
danielebarchiesi@0 1433 // Create a list which includes the current theme and all its base themes.
danielebarchiesi@0 1434 if (isset($theme_object->base_themes)) {
danielebarchiesi@0 1435 $theme_keys = array_keys($theme_object->base_themes);
danielebarchiesi@0 1436 $theme_keys[] = $theme;
danielebarchiesi@0 1437 }
danielebarchiesi@0 1438 else {
danielebarchiesi@0 1439 $theme_keys = array($theme);
danielebarchiesi@0 1440 }
danielebarchiesi@0 1441 foreach ($theme_keys as $theme_key) {
danielebarchiesi@0 1442 if (!empty($themes[$theme_key]->info['settings'])) {
danielebarchiesi@0 1443 $cache[$theme] = array_merge($cache[$theme], $themes[$theme_key]->info['settings']);
danielebarchiesi@0 1444 }
danielebarchiesi@0 1445 }
danielebarchiesi@0 1446 }
danielebarchiesi@0 1447
danielebarchiesi@0 1448 // Get the saved global settings from the database.
danielebarchiesi@0 1449 $cache[$theme] = array_merge($cache[$theme], variable_get('theme_settings', array()));
danielebarchiesi@0 1450
danielebarchiesi@0 1451 if ($theme) {
danielebarchiesi@0 1452 // Get the saved theme-specific settings from the database.
danielebarchiesi@0 1453 $cache[$theme] = array_merge($cache[$theme], variable_get('theme_' . $theme . '_settings', array()));
danielebarchiesi@0 1454
danielebarchiesi@0 1455 // If the theme does not support a particular feature, override the global
danielebarchiesi@0 1456 // setting and set the value to NULL.
danielebarchiesi@0 1457 if (!empty($theme_object->info['features'])) {
danielebarchiesi@0 1458 foreach ($features as $feature) {
danielebarchiesi@0 1459 if (!in_array($feature, $theme_object->info['features'])) {
danielebarchiesi@0 1460 $cache[$theme]['toggle_' . $feature] = NULL;
danielebarchiesi@0 1461 }
danielebarchiesi@0 1462 }
danielebarchiesi@0 1463 }
danielebarchiesi@0 1464
danielebarchiesi@0 1465 // Generate the path to the logo image.
danielebarchiesi@0 1466 if ($cache[$theme]['toggle_logo']) {
danielebarchiesi@0 1467 if ($cache[$theme]['default_logo']) {
danielebarchiesi@0 1468 $cache[$theme]['logo'] = file_create_url(dirname($theme_object->filename) . '/logo.png');
danielebarchiesi@0 1469 }
danielebarchiesi@0 1470 elseif ($cache[$theme]['logo_path']) {
danielebarchiesi@0 1471 $cache[$theme]['logo'] = file_create_url($cache[$theme]['logo_path']);
danielebarchiesi@0 1472 }
danielebarchiesi@0 1473 }
danielebarchiesi@0 1474
danielebarchiesi@0 1475 // Generate the path to the favicon.
danielebarchiesi@0 1476 if ($cache[$theme]['toggle_favicon']) {
danielebarchiesi@0 1477 if ($cache[$theme]['default_favicon']) {
danielebarchiesi@0 1478 if (file_exists($favicon = dirname($theme_object->filename) . '/favicon.ico')) {
danielebarchiesi@0 1479 $cache[$theme]['favicon'] = file_create_url($favicon);
danielebarchiesi@0 1480 }
danielebarchiesi@0 1481 else {
danielebarchiesi@0 1482 $cache[$theme]['favicon'] = file_create_url('misc/favicon.ico');
danielebarchiesi@0 1483 }
danielebarchiesi@0 1484 }
danielebarchiesi@0 1485 elseif ($cache[$theme]['favicon_path']) {
danielebarchiesi@0 1486 $cache[$theme]['favicon'] = file_create_url($cache[$theme]['favicon_path']);
danielebarchiesi@0 1487 }
danielebarchiesi@0 1488 else {
danielebarchiesi@0 1489 $cache[$theme]['toggle_favicon'] = FALSE;
danielebarchiesi@0 1490 }
danielebarchiesi@0 1491 }
danielebarchiesi@0 1492 }
danielebarchiesi@0 1493 }
danielebarchiesi@0 1494
danielebarchiesi@0 1495 return isset($cache[$theme][$setting_name]) ? $cache[$theme][$setting_name] : NULL;
danielebarchiesi@0 1496 }
danielebarchiesi@0 1497
danielebarchiesi@0 1498 /**
danielebarchiesi@0 1499 * Renders a system default template, which is essentially a PHP template.
danielebarchiesi@0 1500 *
danielebarchiesi@0 1501 * @param $template_file
danielebarchiesi@0 1502 * The filename of the template to render.
danielebarchiesi@0 1503 * @param $variables
danielebarchiesi@0 1504 * A keyed array of variables that will appear in the output.
danielebarchiesi@0 1505 *
danielebarchiesi@0 1506 * @return
danielebarchiesi@0 1507 * The output generated by the template.
danielebarchiesi@0 1508 */
danielebarchiesi@0 1509 function theme_render_template($template_file, $variables) {
danielebarchiesi@0 1510 // Extract the variables to a local namespace
danielebarchiesi@0 1511 extract($variables, EXTR_SKIP);
danielebarchiesi@0 1512
danielebarchiesi@0 1513 // Start output buffering
danielebarchiesi@0 1514 ob_start();
danielebarchiesi@0 1515
danielebarchiesi@0 1516 // Include the template file
danielebarchiesi@0 1517 include DRUPAL_ROOT . '/' . $template_file;
danielebarchiesi@0 1518
danielebarchiesi@0 1519 // End buffering and return its contents
danielebarchiesi@0 1520 return ob_get_clean();
danielebarchiesi@0 1521 }
danielebarchiesi@0 1522
danielebarchiesi@0 1523 /**
danielebarchiesi@0 1524 * Enables a given list of themes.
danielebarchiesi@0 1525 *
danielebarchiesi@0 1526 * @param $theme_list
danielebarchiesi@0 1527 * An array of theme names.
danielebarchiesi@0 1528 */
danielebarchiesi@0 1529 function theme_enable($theme_list) {
danielebarchiesi@0 1530 drupal_clear_css_cache();
danielebarchiesi@0 1531
danielebarchiesi@0 1532 foreach ($theme_list as $key) {
danielebarchiesi@0 1533 db_update('system')
danielebarchiesi@0 1534 ->fields(array('status' => 1))
danielebarchiesi@0 1535 ->condition('type', 'theme')
danielebarchiesi@0 1536 ->condition('name', $key)
danielebarchiesi@0 1537 ->execute();
danielebarchiesi@0 1538 }
danielebarchiesi@0 1539
danielebarchiesi@0 1540 list_themes(TRUE);
danielebarchiesi@0 1541 menu_rebuild();
danielebarchiesi@0 1542 drupal_theme_rebuild();
danielebarchiesi@0 1543
danielebarchiesi@0 1544 // Invoke hook_themes_enabled() after the themes have been enabled.
danielebarchiesi@0 1545 module_invoke_all('themes_enabled', $theme_list);
danielebarchiesi@0 1546 }
danielebarchiesi@0 1547
danielebarchiesi@0 1548 /**
danielebarchiesi@0 1549 * Disables a given list of themes.
danielebarchiesi@0 1550 *
danielebarchiesi@0 1551 * @param $theme_list
danielebarchiesi@0 1552 * An array of theme names.
danielebarchiesi@0 1553 */
danielebarchiesi@0 1554 function theme_disable($theme_list) {
danielebarchiesi@0 1555 // Don't disable the default theme.
danielebarchiesi@0 1556 if ($pos = array_search(variable_get('theme_default', 'bartik'), $theme_list) !== FALSE) {
danielebarchiesi@0 1557 unset($theme_list[$pos]);
danielebarchiesi@0 1558 if (empty($theme_list)) {
danielebarchiesi@0 1559 return;
danielebarchiesi@0 1560 }
danielebarchiesi@0 1561 }
danielebarchiesi@0 1562
danielebarchiesi@0 1563 drupal_clear_css_cache();
danielebarchiesi@0 1564
danielebarchiesi@0 1565 foreach ($theme_list as $key) {
danielebarchiesi@0 1566 db_update('system')
danielebarchiesi@0 1567 ->fields(array('status' => 0))
danielebarchiesi@0 1568 ->condition('type', 'theme')
danielebarchiesi@0 1569 ->condition('name', $key)
danielebarchiesi@0 1570 ->execute();
danielebarchiesi@0 1571 }
danielebarchiesi@0 1572
danielebarchiesi@0 1573 list_themes(TRUE);
danielebarchiesi@0 1574 menu_rebuild();
danielebarchiesi@0 1575 drupal_theme_rebuild();
danielebarchiesi@0 1576
danielebarchiesi@0 1577 // Invoke hook_themes_disabled after the themes have been disabled.
danielebarchiesi@0 1578 module_invoke_all('themes_disabled', $theme_list);
danielebarchiesi@0 1579 }
danielebarchiesi@0 1580
danielebarchiesi@0 1581 /**
danielebarchiesi@0 1582 * @addtogroup themeable
danielebarchiesi@0 1583 * @{
danielebarchiesi@0 1584 */
danielebarchiesi@0 1585
danielebarchiesi@0 1586 /**
danielebarchiesi@0 1587 * Returns HTML for status and/or error messages, grouped by type.
danielebarchiesi@0 1588 *
danielebarchiesi@0 1589 * An invisible heading identifies the messages for assistive technology.
danielebarchiesi@0 1590 * Sighted users see a colored box. See http://www.w3.org/TR/WCAG-TECHS/H69.html
danielebarchiesi@0 1591 * for info.
danielebarchiesi@0 1592 *
danielebarchiesi@0 1593 * @param $variables
danielebarchiesi@0 1594 * An associative array containing:
danielebarchiesi@0 1595 * - display: (optional) Set to 'status' or 'error' to display only messages
danielebarchiesi@0 1596 * of that type.
danielebarchiesi@0 1597 */
danielebarchiesi@0 1598 function theme_status_messages($variables) {
danielebarchiesi@0 1599 $display = $variables['display'];
danielebarchiesi@0 1600 $output = '';
danielebarchiesi@0 1601
danielebarchiesi@0 1602 $status_heading = array(
danielebarchiesi@0 1603 'status' => t('Status message'),
danielebarchiesi@0 1604 'error' => t('Error message'),
danielebarchiesi@0 1605 'warning' => t('Warning message'),
danielebarchiesi@0 1606 );
danielebarchiesi@0 1607 foreach (drupal_get_messages($display) as $type => $messages) {
danielebarchiesi@0 1608 $output .= "<div class=\"messages $type\">\n";
danielebarchiesi@0 1609 if (!empty($status_heading[$type])) {
danielebarchiesi@0 1610 $output .= '<h2 class="element-invisible">' . $status_heading[$type] . "</h2>\n";
danielebarchiesi@0 1611 }
danielebarchiesi@0 1612 if (count($messages) > 1) {
danielebarchiesi@0 1613 $output .= " <ul>\n";
danielebarchiesi@0 1614 foreach ($messages as $message) {
danielebarchiesi@0 1615 $output .= ' <li>' . $message . "</li>\n";
danielebarchiesi@0 1616 }
danielebarchiesi@0 1617 $output .= " </ul>\n";
danielebarchiesi@0 1618 }
danielebarchiesi@0 1619 else {
danielebarchiesi@0 1620 $output .= $messages[0];
danielebarchiesi@0 1621 }
danielebarchiesi@0 1622 $output .= "</div>\n";
danielebarchiesi@0 1623 }
danielebarchiesi@0 1624 return $output;
danielebarchiesi@0 1625 }
danielebarchiesi@0 1626
danielebarchiesi@0 1627 /**
danielebarchiesi@0 1628 * Returns HTML for a link.
danielebarchiesi@0 1629 *
danielebarchiesi@0 1630 * All Drupal code that outputs a link should call the l() function. That
danielebarchiesi@0 1631 * function performs some initial preprocessing, and then, if necessary, calls
danielebarchiesi@0 1632 * theme('link') for rendering the anchor tag.
danielebarchiesi@0 1633 *
danielebarchiesi@0 1634 * To optimize performance for sites that don't need custom theming of links,
danielebarchiesi@0 1635 * the l() function includes an inline copy of this function, and uses that
danielebarchiesi@0 1636 * copy if none of the enabled modules or the active theme implement any
danielebarchiesi@0 1637 * preprocess or process functions or override this theme implementation.
danielebarchiesi@0 1638 *
danielebarchiesi@0 1639 * @param $variables
danielebarchiesi@0 1640 * An associative array containing the keys 'text', 'path', and 'options'.
danielebarchiesi@0 1641 * See the l() function for information about these variables.
danielebarchiesi@0 1642 *
danielebarchiesi@0 1643 * @see l()
danielebarchiesi@0 1644 */
danielebarchiesi@0 1645 function theme_link($variables) {
danielebarchiesi@0 1646 return '<a href="' . check_plain(url($variables['path'], $variables['options'])) . '"' . drupal_attributes($variables['options']['attributes']) . '>' . ($variables['options']['html'] ? $variables['text'] : check_plain($variables['text'])) . '</a>';
danielebarchiesi@0 1647 }
danielebarchiesi@0 1648
danielebarchiesi@0 1649 /**
danielebarchiesi@0 1650 * Returns HTML for a set of links.
danielebarchiesi@0 1651 *
danielebarchiesi@0 1652 * @param $variables
danielebarchiesi@0 1653 * An associative array containing:
danielebarchiesi@0 1654 * - links: An associative array of links to be themed. The key for each link
danielebarchiesi@0 1655 * is used as its CSS class. Each link should be itself an array, with the
danielebarchiesi@0 1656 * following elements:
danielebarchiesi@0 1657 * - title: The link text.
danielebarchiesi@0 1658 * - href: The link URL. If omitted, the 'title' is shown as a plain text
danielebarchiesi@0 1659 * item in the links list.
danielebarchiesi@0 1660 * - html: (optional) Whether or not 'title' is HTML. If set, the title
danielebarchiesi@0 1661 * will not be passed through check_plain().
danielebarchiesi@0 1662 * - attributes: (optional) Attributes for the anchor, or for the <span>
danielebarchiesi@0 1663 * tag used in its place if no 'href' is supplied. If element 'class' is
danielebarchiesi@0 1664 * included, it must be an array of one or more class names.
danielebarchiesi@0 1665 * If the 'href' element is supplied, the entire link array is passed to
danielebarchiesi@0 1666 * l() as its $options parameter.
danielebarchiesi@0 1667 * - attributes: A keyed array of attributes for the UL containing the
danielebarchiesi@0 1668 * list of links.
danielebarchiesi@0 1669 * - heading: (optional) A heading to precede the links. May be an
danielebarchiesi@0 1670 * associative array or a string. If it's an array, it can have the
danielebarchiesi@0 1671 * following elements:
danielebarchiesi@0 1672 * - text: The heading text.
danielebarchiesi@0 1673 * - level: The heading level (e.g. 'h2', 'h3').
danielebarchiesi@0 1674 * - class: (optional) An array of the CSS classes for the heading.
danielebarchiesi@0 1675 * When using a string it will be used as the text of the heading and the
danielebarchiesi@0 1676 * level will default to 'h2'. Headings should be used on navigation menus
danielebarchiesi@0 1677 * and any list of links that consistently appears on multiple pages. To
danielebarchiesi@0 1678 * make the heading invisible use the 'element-invisible' CSS class. Do not
danielebarchiesi@0 1679 * use 'display:none', which removes it from screen-readers and assistive
danielebarchiesi@0 1680 * technology. Headings allow screen-reader and keyboard only users to
danielebarchiesi@0 1681 * navigate to or skip the links. See
danielebarchiesi@0 1682 * http://juicystudio.com/article/screen-readers-display-none.php and
danielebarchiesi@0 1683 * http://www.w3.org/TR/WCAG-TECHS/H42.html for more information.
danielebarchiesi@0 1684 */
danielebarchiesi@0 1685 function theme_links($variables) {
danielebarchiesi@0 1686 $links = $variables['links'];
danielebarchiesi@0 1687 $attributes = $variables['attributes'];
danielebarchiesi@0 1688 $heading = $variables['heading'];
danielebarchiesi@0 1689 global $language_url;
danielebarchiesi@0 1690 $output = '';
danielebarchiesi@0 1691
danielebarchiesi@0 1692 if (count($links) > 0) {
danielebarchiesi@0 1693 $output = '';
danielebarchiesi@0 1694
danielebarchiesi@0 1695 // Treat the heading first if it is present to prepend it to the
danielebarchiesi@0 1696 // list of links.
danielebarchiesi@0 1697 if (!empty($heading)) {
danielebarchiesi@0 1698 if (is_string($heading)) {
danielebarchiesi@0 1699 // Prepare the array that will be used when the passed heading
danielebarchiesi@0 1700 // is a string.
danielebarchiesi@0 1701 $heading = array(
danielebarchiesi@0 1702 'text' => $heading,
danielebarchiesi@0 1703 // Set the default level of the heading.
danielebarchiesi@0 1704 'level' => 'h2',
danielebarchiesi@0 1705 );
danielebarchiesi@0 1706 }
danielebarchiesi@0 1707 $output .= '<' . $heading['level'];
danielebarchiesi@0 1708 if (!empty($heading['class'])) {
danielebarchiesi@0 1709 $output .= drupal_attributes(array('class' => $heading['class']));
danielebarchiesi@0 1710 }
danielebarchiesi@0 1711 $output .= '>' . check_plain($heading['text']) . '</' . $heading['level'] . '>';
danielebarchiesi@0 1712 }
danielebarchiesi@0 1713
danielebarchiesi@0 1714 $output .= '<ul' . drupal_attributes($attributes) . '>';
danielebarchiesi@0 1715
danielebarchiesi@0 1716 $num_links = count($links);
danielebarchiesi@0 1717 $i = 1;
danielebarchiesi@0 1718
danielebarchiesi@0 1719 foreach ($links as $key => $link) {
danielebarchiesi@0 1720 $class = array($key);
danielebarchiesi@0 1721
danielebarchiesi@0 1722 // Add first, last and active classes to the list of links to help out themers.
danielebarchiesi@0 1723 if ($i == 1) {
danielebarchiesi@0 1724 $class[] = 'first';
danielebarchiesi@0 1725 }
danielebarchiesi@0 1726 if ($i == $num_links) {
danielebarchiesi@0 1727 $class[] = 'last';
danielebarchiesi@0 1728 }
danielebarchiesi@0 1729 if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
danielebarchiesi@0 1730 && (empty($link['language']) || $link['language']->language == $language_url->language)) {
danielebarchiesi@0 1731 $class[] = 'active';
danielebarchiesi@0 1732 }
danielebarchiesi@0 1733 $output .= '<li' . drupal_attributes(array('class' => $class)) . '>';
danielebarchiesi@0 1734
danielebarchiesi@0 1735 if (isset($link['href'])) {
danielebarchiesi@0 1736 // Pass in $link as $options, they share the same keys.
danielebarchiesi@0 1737 $output .= l($link['title'], $link['href'], $link);
danielebarchiesi@0 1738 }
danielebarchiesi@0 1739 elseif (!empty($link['title'])) {
danielebarchiesi@0 1740 // Some links are actually not links, but we wrap these in <span> for adding title and class attributes.
danielebarchiesi@0 1741 if (empty($link['html'])) {
danielebarchiesi@0 1742 $link['title'] = check_plain($link['title']);
danielebarchiesi@0 1743 }
danielebarchiesi@0 1744 $span_attributes = '';
danielebarchiesi@0 1745 if (isset($link['attributes'])) {
danielebarchiesi@0 1746 $span_attributes = drupal_attributes($link['attributes']);
danielebarchiesi@0 1747 }
danielebarchiesi@0 1748 $output .= '<span' . $span_attributes . '>' . $link['title'] . '</span>';
danielebarchiesi@0 1749 }
danielebarchiesi@0 1750
danielebarchiesi@0 1751 $i++;
danielebarchiesi@0 1752 $output .= "</li>\n";
danielebarchiesi@0 1753 }
danielebarchiesi@0 1754
danielebarchiesi@0 1755 $output .= '</ul>';
danielebarchiesi@0 1756 }
danielebarchiesi@0 1757
danielebarchiesi@0 1758 return $output;
danielebarchiesi@0 1759 }
danielebarchiesi@0 1760
danielebarchiesi@0 1761 /**
danielebarchiesi@0 1762 * Returns HTML for an image.
danielebarchiesi@0 1763 *
danielebarchiesi@0 1764 * @param $variables
danielebarchiesi@0 1765 * An associative array containing:
danielebarchiesi@0 1766 * - path: Either the path of the image file (relative to base_path()) or a
danielebarchiesi@0 1767 * full URL.
danielebarchiesi@0 1768 * - width: The width of the image (if known).
danielebarchiesi@0 1769 * - height: The height of the image (if known).
danielebarchiesi@0 1770 * - alt: The alternative text for text-based browsers. HTML 4 and XHTML 1.0
danielebarchiesi@0 1771 * always require an alt attribute. The HTML 5 draft allows the alt
danielebarchiesi@0 1772 * attribute to be omitted in some cases. Therefore, this variable defaults
danielebarchiesi@0 1773 * to an empty string, but can be set to NULL for the attribute to be
danielebarchiesi@0 1774 * omitted. Usually, neither omission nor an empty string satisfies
danielebarchiesi@0 1775 * accessibility requirements, so it is strongly encouraged for code
danielebarchiesi@0 1776 * calling theme('image') to pass a meaningful value for this variable.
danielebarchiesi@0 1777 * - http://www.w3.org/TR/REC-html40/struct/objects.html#h-13.8
danielebarchiesi@0 1778 * - http://www.w3.org/TR/xhtml1/dtds.html
danielebarchiesi@0 1779 * - http://dev.w3.org/html5/spec/Overview.html#alt
danielebarchiesi@0 1780 * - title: The title text is displayed when the image is hovered in some
danielebarchiesi@0 1781 * popular browsers.
danielebarchiesi@0 1782 * - attributes: Associative array of attributes to be placed in the img tag.
danielebarchiesi@0 1783 */
danielebarchiesi@0 1784 function theme_image($variables) {
danielebarchiesi@0 1785 $attributes = $variables['attributes'];
danielebarchiesi@0 1786 $attributes['src'] = file_create_url($variables['path']);
danielebarchiesi@0 1787
danielebarchiesi@0 1788 foreach (array('width', 'height', 'alt', 'title') as $key) {
danielebarchiesi@0 1789
danielebarchiesi@0 1790 if (isset($variables[$key])) {
danielebarchiesi@0 1791 $attributes[$key] = $variables[$key];
danielebarchiesi@0 1792 }
danielebarchiesi@0 1793 }
danielebarchiesi@0 1794
danielebarchiesi@0 1795 return '<img' . drupal_attributes($attributes) . ' />';
danielebarchiesi@0 1796 }
danielebarchiesi@0 1797
danielebarchiesi@0 1798 /**
danielebarchiesi@0 1799 * Returns HTML for a breadcrumb trail.
danielebarchiesi@0 1800 *
danielebarchiesi@0 1801 * @param $variables
danielebarchiesi@0 1802 * An associative array containing:
danielebarchiesi@0 1803 * - breadcrumb: An array containing the breadcrumb links.
danielebarchiesi@0 1804 */
danielebarchiesi@0 1805 function theme_breadcrumb($variables) {
danielebarchiesi@0 1806 $breadcrumb = $variables['breadcrumb'];
danielebarchiesi@0 1807
danielebarchiesi@0 1808 if (!empty($breadcrumb)) {
danielebarchiesi@0 1809 // Provide a navigational heading to give context for breadcrumb links to
danielebarchiesi@0 1810 // screen-reader users. Make the heading invisible with .element-invisible.
danielebarchiesi@0 1811 $output = '<h2 class="element-invisible">' . t('You are here') . '</h2>';
danielebarchiesi@0 1812
danielebarchiesi@0 1813 $output .= '<div class="breadcrumb">' . implode(' » ', $breadcrumb) . '</div>';
danielebarchiesi@0 1814 return $output;
danielebarchiesi@0 1815 }
danielebarchiesi@0 1816 }
danielebarchiesi@0 1817
danielebarchiesi@0 1818 /**
danielebarchiesi@0 1819 * Returns HTML for a table.
danielebarchiesi@0 1820 *
danielebarchiesi@0 1821 * @param $variables
danielebarchiesi@0 1822 * An associative array containing:
danielebarchiesi@0 1823 * - header: An array containing the table headers. Each element of the array
danielebarchiesi@0 1824 * can be either a localized string or an associative array with the
danielebarchiesi@0 1825 * following keys:
danielebarchiesi@0 1826 * - "data": The localized title of the table column.
danielebarchiesi@0 1827 * - "field": The database field represented in the table column (required
danielebarchiesi@0 1828 * if user is to be able to sort on this column).
danielebarchiesi@0 1829 * - "sort": A default sort order for this column ("asc" or "desc"). Only
danielebarchiesi@0 1830 * one column should be given a default sort order because table sorting
danielebarchiesi@0 1831 * only applies to one column at a time.
danielebarchiesi@0 1832 * - Any HTML attributes, such as "colspan", to apply to the column header
danielebarchiesi@0 1833 * cell.
danielebarchiesi@0 1834 * - rows: An array of table rows. Every row is an array of cells, or an
danielebarchiesi@0 1835 * associative array with the following keys:
danielebarchiesi@0 1836 * - "data": an array of cells
danielebarchiesi@0 1837 * - Any HTML attributes, such as "class", to apply to the table row.
danielebarchiesi@0 1838 * - "no_striping": a boolean indicating that the row should receive no
danielebarchiesi@0 1839 * 'even / odd' styling. Defaults to FALSE.
danielebarchiesi@0 1840 * Each cell can be either a string or an associative array with the
danielebarchiesi@0 1841 * following keys:
danielebarchiesi@0 1842 * - "data": The string to display in the table cell.
danielebarchiesi@0 1843 * - "header": Indicates this cell is a header.
danielebarchiesi@0 1844 * - Any HTML attributes, such as "colspan", to apply to the table cell.
danielebarchiesi@0 1845 * Here's an example for $rows:
danielebarchiesi@0 1846 * @code
danielebarchiesi@0 1847 * $rows = array(
danielebarchiesi@0 1848 * // Simple row
danielebarchiesi@0 1849 * array(
danielebarchiesi@0 1850 * 'Cell 1', 'Cell 2', 'Cell 3'
danielebarchiesi@0 1851 * ),
danielebarchiesi@0 1852 * // Row with attributes on the row and some of its cells.
danielebarchiesi@0 1853 * array(
danielebarchiesi@0 1854 * 'data' => array('Cell 1', array('data' => 'Cell 2', 'colspan' => 2)), 'class' => array('funky')
danielebarchiesi@0 1855 * )
danielebarchiesi@0 1856 * );
danielebarchiesi@0 1857 * @endcode
danielebarchiesi@0 1858 * - attributes: An array of HTML attributes to apply to the table tag.
danielebarchiesi@0 1859 * - caption: A localized string to use for the <caption> tag.
danielebarchiesi@0 1860 * - colgroups: An array of column groups. Each element of the array can be
danielebarchiesi@0 1861 * either:
danielebarchiesi@0 1862 * - An array of columns, each of which is an associative array of HTML
danielebarchiesi@0 1863 * attributes applied to the COL element.
danielebarchiesi@0 1864 * - An array of attributes applied to the COLGROUP element, which must
danielebarchiesi@0 1865 * include a "data" attribute. To add attributes to COL elements, set the
danielebarchiesi@0 1866 * "data" attribute with an array of columns, each of which is an
danielebarchiesi@0 1867 * associative array of HTML attributes.
danielebarchiesi@0 1868 * Here's an example for $colgroup:
danielebarchiesi@0 1869 * @code
danielebarchiesi@0 1870 * $colgroup = array(
danielebarchiesi@0 1871 * // COLGROUP with one COL element.
danielebarchiesi@0 1872 * array(
danielebarchiesi@0 1873 * array(
danielebarchiesi@0 1874 * 'class' => array('funky'), // Attribute for the COL element.
danielebarchiesi@0 1875 * ),
danielebarchiesi@0 1876 * ),
danielebarchiesi@0 1877 * // Colgroup with attributes and inner COL elements.
danielebarchiesi@0 1878 * array(
danielebarchiesi@0 1879 * 'data' => array(
danielebarchiesi@0 1880 * array(
danielebarchiesi@0 1881 * 'class' => array('funky'), // Attribute for the COL element.
danielebarchiesi@0 1882 * ),
danielebarchiesi@0 1883 * ),
danielebarchiesi@0 1884 * 'class' => array('jazzy'), // Attribute for the COLGROUP element.
danielebarchiesi@0 1885 * ),
danielebarchiesi@0 1886 * );
danielebarchiesi@0 1887 * @endcode
danielebarchiesi@0 1888 * These optional tags are used to group and set properties on columns
danielebarchiesi@0 1889 * within a table. For example, one may easily group three columns and
danielebarchiesi@0 1890 * apply same background style to all.
danielebarchiesi@0 1891 * - sticky: Use a "sticky" table header.
danielebarchiesi@0 1892 * - empty: The message to display in an extra row if table does not have any
danielebarchiesi@0 1893 * rows.
danielebarchiesi@0 1894 */
danielebarchiesi@0 1895 function theme_table($variables) {
danielebarchiesi@0 1896 $header = $variables['header'];
danielebarchiesi@0 1897 $rows = $variables['rows'];
danielebarchiesi@0 1898 $attributes = $variables['attributes'];
danielebarchiesi@0 1899 $caption = $variables['caption'];
danielebarchiesi@0 1900 $colgroups = $variables['colgroups'];
danielebarchiesi@0 1901 $sticky = $variables['sticky'];
danielebarchiesi@0 1902 $empty = $variables['empty'];
danielebarchiesi@0 1903
danielebarchiesi@0 1904 // Add sticky headers, if applicable.
danielebarchiesi@0 1905 if (count($header) && $sticky) {
danielebarchiesi@0 1906 drupal_add_js('misc/tableheader.js');
danielebarchiesi@0 1907 // Add 'sticky-enabled' class to the table to identify it for JS.
danielebarchiesi@0 1908 // This is needed to target tables constructed by this function.
danielebarchiesi@0 1909 $attributes['class'][] = 'sticky-enabled';
danielebarchiesi@0 1910 }
danielebarchiesi@0 1911
danielebarchiesi@0 1912 $output = '<table' . drupal_attributes($attributes) . ">\n";
danielebarchiesi@0 1913
danielebarchiesi@0 1914 if (isset($caption)) {
danielebarchiesi@0 1915 $output .= '<caption>' . $caption . "</caption>\n";
danielebarchiesi@0 1916 }
danielebarchiesi@0 1917
danielebarchiesi@0 1918 // Format the table columns:
danielebarchiesi@0 1919 if (count($colgroups)) {
danielebarchiesi@0 1920 foreach ($colgroups as $number => $colgroup) {
danielebarchiesi@0 1921 $attributes = array();
danielebarchiesi@0 1922
danielebarchiesi@0 1923 // Check if we're dealing with a simple or complex column
danielebarchiesi@0 1924 if (isset($colgroup['data'])) {
danielebarchiesi@0 1925 foreach ($colgroup as $key => $value) {
danielebarchiesi@0 1926 if ($key == 'data') {
danielebarchiesi@0 1927 $cols = $value;
danielebarchiesi@0 1928 }
danielebarchiesi@0 1929 else {
danielebarchiesi@0 1930 $attributes[$key] = $value;
danielebarchiesi@0 1931 }
danielebarchiesi@0 1932 }
danielebarchiesi@0 1933 }
danielebarchiesi@0 1934 else {
danielebarchiesi@0 1935 $cols = $colgroup;
danielebarchiesi@0 1936 }
danielebarchiesi@0 1937
danielebarchiesi@0 1938 // Build colgroup
danielebarchiesi@0 1939 if (is_array($cols) && count($cols)) {
danielebarchiesi@0 1940 $output .= ' <colgroup' . drupal_attributes($attributes) . '>';
danielebarchiesi@0 1941 $i = 0;
danielebarchiesi@0 1942 foreach ($cols as $col) {
danielebarchiesi@0 1943 $output .= ' <col' . drupal_attributes($col) . ' />';
danielebarchiesi@0 1944 }
danielebarchiesi@0 1945 $output .= " </colgroup>\n";
danielebarchiesi@0 1946 }
danielebarchiesi@0 1947 else {
danielebarchiesi@0 1948 $output .= ' <colgroup' . drupal_attributes($attributes) . " />\n";
danielebarchiesi@0 1949 }
danielebarchiesi@0 1950 }
danielebarchiesi@0 1951 }
danielebarchiesi@0 1952
danielebarchiesi@0 1953 // Add the 'empty' row message if available.
danielebarchiesi@0 1954 if (!count($rows) && $empty) {
danielebarchiesi@0 1955 $header_count = 0;
danielebarchiesi@0 1956 foreach ($header as $header_cell) {
danielebarchiesi@0 1957 if (is_array($header_cell)) {
danielebarchiesi@0 1958 $header_count += isset($header_cell['colspan']) ? $header_cell['colspan'] : 1;
danielebarchiesi@0 1959 }
danielebarchiesi@0 1960 else {
danielebarchiesi@0 1961 $header_count++;
danielebarchiesi@0 1962 }
danielebarchiesi@0 1963 }
danielebarchiesi@0 1964 $rows[] = array(array('data' => $empty, 'colspan' => $header_count, 'class' => array('empty', 'message')));
danielebarchiesi@0 1965 }
danielebarchiesi@0 1966
danielebarchiesi@0 1967 // Format the table header:
danielebarchiesi@0 1968 if (count($header)) {
danielebarchiesi@0 1969 $ts = tablesort_init($header);
danielebarchiesi@0 1970 // HTML requires that the thead tag has tr tags in it followed by tbody
danielebarchiesi@0 1971 // tags. Using ternary operator to check and see if we have any rows.
danielebarchiesi@0 1972 $output .= (count($rows) ? ' <thead><tr>' : ' <tr>');
danielebarchiesi@0 1973 foreach ($header as $cell) {
danielebarchiesi@0 1974 $cell = tablesort_header($cell, $header, $ts);
danielebarchiesi@0 1975 $output .= _theme_table_cell($cell, TRUE);
danielebarchiesi@0 1976 }
danielebarchiesi@0 1977 // Using ternary operator to close the tags based on whether or not there are rows
danielebarchiesi@0 1978 $output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
danielebarchiesi@0 1979 }
danielebarchiesi@0 1980 else {
danielebarchiesi@0 1981 $ts = array();
danielebarchiesi@0 1982 }
danielebarchiesi@0 1983
danielebarchiesi@0 1984 // Format the table rows:
danielebarchiesi@0 1985 if (count($rows)) {
danielebarchiesi@0 1986 $output .= "<tbody>\n";
danielebarchiesi@0 1987 $flip = array('even' => 'odd', 'odd' => 'even');
danielebarchiesi@0 1988 $class = 'even';
danielebarchiesi@0 1989 foreach ($rows as $number => $row) {
danielebarchiesi@0 1990 // Check if we're dealing with a simple or complex row
danielebarchiesi@0 1991 if (isset($row['data'])) {
danielebarchiesi@0 1992 $cells = $row['data'];
danielebarchiesi@0 1993 $no_striping = isset($row['no_striping']) ? $row['no_striping'] : FALSE;
danielebarchiesi@0 1994
danielebarchiesi@0 1995 // Set the attributes array and exclude 'data' and 'no_striping'.
danielebarchiesi@0 1996 $attributes = $row;
danielebarchiesi@0 1997 unset($attributes['data']);
danielebarchiesi@0 1998 unset($attributes['no_striping']);
danielebarchiesi@0 1999 }
danielebarchiesi@0 2000 else {
danielebarchiesi@0 2001 $cells = $row;
danielebarchiesi@0 2002 $attributes = array();
danielebarchiesi@0 2003 $no_striping = FALSE;
danielebarchiesi@0 2004 }
danielebarchiesi@0 2005 if (count($cells)) {
danielebarchiesi@0 2006 // Add odd/even class
danielebarchiesi@0 2007 if (!$no_striping) {
danielebarchiesi@0 2008 $class = $flip[$class];
danielebarchiesi@0 2009 $attributes['class'][] = $class;
danielebarchiesi@0 2010 }
danielebarchiesi@0 2011
danielebarchiesi@0 2012 // Build row
danielebarchiesi@0 2013 $output .= ' <tr' . drupal_attributes($attributes) . '>';
danielebarchiesi@0 2014 $i = 0;
danielebarchiesi@0 2015 foreach ($cells as $cell) {
danielebarchiesi@0 2016 $cell = tablesort_cell($cell, $header, $ts, $i++);
danielebarchiesi@0 2017 $output .= _theme_table_cell($cell);
danielebarchiesi@0 2018 }
danielebarchiesi@0 2019 $output .= " </tr>\n";
danielebarchiesi@0 2020 }
danielebarchiesi@0 2021 }
danielebarchiesi@0 2022 $output .= "</tbody>\n";
danielebarchiesi@0 2023 }
danielebarchiesi@0 2024
danielebarchiesi@0 2025 $output .= "</table>\n";
danielebarchiesi@0 2026 return $output;
danielebarchiesi@0 2027 }
danielebarchiesi@0 2028
danielebarchiesi@0 2029 /**
danielebarchiesi@0 2030 * Returns HTML for a sort icon.
danielebarchiesi@0 2031 *
danielebarchiesi@0 2032 * @param $variables
danielebarchiesi@0 2033 * An associative array containing:
danielebarchiesi@0 2034 * - style: Set to either 'asc' or 'desc', this determines which icon to
danielebarchiesi@0 2035 * show.
danielebarchiesi@0 2036 */
danielebarchiesi@0 2037 function theme_tablesort_indicator($variables) {
danielebarchiesi@0 2038 if ($variables['style'] == "asc") {
danielebarchiesi@0 2039 return theme('image', array('path' => 'misc/arrow-asc.png', 'width' => 13, 'height' => 13, 'alt' => t('sort ascending'), 'title' => t('sort ascending')));
danielebarchiesi@0 2040 }
danielebarchiesi@0 2041 else {
danielebarchiesi@0 2042 return theme('image', array('path' => 'misc/arrow-desc.png', 'width' => 13, 'height' => 13, 'alt' => t('sort descending'), 'title' => t('sort descending')));
danielebarchiesi@0 2043 }
danielebarchiesi@0 2044 }
danielebarchiesi@0 2045
danielebarchiesi@0 2046 /**
danielebarchiesi@0 2047 * Returns HTML for a marker for new or updated content.
danielebarchiesi@0 2048 *
danielebarchiesi@0 2049 * @param $variables
danielebarchiesi@0 2050 * An associative array containing:
danielebarchiesi@0 2051 * - type: Number representing the marker type to display. See MARK_NEW,
danielebarchiesi@0 2052 * MARK_UPDATED, MARK_READ.
danielebarchiesi@0 2053 */
danielebarchiesi@0 2054 function theme_mark($variables) {
danielebarchiesi@0 2055 $type = $variables['type'];
danielebarchiesi@0 2056 global $user;
danielebarchiesi@0 2057 if ($user->uid) {
danielebarchiesi@0 2058 if ($type == MARK_NEW) {
danielebarchiesi@0 2059 return ' <span class="marker">' . t('new') . '</span>';
danielebarchiesi@0 2060 }
danielebarchiesi@0 2061 elseif ($type == MARK_UPDATED) {
danielebarchiesi@0 2062 return ' <span class="marker">' . t('updated') . '</span>';
danielebarchiesi@0 2063 }
danielebarchiesi@0 2064 }
danielebarchiesi@0 2065 }
danielebarchiesi@0 2066
danielebarchiesi@0 2067 /**
danielebarchiesi@0 2068 * Returns HTML for a list or nested list of items.
danielebarchiesi@0 2069 *
danielebarchiesi@0 2070 * @param $variables
danielebarchiesi@0 2071 * An associative array containing:
danielebarchiesi@0 2072 * - items: An array of items to be displayed in the list. If an item is a
danielebarchiesi@0 2073 * string, then it is used as is. If an item is an array, then the "data"
danielebarchiesi@0 2074 * element of the array is used as the contents of the list item. If an item
danielebarchiesi@0 2075 * is an array with a "children" element, those children are displayed in a
danielebarchiesi@0 2076 * nested list. All other elements are treated as attributes of the list
danielebarchiesi@0 2077 * item element.
danielebarchiesi@0 2078 * - title: The title of the list.
danielebarchiesi@0 2079 * - type: The type of list to return (e.g. "ul", "ol").
danielebarchiesi@0 2080 * - attributes: The attributes applied to the list element.
danielebarchiesi@0 2081 */
danielebarchiesi@0 2082 function theme_item_list($variables) {
danielebarchiesi@0 2083 $items = $variables['items'];
danielebarchiesi@0 2084 $title = $variables['title'];
danielebarchiesi@0 2085 $type = $variables['type'];
danielebarchiesi@0 2086 $attributes = $variables['attributes'];
danielebarchiesi@0 2087
danielebarchiesi@0 2088 // Only output the list container and title, if there are any list items.
danielebarchiesi@0 2089 // Check to see whether the block title exists before adding a header.
danielebarchiesi@0 2090 // Empty headers are not semantic and present accessibility challenges.
danielebarchiesi@0 2091 $output = '<div class="item-list">';
danielebarchiesi@0 2092 if (isset($title) && $title !== '') {
danielebarchiesi@0 2093 $output .= '<h3>' . $title . '</h3>';
danielebarchiesi@0 2094 }
danielebarchiesi@0 2095
danielebarchiesi@0 2096 if (!empty($items)) {
danielebarchiesi@0 2097 $output .= "<$type" . drupal_attributes($attributes) . '>';
danielebarchiesi@0 2098 $num_items = count($items);
danielebarchiesi@0 2099 $i = 0;
danielebarchiesi@0 2100 foreach ($items as $item) {
danielebarchiesi@0 2101 $attributes = array();
danielebarchiesi@0 2102 $children = array();
danielebarchiesi@0 2103 $data = '';
danielebarchiesi@0 2104 $i++;
danielebarchiesi@0 2105 if (is_array($item)) {
danielebarchiesi@0 2106 foreach ($item as $key => $value) {
danielebarchiesi@0 2107 if ($key == 'data') {
danielebarchiesi@0 2108 $data = $value;
danielebarchiesi@0 2109 }
danielebarchiesi@0 2110 elseif ($key == 'children') {
danielebarchiesi@0 2111 $children = $value;
danielebarchiesi@0 2112 }
danielebarchiesi@0 2113 else {
danielebarchiesi@0 2114 $attributes[$key] = $value;
danielebarchiesi@0 2115 }
danielebarchiesi@0 2116 }
danielebarchiesi@0 2117 }
danielebarchiesi@0 2118 else {
danielebarchiesi@0 2119 $data = $item;
danielebarchiesi@0 2120 }
danielebarchiesi@0 2121 if (count($children) > 0) {
danielebarchiesi@0 2122 // Render nested list.
danielebarchiesi@0 2123 $data .= theme_item_list(array('items' => $children, 'title' => NULL, 'type' => $type, 'attributes' => $attributes));
danielebarchiesi@0 2124 }
danielebarchiesi@0 2125 if ($i == 1) {
danielebarchiesi@0 2126 $attributes['class'][] = 'first';
danielebarchiesi@0 2127 }
danielebarchiesi@0 2128 if ($i == $num_items) {
danielebarchiesi@0 2129 $attributes['class'][] = 'last';
danielebarchiesi@0 2130 }
danielebarchiesi@0 2131 $output .= '<li' . drupal_attributes($attributes) . '>' . $data . "</li>\n";
danielebarchiesi@0 2132 }
danielebarchiesi@0 2133 $output .= "</$type>";
danielebarchiesi@0 2134 }
danielebarchiesi@0 2135 $output .= '</div>';
danielebarchiesi@0 2136 return $output;
danielebarchiesi@0 2137 }
danielebarchiesi@0 2138
danielebarchiesi@0 2139 /**
danielebarchiesi@0 2140 * Returns HTML for a "more help" link.
danielebarchiesi@0 2141 *
danielebarchiesi@0 2142 * @param $variables
danielebarchiesi@0 2143 * An associative array containing:
danielebarchiesi@0 2144 * - url: The URL for the link.
danielebarchiesi@0 2145 */
danielebarchiesi@0 2146 function theme_more_help_link($variables) {
danielebarchiesi@0 2147 return '<div class="more-help-link">' . l(t('More help'), $variables['url']) . '</div>';
danielebarchiesi@0 2148 }
danielebarchiesi@0 2149
danielebarchiesi@0 2150 /**
danielebarchiesi@0 2151 * Returns HTML for a feed icon.
danielebarchiesi@0 2152 *
danielebarchiesi@0 2153 * @param $variables
danielebarchiesi@0 2154 * An associative array containing:
danielebarchiesi@0 2155 * - url: An internal system path or a fully qualified external URL of the
danielebarchiesi@0 2156 * feed.
danielebarchiesi@0 2157 * - title: A descriptive title of the feed.
danielebarchiesi@0 2158 */
danielebarchiesi@0 2159 function theme_feed_icon($variables) {
danielebarchiesi@0 2160 $text = t('Subscribe to !feed-title', array('!feed-title' => $variables['title']));
danielebarchiesi@0 2161 if ($image = theme('image', array('path' => 'misc/feed.png', 'width' => 16, 'height' => 16, 'alt' => $text))) {
danielebarchiesi@0 2162 return l($image, $variables['url'], array('html' => TRUE, 'attributes' => array('class' => array('feed-icon'), 'title' => $text)));
danielebarchiesi@0 2163 }
danielebarchiesi@0 2164 }
danielebarchiesi@0 2165
danielebarchiesi@0 2166 /**
danielebarchiesi@0 2167 * Returns HTML for a generic HTML tag with attributes.
danielebarchiesi@0 2168 *
danielebarchiesi@0 2169 * @param $variables
danielebarchiesi@0 2170 * An associative array containing:
danielebarchiesi@0 2171 * - element: An associative array describing the tag:
danielebarchiesi@0 2172 * - #tag: The tag name to output. Typical tags added to the HTML HEAD:
danielebarchiesi@0 2173 * - meta: To provide meta information, such as a page refresh.
danielebarchiesi@0 2174 * - link: To refer to stylesheets and other contextual information.
danielebarchiesi@0 2175 * - script: To load JavaScript.
danielebarchiesi@0 2176 * - #attributes: (optional) An array of HTML attributes to apply to the
danielebarchiesi@0 2177 * tag.
danielebarchiesi@0 2178 * - #value: (optional) A string containing tag content, such as inline
danielebarchiesi@0 2179 * CSS.
danielebarchiesi@0 2180 * - #value_prefix: (optional) A string to prepend to #value, e.g. a CDATA
danielebarchiesi@0 2181 * wrapper prefix.
danielebarchiesi@0 2182 * - #value_suffix: (optional) A string to append to #value, e.g. a CDATA
danielebarchiesi@0 2183 * wrapper suffix.
danielebarchiesi@0 2184 */
danielebarchiesi@0 2185 function theme_html_tag($variables) {
danielebarchiesi@0 2186 $element = $variables['element'];
danielebarchiesi@0 2187 $attributes = isset($element['#attributes']) ? drupal_attributes($element['#attributes']) : '';
danielebarchiesi@0 2188 if (!isset($element['#value'])) {
danielebarchiesi@0 2189 return '<' . $element['#tag'] . $attributes . " />\n";
danielebarchiesi@0 2190 }
danielebarchiesi@0 2191 else {
danielebarchiesi@0 2192 $output = '<' . $element['#tag'] . $attributes . '>';
danielebarchiesi@0 2193 if (isset($element['#value_prefix'])) {
danielebarchiesi@0 2194 $output .= $element['#value_prefix'];
danielebarchiesi@0 2195 }
danielebarchiesi@0 2196 $output .= $element['#value'];
danielebarchiesi@0 2197 if (isset($element['#value_suffix'])) {
danielebarchiesi@0 2198 $output .= $element['#value_suffix'];
danielebarchiesi@0 2199 }
danielebarchiesi@0 2200 $output .= '</' . $element['#tag'] . ">\n";
danielebarchiesi@0 2201 return $output;
danielebarchiesi@0 2202 }
danielebarchiesi@0 2203 }
danielebarchiesi@0 2204
danielebarchiesi@0 2205 /**
danielebarchiesi@0 2206 * Returns HTML for a "more" link, like those used in blocks.
danielebarchiesi@0 2207 *
danielebarchiesi@0 2208 * @param $variables
danielebarchiesi@0 2209 * An associative array containing:
danielebarchiesi@0 2210 * - url: The URL of the main page.
danielebarchiesi@0 2211 * - title: A descriptive verb for the link, like 'Read more'.
danielebarchiesi@0 2212 */
danielebarchiesi@0 2213 function theme_more_link($variables) {
danielebarchiesi@0 2214 return '<div class="more-link">' . l(t('More'), $variables['url'], array('attributes' => array('title' => $variables['title']))) . '</div>';
danielebarchiesi@0 2215 }
danielebarchiesi@0 2216
danielebarchiesi@0 2217 /**
danielebarchiesi@0 2218 * Returns HTML for a username, potentially linked to the user's page.
danielebarchiesi@0 2219 *
danielebarchiesi@0 2220 * @param $variables
danielebarchiesi@0 2221 * An associative array containing:
danielebarchiesi@0 2222 * - account: The user object to format.
danielebarchiesi@0 2223 * - name: The user's name, sanitized.
danielebarchiesi@0 2224 * - extra: Additional text to append to the user's name, sanitized.
danielebarchiesi@0 2225 * - link_path: The path or URL of the user's profile page, home page, or
danielebarchiesi@0 2226 * other desired page to link to for more information about the user.
danielebarchiesi@0 2227 * - link_options: An array of options to pass to the l() function's $options
danielebarchiesi@0 2228 * parameter if linking the user's name to the user's page.
danielebarchiesi@0 2229 * - attributes_array: An array of attributes to pass to the
danielebarchiesi@0 2230 * drupal_attributes() function if not linking to the user's page.
danielebarchiesi@0 2231 *
danielebarchiesi@0 2232 * @see template_preprocess_username()
danielebarchiesi@0 2233 * @see template_process_username()
danielebarchiesi@0 2234 */
danielebarchiesi@0 2235 function theme_username($variables) {
danielebarchiesi@0 2236 if (isset($variables['link_path'])) {
danielebarchiesi@0 2237 // We have a link path, so we should generate a link using l().
danielebarchiesi@0 2238 // Additional classes may be added as array elements like
danielebarchiesi@0 2239 // $variables['link_options']['attributes']['class'][] = 'myclass';
danielebarchiesi@0 2240 $output = l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
danielebarchiesi@0 2241 }
danielebarchiesi@0 2242 else {
danielebarchiesi@0 2243 // Modules may have added important attributes so they must be included
danielebarchiesi@0 2244 // in the output. Additional classes may be added as array elements like
danielebarchiesi@0 2245 // $variables['attributes_array']['class'][] = 'myclass';
danielebarchiesi@0 2246 $output = '<span' . drupal_attributes($variables['attributes_array']) . '>' . $variables['name'] . $variables['extra'] . '</span>';
danielebarchiesi@0 2247 }
danielebarchiesi@0 2248 return $output;
danielebarchiesi@0 2249 }
danielebarchiesi@0 2250
danielebarchiesi@0 2251 /**
danielebarchiesi@0 2252 * Returns HTML for a progress bar.
danielebarchiesi@0 2253 *
danielebarchiesi@0 2254 * Note that the core Batch API uses this only for non-JavaScript batch jobs.
danielebarchiesi@0 2255 *
danielebarchiesi@0 2256 * @param $variables
danielebarchiesi@0 2257 * An associative array containing:
danielebarchiesi@0 2258 * - percent: The percentage of the progress.
danielebarchiesi@0 2259 * - message: A string containing information to be displayed.
danielebarchiesi@0 2260 */
danielebarchiesi@0 2261 function theme_progress_bar($variables) {
danielebarchiesi@0 2262 $output = '<div id="progress" class="progress">';
danielebarchiesi@0 2263 $output .= '<div class="bar"><div class="filled" style="width: ' . $variables['percent'] . '%"></div></div>';
danielebarchiesi@0 2264 $output .= '<div class="percentage">' . $variables['percent'] . '%</div>';
danielebarchiesi@0 2265 $output .= '<div class="message">' . $variables['message'] . '</div>';
danielebarchiesi@0 2266 $output .= '</div>';
danielebarchiesi@0 2267
danielebarchiesi@0 2268 return $output;
danielebarchiesi@0 2269 }
danielebarchiesi@0 2270
danielebarchiesi@0 2271 /**
danielebarchiesi@0 2272 * Returns HTML for an indentation div; used for drag and drop tables.
danielebarchiesi@0 2273 *
danielebarchiesi@0 2274 * @param $variables
danielebarchiesi@0 2275 * An associative array containing:
danielebarchiesi@0 2276 * - size: Optional. The number of indentations to create.
danielebarchiesi@0 2277 */
danielebarchiesi@0 2278 function theme_indentation($variables) {
danielebarchiesi@0 2279 $output = '';
danielebarchiesi@0 2280 for ($n = 0; $n < $variables['size']; $n++) {
danielebarchiesi@0 2281 $output .= '<div class="indentation">&nbsp;</div>';
danielebarchiesi@0 2282 }
danielebarchiesi@0 2283 return $output;
danielebarchiesi@0 2284 }
danielebarchiesi@0 2285
danielebarchiesi@0 2286 /**
danielebarchiesi@0 2287 * @} End of "addtogroup themeable".
danielebarchiesi@0 2288 */
danielebarchiesi@0 2289
danielebarchiesi@0 2290 /**
danielebarchiesi@0 2291 * Returns HTML output for a single table cell for theme_table().
danielebarchiesi@0 2292 *
danielebarchiesi@0 2293 * @param $cell
danielebarchiesi@0 2294 * Array of cell information, or string to display in cell.
danielebarchiesi@0 2295 * @param bool $header
danielebarchiesi@0 2296 * TRUE if this cell is a table header cell, FALSE if it is an ordinary
danielebarchiesi@0 2297 * table cell. If $cell is an array with element 'header' set to TRUE, that
danielebarchiesi@0 2298 * will override the $header parameter.
danielebarchiesi@0 2299 *
danielebarchiesi@0 2300 * @return
danielebarchiesi@0 2301 * HTML for the cell.
danielebarchiesi@0 2302 */
danielebarchiesi@0 2303 function _theme_table_cell($cell, $header = FALSE) {
danielebarchiesi@0 2304 $attributes = '';
danielebarchiesi@0 2305
danielebarchiesi@0 2306 if (is_array($cell)) {
danielebarchiesi@0 2307 $data = isset($cell['data']) ? $cell['data'] : '';
danielebarchiesi@0 2308 // Cell's data property can be a string or a renderable array.
danielebarchiesi@0 2309 if (is_array($data)) {
danielebarchiesi@0 2310 $data = drupal_render($data);
danielebarchiesi@0 2311 }
danielebarchiesi@0 2312 $header |= isset($cell['header']);
danielebarchiesi@0 2313 unset($cell['data']);
danielebarchiesi@0 2314 unset($cell['header']);
danielebarchiesi@0 2315 $attributes = drupal_attributes($cell);
danielebarchiesi@0 2316 }
danielebarchiesi@0 2317 else {
danielebarchiesi@0 2318 $data = $cell;
danielebarchiesi@0 2319 }
danielebarchiesi@0 2320
danielebarchiesi@0 2321 if ($header) {
danielebarchiesi@0 2322 $output = "<th$attributes>$data</th>";
danielebarchiesi@0 2323 }
danielebarchiesi@0 2324 else {
danielebarchiesi@0 2325 $output = "<td$attributes>$data</td>";
danielebarchiesi@0 2326 }
danielebarchiesi@0 2327
danielebarchiesi@0 2328 return $output;
danielebarchiesi@0 2329 }
danielebarchiesi@0 2330
danielebarchiesi@0 2331 /**
danielebarchiesi@0 2332 * Adds a default set of helper variables for variable processors and templates.
danielebarchiesi@0 2333 *
danielebarchiesi@0 2334 * This function is called for theme hooks implemented as templates only, not
danielebarchiesi@0 2335 * for theme hooks implemented as functions. This preprocess function is the
danielebarchiesi@0 2336 * first in the sequence of preprocessing and processing functions that is
danielebarchiesi@0 2337 * called when preparing variables for a template. See theme() for more details
danielebarchiesi@0 2338 * about the full sequence.
danielebarchiesi@0 2339 *
danielebarchiesi@0 2340 * @see theme()
danielebarchiesi@0 2341 * @see template_process()
danielebarchiesi@0 2342 */
danielebarchiesi@0 2343 function template_preprocess(&$variables, $hook) {
danielebarchiesi@0 2344 global $user;
danielebarchiesi@0 2345 static $count = array();
danielebarchiesi@0 2346
danielebarchiesi@0 2347 // Track run count for each hook to provide zebra striping. See
danielebarchiesi@0 2348 // "template_preprocess_block()" which provides the same feature specific to
danielebarchiesi@0 2349 // blocks.
danielebarchiesi@0 2350 $count[$hook] = isset($count[$hook]) && is_int($count[$hook]) ? $count[$hook] : 1;
danielebarchiesi@0 2351 $variables['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even';
danielebarchiesi@0 2352 $variables['id'] = $count[$hook]++;
danielebarchiesi@0 2353
danielebarchiesi@0 2354 // Tell all templates where they are located.
danielebarchiesi@0 2355 $variables['directory'] = path_to_theme();
danielebarchiesi@0 2356
danielebarchiesi@0 2357 // Initialize html class attribute for the current hook.
danielebarchiesi@0 2358 $variables['classes_array'] = array(drupal_html_class($hook));
danielebarchiesi@0 2359
danielebarchiesi@0 2360 // Merge in variables that don't depend on hook and don't change during a
danielebarchiesi@0 2361 // single page request.
danielebarchiesi@0 2362 // Use the advanced drupal_static() pattern, since this is called very often.
danielebarchiesi@0 2363 static $drupal_static_fast;
danielebarchiesi@0 2364 if (!isset($drupal_static_fast)) {
danielebarchiesi@0 2365 $drupal_static_fast['default_variables'] = &drupal_static(__FUNCTION__);
danielebarchiesi@0 2366 }
danielebarchiesi@0 2367 $default_variables = &$drupal_static_fast['default_variables'];
danielebarchiesi@0 2368 // Global $user object shouldn't change during a page request once rendering
danielebarchiesi@0 2369 // has started, but if there's an edge case where it does, re-fetch the
danielebarchiesi@0 2370 // variables appropriate for the new user.
danielebarchiesi@0 2371 if (!isset($default_variables) || ($user !== $default_variables['user'])) {
danielebarchiesi@0 2372 $default_variables = _template_preprocess_default_variables();
danielebarchiesi@0 2373 }
danielebarchiesi@0 2374 $variables += $default_variables;
danielebarchiesi@0 2375 }
danielebarchiesi@0 2376
danielebarchiesi@0 2377 /**
danielebarchiesi@0 2378 * Returns hook-independent variables to template_preprocess().
danielebarchiesi@0 2379 */
danielebarchiesi@0 2380 function _template_preprocess_default_variables() {
danielebarchiesi@0 2381 global $user;
danielebarchiesi@0 2382
danielebarchiesi@0 2383 // Variables that don't depend on a database connection.
danielebarchiesi@0 2384 $variables = array(
danielebarchiesi@0 2385 'attributes_array' => array(),
danielebarchiesi@0 2386 'title_attributes_array' => array(),
danielebarchiesi@0 2387 'content_attributes_array' => array(),
danielebarchiesi@0 2388 'title_prefix' => array(),
danielebarchiesi@0 2389 'title_suffix' => array(),
danielebarchiesi@0 2390 'user' => $user,
danielebarchiesi@0 2391 'db_is_active' => !defined('MAINTENANCE_MODE'),
danielebarchiesi@0 2392 'is_admin' => FALSE,
danielebarchiesi@0 2393 'logged_in' => FALSE,
danielebarchiesi@0 2394 );
danielebarchiesi@0 2395
danielebarchiesi@0 2396 // The user object has no uid property when the database does not exist during
danielebarchiesi@0 2397 // install. The user_access() check deals with issues when in maintenance mode
danielebarchiesi@0 2398 // as uid is set but the user.module has not been included.
danielebarchiesi@0 2399 if (isset($user->uid) && function_exists('user_access')) {
danielebarchiesi@0 2400 $variables['is_admin'] = user_access('access administration pages');
danielebarchiesi@0 2401 $variables['logged_in'] = ($user->uid > 0);
danielebarchiesi@0 2402 }
danielebarchiesi@0 2403
danielebarchiesi@0 2404 // drupal_is_front_page() might throw an exception.
danielebarchiesi@0 2405 try {
danielebarchiesi@0 2406 $variables['is_front'] = drupal_is_front_page();
danielebarchiesi@0 2407 }
danielebarchiesi@0 2408 catch (Exception $e) {
danielebarchiesi@0 2409 // If the database is not yet available, set default values for these
danielebarchiesi@0 2410 // variables.
danielebarchiesi@0 2411 $variables['is_front'] = FALSE;
danielebarchiesi@0 2412 $variables['db_is_active'] = FALSE;
danielebarchiesi@0 2413 }
danielebarchiesi@0 2414
danielebarchiesi@0 2415 return $variables;
danielebarchiesi@0 2416 }
danielebarchiesi@0 2417
danielebarchiesi@0 2418 /**
danielebarchiesi@0 2419 * Adds helper variables derived from variables defined during preprocessing.
danielebarchiesi@0 2420 *
danielebarchiesi@0 2421 * When preparing variables for a theme hook implementation, all 'preprocess'
danielebarchiesi@0 2422 * functions run first, then all 'process' functions (see theme() for details
danielebarchiesi@0 2423 * about the full sequence).
danielebarchiesi@0 2424 *
danielebarchiesi@0 2425 * This function serializes array variables manipulated during the preprocessing
danielebarchiesi@0 2426 * phase into strings for convenient use by templates. As with
danielebarchiesi@0 2427 * template_preprocess(), this function does not get called for theme hooks
danielebarchiesi@0 2428 * implemented as functions.
danielebarchiesi@0 2429 *
danielebarchiesi@0 2430 * @see theme()
danielebarchiesi@0 2431 * @see template_preprocess()
danielebarchiesi@0 2432 */
danielebarchiesi@0 2433 function template_process(&$variables, $hook) {
danielebarchiesi@0 2434 // Flatten out classes.
danielebarchiesi@0 2435 $variables['classes'] = implode(' ', $variables['classes_array']);
danielebarchiesi@0 2436
danielebarchiesi@0 2437 // Flatten out attributes, title_attributes, and content_attributes.
danielebarchiesi@0 2438 // Because this function can be called very often, and often with empty
danielebarchiesi@0 2439 // attributes, optimize performance by only calling drupal_attributes() if
danielebarchiesi@0 2440 // necessary.
danielebarchiesi@0 2441 $variables['attributes'] = $variables['attributes_array'] ? drupal_attributes($variables['attributes_array']) : '';
danielebarchiesi@0 2442 $variables['title_attributes'] = $variables['title_attributes_array'] ? drupal_attributes($variables['title_attributes_array']) : '';
danielebarchiesi@0 2443 $variables['content_attributes'] = $variables['content_attributes_array'] ? drupal_attributes($variables['content_attributes_array']) : '';
danielebarchiesi@0 2444 }
danielebarchiesi@0 2445
danielebarchiesi@0 2446 /**
danielebarchiesi@0 2447 * Preprocess variables for html.tpl.php
danielebarchiesi@0 2448 *
danielebarchiesi@0 2449 * @see system_elements()
danielebarchiesi@0 2450 * @see html.tpl.php
danielebarchiesi@0 2451 */
danielebarchiesi@0 2452 function template_preprocess_html(&$variables) {
danielebarchiesi@0 2453 // Compile a list of classes that are going to be applied to the body element.
danielebarchiesi@0 2454 // This allows advanced theming based on context (home page, node of certain type, etc.).
danielebarchiesi@0 2455 // Add a class that tells us whether we're on the front page or not.
danielebarchiesi@0 2456 $variables['classes_array'][] = $variables['is_front'] ? 'front' : 'not-front';
danielebarchiesi@0 2457 // Add a class that tells us whether the page is viewed by an authenticated user or not.
danielebarchiesi@0 2458 $variables['classes_array'][] = $variables['logged_in'] ? 'logged-in' : 'not-logged-in';
danielebarchiesi@0 2459
danielebarchiesi@0 2460 // Add information about the number of sidebars.
danielebarchiesi@0 2461 if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) {
danielebarchiesi@0 2462 $variables['classes_array'][] = 'two-sidebars';
danielebarchiesi@0 2463 }
danielebarchiesi@0 2464 elseif (!empty($variables['page']['sidebar_first'])) {
danielebarchiesi@0 2465 $variables['classes_array'][] = 'one-sidebar sidebar-first';
danielebarchiesi@0 2466 }
danielebarchiesi@0 2467 elseif (!empty($variables['page']['sidebar_second'])) {
danielebarchiesi@0 2468 $variables['classes_array'][] = 'one-sidebar sidebar-second';
danielebarchiesi@0 2469 }
danielebarchiesi@0 2470 else {
danielebarchiesi@0 2471 $variables['classes_array'][] = 'no-sidebars';
danielebarchiesi@0 2472 }
danielebarchiesi@0 2473
danielebarchiesi@0 2474 // Populate the body classes.
danielebarchiesi@0 2475 if ($suggestions = theme_get_suggestions(arg(), 'page', '-')) {
danielebarchiesi@0 2476 foreach ($suggestions as $suggestion) {
danielebarchiesi@0 2477 if ($suggestion != 'page-front') {
danielebarchiesi@0 2478 // Add current suggestion to page classes to make it possible to theme
danielebarchiesi@0 2479 // the page depending on the current page type (e.g. node, admin, user,
danielebarchiesi@0 2480 // etc.) as well as more specific data like node-12 or node-edit.
danielebarchiesi@0 2481 $variables['classes_array'][] = drupal_html_class($suggestion);
danielebarchiesi@0 2482 }
danielebarchiesi@0 2483 }
danielebarchiesi@0 2484 }
danielebarchiesi@0 2485
danielebarchiesi@0 2486 // If on an individual node page, add the node type to body classes.
danielebarchiesi@0 2487 if ($node = menu_get_object()) {
danielebarchiesi@0 2488 $variables['classes_array'][] = drupal_html_class('node-type-' . $node->type);
danielebarchiesi@0 2489 }
danielebarchiesi@0 2490
danielebarchiesi@0 2491 // RDFa allows annotation of XHTML pages with RDF data, while GRDDL provides
danielebarchiesi@0 2492 // mechanisms for extraction of this RDF content via XSLT transformation
danielebarchiesi@0 2493 // using an associated GRDDL profile.
danielebarchiesi@0 2494 $variables['rdf_namespaces'] = drupal_get_rdf_namespaces();
danielebarchiesi@0 2495 $variables['grddl_profile'] = 'http://www.w3.org/1999/xhtml/vocab';
danielebarchiesi@0 2496 $variables['language'] = $GLOBALS['language'];
danielebarchiesi@0 2497 $variables['language']->dir = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
danielebarchiesi@0 2498
danielebarchiesi@0 2499 // Add favicon.
danielebarchiesi@0 2500 if (theme_get_setting('toggle_favicon')) {
danielebarchiesi@0 2501 $favicon = theme_get_setting('favicon');
danielebarchiesi@0 2502 $type = theme_get_setting('favicon_mimetype');
danielebarchiesi@0 2503 drupal_add_html_head_link(array('rel' => 'shortcut icon', 'href' => drupal_strip_dangerous_protocols($favicon), 'type' => $type));
danielebarchiesi@0 2504 }
danielebarchiesi@0 2505
danielebarchiesi@0 2506 // Construct page title.
danielebarchiesi@0 2507 if (drupal_get_title()) {
danielebarchiesi@0 2508 $head_title = array(
danielebarchiesi@0 2509 'title' => strip_tags(drupal_get_title()),
danielebarchiesi@0 2510 'name' => check_plain(variable_get('site_name', 'Drupal')),
danielebarchiesi@0 2511 );
danielebarchiesi@0 2512 }
danielebarchiesi@0 2513 else {
danielebarchiesi@0 2514 $head_title = array('name' => check_plain(variable_get('site_name', 'Drupal')));
danielebarchiesi@0 2515 if (variable_get('site_slogan', '')) {
danielebarchiesi@0 2516 $head_title['slogan'] = filter_xss_admin(variable_get('site_slogan', ''));
danielebarchiesi@0 2517 }
danielebarchiesi@0 2518 }
danielebarchiesi@0 2519 $variables['head_title_array'] = $head_title;
danielebarchiesi@0 2520 $variables['head_title'] = implode(' | ', $head_title);
danielebarchiesi@0 2521
danielebarchiesi@0 2522 // Populate the page template suggestions.
danielebarchiesi@0 2523 if ($suggestions = theme_get_suggestions(arg(), 'html')) {
danielebarchiesi@0 2524 $variables['theme_hook_suggestions'] = $suggestions;
danielebarchiesi@0 2525 }
danielebarchiesi@0 2526 }
danielebarchiesi@0 2527
danielebarchiesi@0 2528 /**
danielebarchiesi@0 2529 * Preprocess variables for page.tpl.php
danielebarchiesi@0 2530 *
danielebarchiesi@0 2531 * Most themes utilize their own copy of page.tpl.php. The default is located
danielebarchiesi@0 2532 * inside "modules/system/page.tpl.php". Look in there for the full list of
danielebarchiesi@0 2533 * variables.
danielebarchiesi@0 2534 *
danielebarchiesi@0 2535 * Uses the arg() function to generate a series of page template suggestions
danielebarchiesi@0 2536 * based on the current path.
danielebarchiesi@0 2537 *
danielebarchiesi@0 2538 * Any changes to variables in this preprocessor should also be changed inside
danielebarchiesi@0 2539 * template_preprocess_maintenance_page() to keep all of them consistent.
danielebarchiesi@0 2540 *
danielebarchiesi@0 2541 * @see drupal_render_page()
danielebarchiesi@0 2542 * @see template_process_page()
danielebarchiesi@0 2543 * @see page.tpl.php
danielebarchiesi@0 2544 */
danielebarchiesi@0 2545 function template_preprocess_page(&$variables) {
danielebarchiesi@0 2546 // Move some variables to the top level for themer convenience and template cleanliness.
danielebarchiesi@0 2547 $variables['show_messages'] = $variables['page']['#show_messages'];
danielebarchiesi@0 2548
danielebarchiesi@0 2549 foreach (system_region_list($GLOBALS['theme']) as $region_key => $region_name) {
danielebarchiesi@0 2550 if (!isset($variables['page'][$region_key])) {
danielebarchiesi@0 2551 $variables['page'][$region_key] = array();
danielebarchiesi@0 2552 }
danielebarchiesi@0 2553 }
danielebarchiesi@0 2554
danielebarchiesi@0 2555 // Set up layout variable.
danielebarchiesi@0 2556 $variables['layout'] = 'none';
danielebarchiesi@0 2557 if (!empty($variables['page']['sidebar_first'])) {
danielebarchiesi@0 2558 $variables['layout'] = 'first';
danielebarchiesi@0 2559 }
danielebarchiesi@0 2560 if (!empty($variables['page']['sidebar_second'])) {
danielebarchiesi@0 2561 $variables['layout'] = ($variables['layout'] == 'first') ? 'both' : 'second';
danielebarchiesi@0 2562 }
danielebarchiesi@0 2563
danielebarchiesi@0 2564 $variables['base_path'] = base_path();
danielebarchiesi@0 2565 $variables['front_page'] = url();
danielebarchiesi@0 2566 $variables['feed_icons'] = drupal_get_feeds();
danielebarchiesi@0 2567 $variables['language'] = $GLOBALS['language'];
danielebarchiesi@0 2568 $variables['language']->dir = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
danielebarchiesi@0 2569 $variables['logo'] = theme_get_setting('logo');
danielebarchiesi@0 2570 $variables['main_menu'] = theme_get_setting('toggle_main_menu') ? menu_main_menu() : array();
danielebarchiesi@0 2571 $variables['secondary_menu'] = theme_get_setting('toggle_secondary_menu') ? menu_secondary_menu() : array();
danielebarchiesi@0 2572 $variables['action_links'] = menu_local_actions();
danielebarchiesi@0 2573 $variables['site_name'] = (theme_get_setting('toggle_name') ? filter_xss_admin(variable_get('site_name', 'Drupal')) : '');
danielebarchiesi@0 2574 $variables['site_slogan'] = (theme_get_setting('toggle_slogan') ? filter_xss_admin(variable_get('site_slogan', '')) : '');
danielebarchiesi@0 2575 $variables['tabs'] = menu_local_tabs();
danielebarchiesi@0 2576
danielebarchiesi@0 2577 if ($node = menu_get_object()) {
danielebarchiesi@0 2578 $variables['node'] = $node;
danielebarchiesi@0 2579 }
danielebarchiesi@0 2580
danielebarchiesi@0 2581 // Populate the page template suggestions.
danielebarchiesi@0 2582 if ($suggestions = theme_get_suggestions(arg(), 'page')) {
danielebarchiesi@0 2583 $variables['theme_hook_suggestions'] = $suggestions;
danielebarchiesi@0 2584 }
danielebarchiesi@0 2585 }
danielebarchiesi@0 2586
danielebarchiesi@0 2587 /**
danielebarchiesi@0 2588 * Process variables for page.tpl.php
danielebarchiesi@0 2589 *
danielebarchiesi@0 2590 * Perform final addition of variables before passing them into the template.
danielebarchiesi@0 2591 * To customize these variables, simply set them in an earlier step.
danielebarchiesi@0 2592 *
danielebarchiesi@0 2593 * @see template_preprocess_page()
danielebarchiesi@0 2594 * @see page.tpl.php
danielebarchiesi@0 2595 */
danielebarchiesi@0 2596 function template_process_page(&$variables) {
danielebarchiesi@0 2597 if (!isset($variables['breadcrumb'])) {
danielebarchiesi@0 2598 // Build the breadcrumb last, so as to increase the chance of being able to
danielebarchiesi@0 2599 // re-use the cache of an already rendered menu containing the active link
danielebarchiesi@0 2600 // for the current page.
danielebarchiesi@0 2601 // @see menu_tree_page_data()
danielebarchiesi@0 2602 $variables['breadcrumb'] = theme('breadcrumb', array('breadcrumb' => drupal_get_breadcrumb()));
danielebarchiesi@0 2603 }
danielebarchiesi@0 2604 if (!isset($variables['title'])) {
danielebarchiesi@0 2605 $variables['title'] = drupal_get_title();
danielebarchiesi@0 2606 }
danielebarchiesi@0 2607
danielebarchiesi@0 2608 // Generate messages last in order to capture as many as possible for the
danielebarchiesi@0 2609 // current page.
danielebarchiesi@0 2610 if (!isset($variables['messages'])) {
danielebarchiesi@0 2611 $variables['messages'] = $variables['show_messages'] ? theme('status_messages') : '';
danielebarchiesi@0 2612 }
danielebarchiesi@0 2613 }
danielebarchiesi@0 2614
danielebarchiesi@0 2615 /**
danielebarchiesi@0 2616 * Process variables for html.tpl.php
danielebarchiesi@0 2617 *
danielebarchiesi@0 2618 * Perform final addition and modification of variables before passing into
danielebarchiesi@0 2619 * the template. To customize these variables, call drupal_render() on elements
danielebarchiesi@0 2620 * in $variables['page'] during THEME_preprocess_page().
danielebarchiesi@0 2621 *
danielebarchiesi@0 2622 * @see template_preprocess_html()
danielebarchiesi@0 2623 * @see html.tpl.php
danielebarchiesi@0 2624 */
danielebarchiesi@0 2625 function template_process_html(&$variables) {
danielebarchiesi@0 2626 // Render page_top and page_bottom into top level variables.
danielebarchiesi@0 2627 $variables['page_top'] = drupal_render($variables['page']['page_top']);
danielebarchiesi@0 2628 $variables['page_bottom'] = drupal_render($variables['page']['page_bottom']);
danielebarchiesi@0 2629 // Place the rendered HTML for the page body into a top level variable.
danielebarchiesi@0 2630 $variables['page'] = $variables['page']['#children'];
danielebarchiesi@0 2631 $variables['page_bottom'] .= drupal_get_js('footer');
danielebarchiesi@0 2632
danielebarchiesi@0 2633 $variables['head'] = drupal_get_html_head();
danielebarchiesi@0 2634 $variables['css'] = drupal_add_css();
danielebarchiesi@0 2635 $variables['styles'] = drupal_get_css();
danielebarchiesi@0 2636 $variables['scripts'] = drupal_get_js();
danielebarchiesi@0 2637 }
danielebarchiesi@0 2638
danielebarchiesi@0 2639 /**
danielebarchiesi@0 2640 * Generate an array of suggestions from path arguments.
danielebarchiesi@0 2641 *
danielebarchiesi@0 2642 * This is typically called for adding to the 'theme_hook_suggestions' or
danielebarchiesi@0 2643 * 'classes_array' variables from within preprocess functions, when wanting to
danielebarchiesi@0 2644 * base the additional suggestions on the path of the current page.
danielebarchiesi@0 2645 *
danielebarchiesi@0 2646 * @param $args
danielebarchiesi@0 2647 * An array of path arguments, such as from function arg().
danielebarchiesi@0 2648 * @param $base
danielebarchiesi@0 2649 * A string identifying the base 'thing' from which more specific suggestions
danielebarchiesi@0 2650 * are derived. For example, 'page' or 'html'.
danielebarchiesi@0 2651 * @param $delimiter
danielebarchiesi@0 2652 * The string used to delimit increasingly specific information. The default
danielebarchiesi@0 2653 * of '__' is appropriate for theme hook suggestions. '-' is appropriate for
danielebarchiesi@0 2654 * extra classes.
danielebarchiesi@0 2655 *
danielebarchiesi@0 2656 * @return
danielebarchiesi@0 2657 * An array of suggestions, suitable for adding to
danielebarchiesi@0 2658 * $variables['theme_hook_suggestions'] within a preprocess function or to
danielebarchiesi@0 2659 * $variables['classes_array'] if the suggestions represent extra CSS classes.
danielebarchiesi@0 2660 */
danielebarchiesi@0 2661 function theme_get_suggestions($args, $base, $delimiter = '__') {
danielebarchiesi@0 2662
danielebarchiesi@0 2663 // Build a list of suggested theme hooks or body classes in order of
danielebarchiesi@0 2664 // specificity. One suggestion is made for every element of the current path,
danielebarchiesi@0 2665 // though numeric elements are not carried to subsequent suggestions. For
danielebarchiesi@0 2666 // example, for $base='page', http://www.example.com/node/1/edit would result
danielebarchiesi@0 2667 // in the following suggestions and body classes:
danielebarchiesi@0 2668 //
danielebarchiesi@0 2669 // page__node page-node
danielebarchiesi@0 2670 // page__node__% page-node-%
danielebarchiesi@0 2671 // page__node__1 page-node-1
danielebarchiesi@0 2672 // page__node__edit page-node-edit
danielebarchiesi@0 2673
danielebarchiesi@0 2674 $suggestions = array();
danielebarchiesi@0 2675 $prefix = $base;
danielebarchiesi@0 2676 foreach ($args as $arg) {
danielebarchiesi@0 2677 // Remove slashes or null per SA-CORE-2009-003 and change - (hyphen) to _
danielebarchiesi@0 2678 // (underscore).
danielebarchiesi@0 2679 //
danielebarchiesi@0 2680 // When we discover templates in @see drupal_find_theme_templates,
danielebarchiesi@0 2681 // hyphens (-) are converted to underscores (_) before the theme hook
danielebarchiesi@0 2682 // is registered. We do this because the hyphens used for delimiters
danielebarchiesi@0 2683 // in hook suggestions cannot be used in the function names of the
danielebarchiesi@0 2684 // associated preprocess functions. Any page templates designed to be used
danielebarchiesi@0 2685 // on paths that contain a hyphen are also registered with these hyphens
danielebarchiesi@0 2686 // converted to underscores so here we must convert any hyphens in path
danielebarchiesi@0 2687 // arguments to underscores here before fetching theme hook suggestions
danielebarchiesi@0 2688 // to ensure the templates are appropriately recognized.
danielebarchiesi@0 2689 $arg = str_replace(array("/", "\\", "\0", '-'), array('', '', '', '_'), $arg);
danielebarchiesi@0 2690 // The percent acts as a wildcard for numeric arguments since
danielebarchiesi@0 2691 // asterisks are not valid filename characters on many filesystems.
danielebarchiesi@0 2692 if (is_numeric($arg)) {
danielebarchiesi@0 2693 $suggestions[] = $prefix . $delimiter . '%';
danielebarchiesi@0 2694 }
danielebarchiesi@0 2695 $suggestions[] = $prefix . $delimiter . $arg;
danielebarchiesi@0 2696 if (!is_numeric($arg)) {
danielebarchiesi@0 2697 $prefix .= $delimiter . $arg;
danielebarchiesi@0 2698 }
danielebarchiesi@0 2699 }
danielebarchiesi@0 2700 if (drupal_is_front_page()) {
danielebarchiesi@0 2701 // Front templates should be based on root only, not prefixed arguments.
danielebarchiesi@0 2702 $suggestions[] = $base . $delimiter . 'front';
danielebarchiesi@0 2703 }
danielebarchiesi@0 2704
danielebarchiesi@0 2705 return $suggestions;
danielebarchiesi@0 2706 }
danielebarchiesi@0 2707
danielebarchiesi@0 2708 /**
danielebarchiesi@0 2709 * Process variables for maintenance-page.tpl.php.
danielebarchiesi@0 2710 *
danielebarchiesi@0 2711 * The variables array generated here is a mirror of
danielebarchiesi@0 2712 * template_preprocess_page(). This preprocessor will run its course when
danielebarchiesi@0 2713 * theme_maintenance_page() is invoked. An alternate template file of
danielebarchiesi@0 2714 * maintenance-page--offline.tpl.php can be used when the database is offline to
danielebarchiesi@0 2715 * hide errors and completely replace the content.
danielebarchiesi@0 2716 *
danielebarchiesi@0 2717 * The $variables array contains the following arguments:
danielebarchiesi@0 2718 * - $content
danielebarchiesi@0 2719 *
danielebarchiesi@0 2720 * @see maintenance-page.tpl.php
danielebarchiesi@0 2721 */
danielebarchiesi@0 2722 function template_preprocess_maintenance_page(&$variables) {
danielebarchiesi@0 2723 // Add favicon
danielebarchiesi@0 2724 if (theme_get_setting('toggle_favicon')) {
danielebarchiesi@0 2725 $favicon = theme_get_setting('favicon');
danielebarchiesi@0 2726 $type = theme_get_setting('favicon_mimetype');
danielebarchiesi@0 2727 drupal_add_html_head_link(array('rel' => 'shortcut icon', 'href' => drupal_strip_dangerous_protocols($favicon), 'type' => $type));
danielebarchiesi@0 2728 }
danielebarchiesi@0 2729
danielebarchiesi@0 2730 global $theme;
danielebarchiesi@0 2731 // Retrieve the theme data to list all available regions.
danielebarchiesi@0 2732 $theme_data = list_themes();
danielebarchiesi@0 2733 $regions = $theme_data[$theme]->info['regions'];
danielebarchiesi@0 2734
danielebarchiesi@0 2735 // Get all region content set with drupal_add_region_content().
danielebarchiesi@0 2736 foreach (array_keys($regions) as $region) {
danielebarchiesi@0 2737 // Assign region to a region variable.
danielebarchiesi@0 2738 $region_content = drupal_get_region_content($region);
danielebarchiesi@0 2739 isset($variables[$region]) ? $variables[$region] .= $region_content : $variables[$region] = $region_content;
danielebarchiesi@0 2740 }
danielebarchiesi@0 2741
danielebarchiesi@0 2742 // Setup layout variable.
danielebarchiesi@0 2743 $variables['layout'] = 'none';
danielebarchiesi@0 2744 if (!empty($variables['sidebar_first'])) {
danielebarchiesi@0 2745 $variables['layout'] = 'first';
danielebarchiesi@0 2746 }
danielebarchiesi@0 2747 if (!empty($variables['sidebar_second'])) {
danielebarchiesi@0 2748 $variables['layout'] = ($variables['layout'] == 'first') ? 'both' : 'second';
danielebarchiesi@0 2749 }
danielebarchiesi@0 2750
danielebarchiesi@0 2751 // Construct page title
danielebarchiesi@0 2752 if (drupal_get_title()) {
danielebarchiesi@0 2753 $head_title = array(
danielebarchiesi@0 2754 'title' => strip_tags(drupal_get_title()),
danielebarchiesi@0 2755 'name' => variable_get('site_name', 'Drupal'),
danielebarchiesi@0 2756 );
danielebarchiesi@0 2757 }
danielebarchiesi@0 2758 else {
danielebarchiesi@0 2759 $head_title = array('name' => variable_get('site_name', 'Drupal'));
danielebarchiesi@0 2760 if (variable_get('site_slogan', '')) {
danielebarchiesi@0 2761 $head_title['slogan'] = variable_get('site_slogan', '');
danielebarchiesi@0 2762 }
danielebarchiesi@0 2763 }
danielebarchiesi@0 2764
danielebarchiesi@0 2765 // set the default language if necessary
danielebarchiesi@0 2766 $language = isset($GLOBALS['language']) ? $GLOBALS['language'] : language_default();
danielebarchiesi@0 2767
danielebarchiesi@0 2768 $variables['head_title_array'] = $head_title;
danielebarchiesi@0 2769 $variables['head_title'] = implode(' | ', $head_title);
danielebarchiesi@0 2770 $variables['base_path'] = base_path();
danielebarchiesi@0 2771 $variables['front_page'] = url();
danielebarchiesi@0 2772 $variables['breadcrumb'] = '';
danielebarchiesi@0 2773 $variables['feed_icons'] = '';
danielebarchiesi@0 2774 $variables['help'] = '';
danielebarchiesi@0 2775 $variables['language'] = $language;
danielebarchiesi@0 2776 $variables['language']->dir = $language->direction ? 'rtl' : 'ltr';
danielebarchiesi@0 2777 $variables['logo'] = theme_get_setting('logo');
danielebarchiesi@0 2778 $variables['messages'] = $variables['show_messages'] ? theme('status_messages') : '';
danielebarchiesi@0 2779 $variables['main_menu'] = array();
danielebarchiesi@0 2780 $variables['secondary_menu'] = array();
danielebarchiesi@0 2781 $variables['site_name'] = (theme_get_setting('toggle_name') ? variable_get('site_name', 'Drupal') : '');
danielebarchiesi@0 2782 $variables['site_slogan'] = (theme_get_setting('toggle_slogan') ? variable_get('site_slogan', '') : '');
danielebarchiesi@0 2783 $variables['tabs'] = '';
danielebarchiesi@0 2784 $variables['title'] = drupal_get_title();
danielebarchiesi@0 2785
danielebarchiesi@0 2786 // Compile a list of classes that are going to be applied to the body element.
danielebarchiesi@0 2787 $variables['classes_array'][] = 'in-maintenance';
danielebarchiesi@0 2788 if (isset($variables['db_is_active']) && !$variables['db_is_active']) {
danielebarchiesi@0 2789 $variables['classes_array'][] = 'db-offline';
danielebarchiesi@0 2790 }
danielebarchiesi@0 2791 if ($variables['layout'] == 'both') {
danielebarchiesi@0 2792 $variables['classes_array'][] = 'two-sidebars';
danielebarchiesi@0 2793 }
danielebarchiesi@0 2794 elseif ($variables['layout'] == 'none') {
danielebarchiesi@0 2795 $variables['classes_array'][] = 'no-sidebars';
danielebarchiesi@0 2796 }
danielebarchiesi@0 2797 else {
danielebarchiesi@0 2798 $variables['classes_array'][] = 'one-sidebar sidebar-' . $variables['layout'];
danielebarchiesi@0 2799 }
danielebarchiesi@0 2800
danielebarchiesi@0 2801 // Dead databases will show error messages so supplying this template will
danielebarchiesi@0 2802 // allow themers to override the page and the content completely.
danielebarchiesi@0 2803 if (isset($variables['db_is_active']) && !$variables['db_is_active']) {
danielebarchiesi@0 2804 $variables['theme_hook_suggestion'] = 'maintenance_page__offline';
danielebarchiesi@0 2805 }
danielebarchiesi@0 2806 }
danielebarchiesi@0 2807
danielebarchiesi@0 2808 /**
danielebarchiesi@0 2809 * Theme process function for theme_maintenance_field().
danielebarchiesi@0 2810 *
danielebarchiesi@0 2811 * The variables array generated here is a mirror of template_process_html().
danielebarchiesi@0 2812 * This processor will run its course when theme_maintenance_page() is invoked.
danielebarchiesi@0 2813 *
danielebarchiesi@0 2814 * @see maintenance-page.tpl.php
danielebarchiesi@0 2815 * @see template_process_html()
danielebarchiesi@0 2816 */
danielebarchiesi@0 2817 function template_process_maintenance_page(&$variables) {
danielebarchiesi@0 2818 $variables['head'] = drupal_get_html_head();
danielebarchiesi@0 2819 $variables['css'] = drupal_add_css();
danielebarchiesi@0 2820 $variables['styles'] = drupal_get_css();
danielebarchiesi@0 2821 $variables['scripts'] = drupal_get_js();
danielebarchiesi@0 2822 }
danielebarchiesi@0 2823
danielebarchiesi@0 2824 /**
danielebarchiesi@0 2825 * Preprocess variables for region.tpl.php
danielebarchiesi@0 2826 *
danielebarchiesi@0 2827 * Prepares the values passed to the theme_region function to be passed into a
danielebarchiesi@0 2828 * pluggable template engine. Uses the region name to generate a template file
danielebarchiesi@0 2829 * suggestions. If none are found, the default region.tpl.php is used.
danielebarchiesi@0 2830 *
danielebarchiesi@0 2831 * @see drupal_region_class()
danielebarchiesi@0 2832 * @see region.tpl.php
danielebarchiesi@0 2833 */
danielebarchiesi@0 2834 function template_preprocess_region(&$variables) {
danielebarchiesi@0 2835 // Create the $content variable that templates expect.
danielebarchiesi@0 2836 $variables['content'] = $variables['elements']['#children'];
danielebarchiesi@0 2837 $variables['region'] = $variables['elements']['#region'];
danielebarchiesi@0 2838
danielebarchiesi@0 2839 $variables['classes_array'][] = drupal_region_class($variables['region']);
danielebarchiesi@0 2840 $variables['theme_hook_suggestions'][] = 'region__' . $variables['region'];
danielebarchiesi@0 2841 }
danielebarchiesi@0 2842
danielebarchiesi@0 2843 /**
danielebarchiesi@0 2844 * Preprocesses variables for theme_username().
danielebarchiesi@0 2845 *
danielebarchiesi@0 2846 * Modules that make any changes to variables like 'name' or 'extra' must insure
danielebarchiesi@0 2847 * that the final string is safe to include directly in the output by using
danielebarchiesi@0 2848 * check_plain() or filter_xss().
danielebarchiesi@0 2849 *
danielebarchiesi@0 2850 * @see template_process_username()
danielebarchiesi@0 2851 */
danielebarchiesi@0 2852 function template_preprocess_username(&$variables) {
danielebarchiesi@0 2853 $account = $variables['account'];
danielebarchiesi@0 2854
danielebarchiesi@0 2855 $variables['extra'] = '';
danielebarchiesi@0 2856 if (empty($account->uid)) {
danielebarchiesi@0 2857 $variables['uid'] = 0;
danielebarchiesi@0 2858 if (theme_get_setting('toggle_comment_user_verification')) {
danielebarchiesi@0 2859 $variables['extra'] = ' (' . t('not verified') . ')';
danielebarchiesi@0 2860 }
danielebarchiesi@0 2861 }
danielebarchiesi@0 2862 else {
danielebarchiesi@0 2863 $variables['uid'] = (int) $account->uid;
danielebarchiesi@0 2864 }
danielebarchiesi@0 2865
danielebarchiesi@0 2866 // Set the name to a formatted name that is safe for printing and
danielebarchiesi@0 2867 // that won't break tables by being too long. Keep an unshortened,
danielebarchiesi@0 2868 // unsanitized version, in case other preprocess functions want to implement
danielebarchiesi@0 2869 // their own shortening logic or add markup. If they do so, they must ensure
danielebarchiesi@0 2870 // that $variables['name'] is safe for printing.
danielebarchiesi@0 2871 $name = $variables['name_raw'] = format_username($account);
danielebarchiesi@0 2872 if (drupal_strlen($name) > 20) {
danielebarchiesi@0 2873 $name = drupal_substr($name, 0, 15) . '...';
danielebarchiesi@0 2874 }
danielebarchiesi@0 2875 $variables['name'] = check_plain($name);
danielebarchiesi@0 2876
danielebarchiesi@0 2877 $variables['profile_access'] = user_access('access user profiles');
danielebarchiesi@0 2878 $variables['link_attributes'] = array();
danielebarchiesi@0 2879 // Populate link path and attributes if appropriate.
danielebarchiesi@0 2880 if ($variables['uid'] && $variables['profile_access']) {
danielebarchiesi@0 2881 // We are linking to a local user.
danielebarchiesi@0 2882 $variables['link_attributes'] = array('title' => t('View user profile.'));
danielebarchiesi@0 2883 $variables['link_path'] = 'user/' . $variables['uid'];
danielebarchiesi@0 2884 }
danielebarchiesi@0 2885 elseif (!empty($account->homepage)) {
danielebarchiesi@0 2886 // Like the 'class' attribute, the 'rel' attribute can hold a
danielebarchiesi@0 2887 // space-separated set of values, so initialize it as an array to make it
danielebarchiesi@0 2888 // easier for other preprocess functions to append to it.
danielebarchiesi@0 2889 $variables['link_attributes'] = array('rel' => array('nofollow'));
danielebarchiesi@0 2890 $variables['link_path'] = $account->homepage;
danielebarchiesi@0 2891 $variables['homepage'] = $account->homepage;
danielebarchiesi@0 2892 }
danielebarchiesi@0 2893 // We do not want the l() function to check_plain() a second time.
danielebarchiesi@0 2894 $variables['link_options']['html'] = TRUE;
danielebarchiesi@0 2895 // Set a default class.
danielebarchiesi@0 2896 $variables['attributes_array'] = array('class' => array('username'));
danielebarchiesi@0 2897 }
danielebarchiesi@0 2898
danielebarchiesi@0 2899 /**
danielebarchiesi@0 2900 * Processes variables for theme_username().
danielebarchiesi@0 2901 *
danielebarchiesi@0 2902 * @see template_preprocess_username()
danielebarchiesi@0 2903 */
danielebarchiesi@0 2904 function template_process_username(&$variables) {
danielebarchiesi@0 2905 // Finalize the link_options array for passing to the l() function.
danielebarchiesi@0 2906 // This is done in the process phase so that attributes may be added by
danielebarchiesi@0 2907 // modules or the theme during the preprocess phase.
danielebarchiesi@0 2908 if (isset($variables['link_path'])) {
danielebarchiesi@0 2909 // $variables['attributes_array'] contains attributes that should be applied
danielebarchiesi@0 2910 // regardless of whether a link is being rendered or not.
danielebarchiesi@0 2911 // $variables['link_attributes'] contains attributes that should only be
danielebarchiesi@0 2912 // applied if a link is being rendered. Preprocess functions are encouraged
danielebarchiesi@0 2913 // to use the former unless they want to add attributes on the link only.
danielebarchiesi@0 2914 // If a link is being rendered, these need to be merged. Some attributes are
danielebarchiesi@0 2915 // themselves arrays, so the merging needs to be recursive.
danielebarchiesi@0 2916 $variables['link_options']['attributes'] = array_merge_recursive($variables['link_attributes'], $variables['attributes_array']);
danielebarchiesi@0 2917 }
danielebarchiesi@0 2918 }