PageRenderTime 208ms CodeModel.GetById 5ms app.highlight 181ms RepoModel.GetById 2ms app.codeStats 0ms

/includes/theme.inc

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