PageRenderTime 5ms CodeModel.GetById 4ms app.highlight 103ms RepoModel.GetById 1ms app.codeStats 2ms

/includes/theme.inc

https://bitbucket.org/antisocnet/drupal
Pascal | 2905 lines | 1614 code | 142 blank | 1149 comment | 245 complexity | fa82d6b59f8fb0e685a89e3d3cfd3e3a MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2
   3/**
   4 * @file
   5 * The theme system, which controls the output of Drupal.
   6 *
   7 * The theme system allows for nearly all output of the Drupal system to be
   8 * customized by user themes.
   9 */
  10
  11/**
  12 * @defgroup content_flags Content markers
  13 * @{
  14 * Markers used by theme_mark() and node_mark() to designate content.
  15 * @see theme_mark(), node_mark()
  16 */
  17
  18/**
  19 * Mark content as read.
  20 */
  21define('MARK_READ', 0);
  22
  23/**
  24 * Mark content as being new.
  25 */
  26define('MARK_NEW', 1);
  27
  28/**
  29 * Mark content as being updated.
  30 */
  31define('MARK_UPDATED', 2);
  32
  33/**
  34 * @} End of "Content markers".
  35 */
  36
  37/**
  38 * Determines if a theme is available to use.
  39 *
  40 * @param $theme
  41 *   Either the name of a theme or a full theme object.
  42 *
  43 * @return
  44 *   Boolean TRUE if the theme is enabled or is the site administration theme;
  45 *   FALSE otherwise.
  46 */
  47function drupal_theme_access($theme) {
  48  if (is_object($theme)) {
  49    return _drupal_theme_access($theme);
  50  }
  51  else {
  52    $themes = list_themes();
  53    return isset($themes[$theme]) && _drupal_theme_access($themes[$theme]);
  54  }
  55}
  56
  57/**
  58 * Helper function for determining access to a theme.
  59 *
  60 * @see drupal_theme_access()
  61 */
  62function _drupal_theme_access($theme) {
  63  $admin_theme = variable_get('admin_theme');
  64  return !empty($theme->status) || ($admin_theme && $theme->name == $admin_theme);
  65}
  66
  67/**
  68 * Initializes the theme system by loading the theme.
  69 */
  70function drupal_theme_initialize() {
  71  global $theme, $user, $theme_key;
  72
  73  // If $theme is already set, assume the others are set, too, and do nothing
  74  if (isset($theme)) {
  75    return;
  76  }
  77
  78  drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
  79  $themes = list_themes();
  80
  81  // Only select the user selected theme if it is available in the
  82  // list of themes that can be accessed.
  83  $theme = !empty($user->theme) && drupal_theme_access($user->theme) ? $user->theme : variable_get('theme_default', 'bartik');
  84
  85  // Allow modules to override the theme. Validation has already been performed
  86  // inside menu_get_custom_theme(), so we do not need to check it again here.
  87  $custom_theme = menu_get_custom_theme();
  88  $theme = !empty($custom_theme) ? $custom_theme : $theme;
  89
  90  // Store the identifier for retrieving theme settings with.
  91  $theme_key = $theme;
  92
  93  // Find all our ancestor themes and put them in an array.
  94  $base_theme = array();
  95  $ancestor = $theme;
  96  while ($ancestor && isset($themes[$ancestor]->base_theme)) {
  97    $ancestor = $themes[$ancestor]->base_theme;
  98    $base_theme[] = $themes[$ancestor];
  99  }
 100  _drupal_theme_initialize($themes[$theme], array_reverse($base_theme));
 101
 102  // Themes can have alter functions, so reset the drupal_alter() cache.
 103  drupal_static_reset('drupal_alter');
 104
 105  // Provide the page with information about the theme that's used, so that a
 106  // later Ajax request can be rendered using the same theme.
 107  // @see ajax_base_page_theme()
 108  $setting['ajaxPageState'] = array(
 109    'theme' => $theme_key,
 110    'theme_token' => drupal_get_token($theme_key),
 111  );
 112  drupal_add_js($setting, 'setting');
 113}
 114
 115/**
 116 * Initializes the theme system given already loaded information.
 117 *
 118 * This 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 * Gets the theme registry.
 240 *
 241 * @param $complete
 242 *   Optional boolean to indicate whether to return the complete theme registry
 243 *   array or an instance of the ThemeRegistry class. If TRUE, the complete
 244 *   theme registry array will be returned. This is useful if you want to
 245 *   foreach over the whole registry, use array_* functions or inspect it in a
 246 *   debugger. If FALSE, an instance of the ThemeRegistry class will be
 247 *   returned, this provides an ArrayObject which allows it to be accessed
 248 *   with array syntax and  isset(), and should be more lightweight
 249 *   than the full registry. Defaults to TRUE.
 250 *
 251 * @return
 252 *   The complete theme registry array, or an instance of the ThemeRegistry
 253 *   class.
 254 */
 255function theme_get_registry($complete = TRUE) {
 256  // Use the advanced drupal_static() pattern, since this is called very often.
 257  static $drupal_static_fast;
 258  if (!isset($drupal_static_fast)) {
 259    $drupal_static_fast['registry'] = &drupal_static('theme_get_registry');
 260  }
 261  $theme_registry = &$drupal_static_fast['registry'];
 262
 263  // Initialize the theme, if this is called early in the bootstrap, or after
 264  // static variables have been reset.
 265  if (!is_array($theme_registry)) {
 266    drupal_theme_initialize();
 267    $theme_registry = array();
 268  }
 269
 270  $key = (int) $complete;
 271
 272  if (!isset($theme_registry[$key])) {
 273    list($callback, $arguments) = _theme_registry_callback();
 274    if (!$complete) {
 275      $arguments[] = FALSE;
 276    }
 277    $theme_registry[$key] = call_user_func_array($callback, $arguments);
 278  }
 279
 280  return $theme_registry[$key];
 281}
 282
 283/**
 284 * Sets the callback that will be used by theme_get_registry().
 285 *
 286 * @param $callback
 287 *   The name of the callback function.
 288 * @param $arguments
 289 *   The arguments to pass to the function.
 290 */
 291function _theme_registry_callback($callback = NULL, array $arguments = array()) {
 292  static $stored;
 293  if (isset($callback)) {
 294    $stored = array($callback, $arguments);
 295  }
 296  return $stored;
 297}
 298
 299/**
 300 * Gets the theme_registry cache; if it doesn't exist, builds it.
 301 *
 302 * @param $theme
 303 *   The loaded $theme object as returned by list_themes().
 304 * @param $base_theme
 305 *   An array of loaded $theme objects representing the ancestor themes in
 306 *   oldest first order.
 307 * @param $theme_engine
 308 *   The name of the theme engine.
 309 * @param $complete
 310 *   Whether to load the complete theme registry or an instance of the
 311 *   ThemeRegistry class.
 312 *
 313 * @return
 314 *   The theme registry array, or an instance of the ThemeRegistry class.
 315 */
 316function _theme_load_registry($theme, $base_theme = NULL, $theme_engine = NULL, $complete = TRUE) {
 317  if ($complete) {
 318    // Check the theme registry cache; if it exists, use it.
 319    $cached = cache_get("theme_registry:$theme->name");
 320    if (isset($cached->data)) {
 321      $registry = $cached->data;
 322    }
 323    else {
 324      // If not, build one and cache it.
 325      $registry = _theme_build_registry($theme, $base_theme, $theme_engine);
 326      // Only persist this registry if all modules are loaded. This assures a
 327      // complete set of theme hooks.
 328      if (module_load_all(NULL)) {
 329        _theme_save_registry($theme, $registry);
 330      }
 331    }
 332    return $registry;
 333  }
 334  else {
 335    return new ThemeRegistry('theme_registry:runtime:' . $theme->name, 'cache');
 336  }
 337}
 338
 339/**
 340 * Writes the theme_registry cache into the database.
 341 */
 342function _theme_save_registry($theme, $registry) {
 343  cache_set("theme_registry:$theme->name", $registry);
 344}
 345
 346/**
 347 * Forces the system to rebuild the theme registry.
 348 *
 349 * This function should be called when modules are added to the system, or when
 350 * a dynamic system needs to add more theme hooks.
 351 */
 352function drupal_theme_rebuild() {
 353  drupal_static_reset('theme_get_registry');
 354  cache_clear_all('theme_registry', 'cache', TRUE);
 355}
 356
 357/**
 358 * Builds the run-time theme registry.
 359 *
 360 * Extends DrupalCacheArray to allow the theme registry to be accessed as a
 361 * complete registry, while internally caching only the parts of the registry
 362 * that are actually in use on the site. On cache misses the complete
 363 * theme registry is loaded and used to update the run-time cache.
 364 */
 365class ThemeRegistry Extends DrupalCacheArray {
 366
 367  /**
 368   * Whether the partial registry can be persisted to the cache.
 369   *
 370   * This is only allowed if all modules and the request method is GET. theme()
 371   * should be very rarely called on POST requests and this avoids polluting
 372   * the runtime cache.
 373   */
 374  protected $persistable;
 375
 376  /**
 377   * The complete theme registry array.
 378   */
 379  protected $completeRegistry;
 380
 381  function __construct($cid, $bin) {
 382    $this->cid = $cid;
 383    $this->bin = $bin;
 384    $this->persistable = module_load_all(NULL) && $_SERVER['REQUEST_METHOD'] == 'GET';
 385
 386    $data = array();
 387    if ($this->persistable && $cached = cache_get($this->cid, $this->bin)) {
 388      $data = $cached->data;
 389    }
 390    else {
 391      // If there is no runtime cache stored, fetch the full theme registry,
 392      // but then initialize each value to NULL. This allows offsetExists()
 393      // to function correctly on non-registered theme hooks without triggering
 394      // a call to resolveCacheMiss().
 395      $data = $this->initializeRegistry();
 396      if ($this->persistable) {
 397        $this->set($data);
 398      }
 399    }
 400    $this->storage = $data;
 401  }
 402
 403  /**
 404   * Initializes the full theme registry.
 405   *
 406   * @return
 407   *   An array with the keys of the full theme registry, but the values
 408   *   initialized to NULL.
 409   */
 410  function initializeRegistry() {
 411    $this->completeRegistry = theme_get_registry();
 412
 413    return array_fill_keys(array_keys($this->completeRegistry), NULL);
 414  }
 415
 416  public function offsetExists($offset) {
 417    // Since the theme registry allows for theme hooks to be requested that
 418    // are not registered, just check the existence of the key in the registry.
 419    // Use array_key_exists() here since a NULL value indicates that the theme
 420    // hook exists but has not yet been requested.
 421    return array_key_exists($offset, $this->storage);
 422  }
 423
 424  public function offsetGet($offset) {
 425    // If the offset is set but empty, it is a registered theme hook that has
 426    // not yet been requested. Offsets that do not exist at all were not
 427    // registered in hook_theme().
 428    if (isset($this->storage[$offset])) {
 429      return $this->storage[$offset];
 430    }
 431    elseif (array_key_exists($offset, $this->storage)) {
 432      return $this->resolveCacheMiss($offset);
 433    }
 434  }
 435
 436  public function resolveCacheMiss($offset) {
 437    if (!isset($this->completeRegistry)) {
 438      $this->completeRegistry = theme_get_registry();
 439    }
 440    $this->storage[$offset] = $this->completeRegistry[$offset];
 441    if ($this->persistable) {
 442      $this->persist($offset);
 443    }
 444    return $this->storage[$offset];
 445  }
 446
 447  public function set($data, $lock = TRUE) {
 448    $lock_name = $this->cid . ':' . $this->bin;
 449    if (!$lock || lock_acquire($lock_name)) {
 450      if ($cached = cache_get($this->cid, $this->bin)) {
 451        // Use array merge instead of union so that filled in values in $data
 452        // overwrite empty values in the current cache.
 453        $data = array_merge($cached->data, $data);
 454      }
 455      else {
 456        $registry = $this->initializeRegistry();
 457        $data = array_merge($registry, $data);
 458      }
 459      cache_set($this->cid, $data, $this->bin);
 460      if ($lock) {
 461        lock_release($lock_name);
 462      }
 463    }
 464  }
 465}
 466
 467/**
 468 * Process a single implementation of hook_theme().
 469 *
 470 * @param $cache
 471 *   The theme registry that will eventually be cached; It is an associative
 472 *   array keyed by theme hooks, whose values are associative arrays describing
 473 *   the hook:
 474 *   - 'type': The passed-in $type.
 475 *   - 'theme path': The passed-in $path.
 476 *   - 'function': The name of the function generating output for this theme
 477 *     hook. Either defined explicitly in hook_theme() or, if neither 'function'
 478 *     nor 'template' is defined, then the default theme function name is used.
 479 *     The default theme function name is the theme hook prefixed by either
 480 *     'theme_' for modules or '$name_' for everything else. If 'function' is
 481 *     defined, 'template' is not used.
 482 *   - 'template': The filename of the template generating output for this
 483 *     theme hook. The template is in the directory defined by the 'path' key of
 484 *     hook_theme() or defaults to $path.
 485 *   - 'variables': The variables for this theme hook as defined in
 486 *     hook_theme(). If there is more than one implementation and 'variables' is
 487 *     not specified in a later one, then the previous definition is kept.
 488 *   - 'render element': The renderable element for this theme hook as defined
 489 *     in hook_theme(). If there is more than one implementation and
 490 *     'render element' is not specified in a later one, then the previous
 491 *     definition is kept.
 492 *   - 'preprocess functions': See theme() for detailed documentation.
 493 *   - 'process functions': See theme() for detailed documentation.
 494 * @param $name
 495 *   The name of the module, theme engine, base theme engine, theme or base
 496 *   theme implementing hook_theme().
 497 * @param $type
 498 *   One of 'module', 'theme_engine', 'base_theme_engine', 'theme', or
 499 *   'base_theme'. Unlike regular hooks that can only be implemented by modules,
 500 *   each of these can implement hook_theme(). _theme_process_registry() is
 501 *   called in aforementioned order and new entries override older ones. For
 502 *   example, if a theme hook is both defined by a module and a theme, then the
 503 *   definition in the theme will be used.
 504 * @param $theme
 505 *   The loaded $theme object as returned from list_themes().
 506 * @param $path
 507 *   The directory where $name is. For example, modules/system or
 508 *   themes/bartik.
 509 *
 510 * @see theme()
 511 * @see _theme_process_registry()
 512 * @see hook_theme()
 513 * @see list_themes()
 514 */
 515function _theme_process_registry(&$cache, $name, $type, $theme, $path) {
 516  $result = array();
 517
 518  // Processor functions work in two distinct phases with the process
 519  // functions always being executed after the preprocess functions.
 520  $variable_process_phases = array(
 521    'preprocess functions' => 'preprocess',
 522    'process functions'    => 'process',
 523  );
 524
 525  $hook_defaults = array(
 526    'variables' => TRUE,
 527    'render element' => TRUE,
 528    'pattern' => TRUE,
 529    'base hook' => TRUE,
 530  );
 531
 532  // Invoke the hook_theme() implementation, process what is returned, and
 533  // merge it into $cache.
 534  $function = $name . '_theme';
 535  if (function_exists($function)) {
 536    $result = $function($cache, $type, $theme, $path);
 537    foreach ($result as $hook => $info) {
 538      // When a theme or engine overrides a module's theme function
 539      // $result[$hook] will only contain key/value pairs for information being
 540      // overridden.  Pull the rest of the information from what was defined by
 541      // an earlier hook.
 542
 543      // Fill in the type and path of the module, theme, or engine that
 544      // implements this theme function.
 545      $result[$hook]['type'] = $type;
 546      $result[$hook]['theme path'] = $path;
 547
 548      // If function and file are omitted, default to standard naming
 549      // conventions.
 550      if (!isset($info['template']) && !isset($info['function'])) {
 551        $result[$hook]['function'] = ($type == 'module' ? 'theme_' : $name . '_') . $hook;
 552      }
 553
 554      if (isset($cache[$hook]['includes'])) {
 555        $result[$hook]['includes'] = $cache[$hook]['includes'];
 556      }
 557
 558      // If the theme implementation defines a file, then also use the path
 559      // that it defined. Otherwise use the default path. This allows
 560      // system.module to declare theme functions on behalf of core .include
 561      // files.
 562      if (isset($info['file'])) {
 563        $include_file = isset($info['path']) ? $info['path'] : $path;
 564        $include_file .= '/' . $info['file'];
 565        include_once DRUPAL_ROOT . '/' . $include_file;
 566        $result[$hook]['includes'][] = $include_file;
 567      }
 568
 569      // If the default keys are not set, use the default values registered
 570      // by the module.
 571      if (isset($cache[$hook])) {
 572        $result[$hook] += array_intersect_key($cache[$hook], $hook_defaults);
 573      }
 574
 575      // The following apply only to theming hooks implemented as templates.
 576      if (isset($info['template'])) {
 577        // Prepend the current theming path when none is set.
 578        if (!isset($info['path'])) {
 579          $result[$hook]['template'] = $path . '/' . $info['template'];
 580        }
 581      }
 582
 583      // Allow variable processors for all theming hooks, whether the hook is
 584      // implemented as a template or as a function.
 585      foreach ($variable_process_phases as $phase_key => $phase) {
 586        // Check for existing variable processors. Ensure arrayness.
 587        if (!isset($info[$phase_key]) || !is_array($info[$phase_key])) {
 588          $info[$phase_key] = array();
 589          $prefixes = array();
 590          if ($type == 'module') {
 591            // Default variable processor prefix.
 592            $prefixes[] = 'template';
 593            // Add all modules so they can intervene with their own variable
 594            // processors. This allows them to provide variable processors even
 595            // if they are not the owner of the current hook.
 596            $prefixes += module_list();
 597          }
 598          elseif ($type == 'theme_engine' || $type == 'base_theme_engine') {
 599            // Theme engines get an extra set that come before the normally
 600            // named variable processors.
 601            $prefixes[] = $name . '_engine';
 602            // The theme engine registers on behalf of the theme using the
 603            // theme's name.
 604            $prefixes[] = $theme;
 605          }
 606          else {
 607            // This applies when the theme manually registers their own variable
 608            // processors.
 609            $prefixes[] = $name;
 610          }
 611          foreach ($prefixes as $prefix) {
 612            // Only use non-hook-specific variable processors for theming hooks
 613            // implemented as templates. See theme().
 614            if (isset($info['template']) && function_exists($prefix . '_' . $phase)) {
 615              $info[$phase_key][] = $prefix . '_' . $phase;
 616            }
 617            if (function_exists($prefix . '_' . $phase . '_' . $hook)) {
 618              $info[$phase_key][] = $prefix . '_' . $phase . '_' . $hook;
 619            }
 620          }
 621        }
 622        // Check for the override flag and prevent the cached variable
 623        // processors from being used. This allows themes or theme engines to
 624        // remove variable processors set earlier in the registry build.
 625        if (!empty($info['override ' . $phase_key])) {
 626          // Flag not needed inside the registry.
 627          unset($result[$hook]['override ' . $phase_key]);
 628        }
 629        elseif (isset($cache[$hook][$phase_key]) && is_array($cache[$hook][$phase_key])) {
 630          $info[$phase_key] = array_merge($cache[$hook][$phase_key], $info[$phase_key]);
 631        }
 632        $result[$hook][$phase_key] = $info[$phase_key];
 633      }
 634    }
 635
 636    // Merge the newly created theme hooks into the existing cache.
 637    $cache = $result + $cache;
 638  }
 639
 640  // Let themes have variable processors even if they didn't register a
 641  // template.
 642  if ($type == 'theme' || $type == 'base_theme') {
 643    foreach ($cache as $hook => $info) {
 644      // Check only if not registered by the theme or engine.
 645      if (empty($result[$hook])) {
 646        foreach ($variable_process_phases as $phase_key => $phase) {
 647          if (!isset($info[$phase_key])) {
 648            $cache[$hook][$phase_key] = array();
 649          }
 650          // Only use non-hook-specific variable processors for theming hooks
 651          // implemented as templates. See theme().
 652          if (isset($info['template']) && function_exists($name . '_' . $phase)) {
 653            $cache[$hook][$phase_key][] = $name . '_' . $phase;
 654          }
 655          if (function_exists($name . '_' . $phase . '_' . $hook)) {
 656            $cache[$hook][$phase_key][] = $name . '_' . $phase . '_' . $hook;
 657            $cache[$hook]['theme path'] = $path;
 658          }
 659          // Ensure uniqueness.
 660          $cache[$hook][$phase_key] = array_unique($cache[$hook][$phase_key]);
 661        }
 662      }
 663    }
 664  }
 665}
 666
 667/**
 668 * Builds the theme registry cache.
 669 *
 670 * @param $theme
 671 *   The loaded $theme object as returned by list_themes().
 672 * @param $base_theme
 673 *   An array of loaded $theme objects representing the ancestor themes in
 674 *   oldest first order.
 675 * @param $theme_engine
 676 *   The name of the theme engine.
 677 */
 678function _theme_build_registry($theme, $base_theme, $theme_engine) {
 679  $cache = array();
 680  // First, process the theme hooks advertised by modules. This will
 681  // serve as the basic registry. Since the list of enabled modules is the same
 682  // regardless of the theme used, this is cached in its own entry to save
 683  // building it for every theme.
 684  if ($cached = cache_get('theme_registry:build:modules')) {
 685    $cache = $cached->data;
 686  }
 687  else {
 688    foreach (module_implements('theme') as $module) {
 689      _theme_process_registry($cache, $module, 'module', $module, drupal_get_path('module', $module));
 690    }
 691    // Only cache this registry if all modules are loaded.
 692    if (module_load_all(NULL)) {
 693      cache_set('theme_registry:build:modules', $cache);
 694    }
 695  }
 696
 697  // Process each base theme.
 698  foreach ($base_theme as $base) {
 699    // If the base theme uses a theme engine, process its hooks.
 700    $base_path = dirname($base->filename);
 701    if ($theme_engine) {
 702      _theme_process_registry($cache, $theme_engine, 'base_theme_engine', $base->name, $base_path);
 703    }
 704    _theme_process_registry($cache, $base->name, 'base_theme', $base->name, $base_path);
 705  }
 706
 707  // And then the same thing, but for the theme.
 708  if ($theme_engine) {
 709    _theme_process_registry($cache, $theme_engine, 'theme_engine', $theme->name, dirname($theme->filename));
 710  }
 711
 712  // Finally, hooks provided by the theme itself.
 713  _theme_process_registry($cache, $theme->name, 'theme', $theme->name, dirname($theme->filename));
 714
 715  // Let modules alter the registry.
 716  drupal_alter('theme_registry', $cache);
 717
 718  // Optimize the registry to not have empty arrays for functions.
 719  foreach ($cache as $hook => $info) {
 720    foreach (array('preprocess functions', 'process functions') as $phase) {
 721      if (empty($info[$phase])) {
 722        unset($cache[$hook][$phase]);
 723      }
 724    }
 725  }
 726  return $cache;
 727}
 728
 729/**
 730 * Returns a list of all currently available themes.
 731 *
 732 * Retrieved from the database, if available and the site is not in maintenance
 733 * mode; otherwise compiled freshly from the filesystem.
 734 *
 735 * @param $refresh
 736 *   Whether to reload the list of themes from the database. Defaults to FALSE.
 737 *
 738 * @return
 739 *   An associative array of the currently available themes. The keys are the
 740 *   themes' machine names and the values are objects having the following
 741 *   properties:
 742 *   - filename: The filepath and name of the .info file.
 743 *   - name: The machine name of the theme.
 744 *   - status: 1 for enabled, 0 for disabled themes.
 745 *   - info: The contents of the .info file.
 746 *   - stylesheets: A two dimensional array, using the first key for the
 747 *     media attribute (e.g. 'all'), the second for the name of the file
 748 *     (e.g. style.css). The value is a complete filepath (e.g.
 749 *     themes/bartik/style.css). Not set if no stylesheets are defined in the
 750 *     .info file.
 751 *   - scripts: An associative array of JavaScripts, using the filename as key
 752 *     and the complete filepath as value. Not set if no scripts are defined in
 753 *     the .info file.
 754 *   - prefix: The base theme engine prefix.
 755 *   - engine: The machine name of the theme engine.
 756 *   - base_theme: If this is a sub-theme, the machine name of the base theme
 757 *     defined in the .info file. Otherwise, the element is not set.
 758 *   - base_themes: If this is a sub-theme, an associative array of the
 759 *     base-theme ancestors of this theme, starting with this theme's base
 760 *     theme, then the base theme's own base theme, etc. Each entry has an
 761 *     array key equal to the theme's machine name, and a value equal to the
 762 *     human-readable theme name; if a theme with matching machine name does
 763 *     not exist in the system, the value will instead be NULL (and since the
 764 *     system would not know whether that theme itself has a base theme, that
 765 *     will end the array of base themes). This is not set if the theme is not
 766 *     a sub-theme.
 767 *   - sub_themes: An associative array of themes on the system that are
 768 *     either direct sub-themes (that is, they declare this theme to be
 769 *     their base theme), direct sub-themes of sub-themes, etc. The keys are
 770 *     the themes' machine names, and the values are the themes' human-readable
 771 *     names. This element is not set if there are no themes on the system that
 772 *     declare this theme as their base theme.
 773*/
 774function list_themes($refresh = FALSE) {
 775  $list = &drupal_static(__FUNCTION__, array());
 776
 777  if ($refresh) {
 778    $list = array();
 779    system_list_reset();
 780  }
 781
 782  if (empty($list)) {
 783    $list = array();
 784    $themes = array();
 785    // Extract from the database only when it is available.
 786    // Also check that the site is not in the middle of an install or update.
 787    if (!defined('MAINTENANCE_MODE')) {
 788      try {
 789        $themes = system_list('theme');
 790      }
 791      catch (Exception $e) {
 792        // If the database is not available, rebuild the theme data.
 793        $themes = _system_rebuild_theme_data();
 794      }
 795    }
 796    else {
 797      // Scan the installation when the database should not be read.
 798      $themes = _system_rebuild_theme_data();
 799    }
 800
 801    foreach ($themes as $theme) {
 802      foreach ($theme->info['stylesheets'] as $media => $stylesheets) {
 803        foreach ($stylesheets as $stylesheet => $path) {
 804          $theme->stylesheets[$media][$stylesheet] = $path;
 805        }
 806      }
 807      foreach ($theme->info['scripts'] as $script => $path) {
 808        $theme->scripts[$script] = $path;
 809      }
 810      if (isset($theme->info['engine'])) {
 811        $theme->engine = $theme->info['engine'];
 812      }
 813      if (isset($theme->info['base theme'])) {
 814        $theme->base_theme = $theme->info['base theme'];
 815      }
 816      // Status is normally retrieved from the database. Add zero values when
 817      // read from the installation directory to prevent notices.
 818      if (!isset($theme->status)) {
 819        $theme->status = 0;
 820      }
 821      $list[$theme->name] = $theme;
 822    }
 823  }
 824
 825  return $list;
 826}
 827
 828/**
 829 * Find all the base themes for the specified theme.
 830 *
 831 * Themes can inherit templates and function implementations from earlier themes.
 832 *
 833 * @param $themes
 834 *   An array of available themes.
 835 * @param $key
 836 *   The name of the theme whose base we are looking for.
 837 * @param $used_keys
 838 *   A recursion parameter preventing endless loops.
 839 * @return
 840 *   Returns an array of all of the theme's ancestors; the first element's value
 841 *   will be NULL if an error occurred.
 842 */
 843function drupal_find_base_themes($themes, $key, $used_keys = array()) {
 844  $base_key = $themes[$key]->info['base theme'];
 845  // Does the base theme exist?
 846  if (!isset($themes[$base_key])) {
 847    return array($base_key => NULL);
 848  }
 849
 850  $current_base_theme = array($base_key => $themes[$base_key]->info['name']);
 851
 852  // Is the base theme itself a child of another theme?
 853  if (isset($themes[$base_key]->info['base theme'])) {
 854    // Do we already know the base themes of this theme?
 855    if (isset($themes[$base_key]->base_themes)) {
 856      return $themes[$base_key]->base_themes + $current_base_theme;
 857    }
 858    // Prevent loops.
 859    if (!empty($used_keys[$base_key])) {
 860      return array($base_key => NULL);
 861    }
 862    $used_keys[$base_key] = TRUE;
 863    return drupal_find_base_themes($themes, $base_key, $used_keys) + $current_base_theme;
 864  }
 865  // If we get here, then this is our parent theme.
 866  return $current_base_theme;
 867}
 868
 869/**
 870 * Generates themed output.
 871 *
 872 * All requests for themed output must go through this function. It examines
 873 * the request and routes it to the appropriate
 874 * @link themeable theme function or template @endlink, by checking the theme
 875 * registry.
 876 *
 877 * Most commonly, the first argument to this function is the name of the theme
 878 * hook. For instance, to theme a taxonomy term, the theme hook name is
 879 * 'taxonomy_term'. Modules register theme hooks within a hook_theme()
 880 * implementation and provide a default implementation via a function named
 881 * theme_HOOK() (e.g., theme_taxonomy_term()) or via a template file named
 882 * according to the value of the 'template' key registered with the theme hook
 883 * (see hook_theme() for details). Default templates are implemented with the
 884 * PHPTemplate rendering engine and are named the same as the theme hook, with
 885 * underscores changed to hyphens, so for the 'taxonomy_term' theme hook, the
 886 * default template is 'taxonomy-term.tpl.php'.
 887 *
 888 * Themes may also register new theme hooks within a hook_theme()
 889 * implementation, but it is more common for themes to override default
 890 * implementations provided by modules than to register entirely new theme
 891 * hooks. Themes can override a default implementation by implementing a
 892 * function named THEME_HOOK() (for example, the 'bartik' theme overrides the
 893 * default implementation of the 'menu_tree' theme hook by implementing a
 894 * bartik_menu_tree() function), or by adding a template file within its folder
 895 * structure that follows the template naming structure used by the theme's
 896 * rendering engine (for example, since the Bartik theme uses the PHPTemplate
 897 * rendering engine, it overrides the default implementation of the 'page' theme
 898 * hook by containing a 'page.tpl.php' file within its folder structure).
 899 *
 900 * If the implementation is a template file, several functions are called
 901 * before the template file is invoked, to modify the $variables array. These
 902 * fall into the "preprocessing" phase and the "processing" phase, and are
 903 * executed (if they exist), in the following order (note that in the following
 904 * list, HOOK indicates the theme hook name, MODULE indicates a module name,
 905 * THEME indicates a theme name, and ENGINE indicates a theme engine name):
 906 * - template_preprocess(&$variables, $hook): Creates a default set of
 907 *   variables for all theme hooks with template implementations.
 908 * - template_preprocess_HOOK(&$variables): Should be implemented by the module
 909 *   that registers the theme hook, to set up default variables.
 910 * - MODULE_preprocess(&$variables, $hook): hook_preprocess() is invoked on all
 911 *   implementing modules.
 912 * - MODULE_preprocess_HOOK(&$variables): hook_preprocess_HOOK() is invoked on
 913 *   all implementing modules, so that modules that didn't define the theme
 914 *   hook can alter the variables.
 915 * - ENGINE_engine_preprocess(&$variables, $hook): Allows the theme engine to
 916 *   set necessary variables for all theme hooks with template implementations.
 917 * - ENGINE_engine_preprocess_HOOK(&$variables): Allows the theme engine to set
 918 *   necessary variables for the particular theme hook.
 919 * - THEME_preprocess(&$variables, $hook): Allows the theme to set necessary
 920 *   variables for all theme hooks with template implementations.
 921 * - THEME_preprocess_HOOK(&$variables): Allows the theme to set necessary
 922 *   variables specific to the particular theme hook.
 923 * - template_process(&$variables, $hook): Creates an additional set of default
 924 *   variables for all theme hooks with template implementations. The variables
 925 *   created in this function are derived from ones created by
 926 *   template_preprocess(), but potentially altered by the other preprocess
 927 *   functions listed above. For example, any preprocess function can add to or
 928 *   modify the $variables['attributes_array'] variable, and after all of them
 929 *   have finished executing, template_process() flattens it into a
 930 *   $variables['attributes'] string for convenient use by templates.
 931 * - template_process_HOOK(&$variables): Should be implemented by the module
 932 *   that registers the theme hook, if it needs to perform additional variable
 933 *   processing after all preprocess functions have finished.
 934 * - MODULE_process(&$variables, $hook): hook_process() is invoked on all
 935 *   implementing modules.
 936 * - MODULE_process_HOOK(&$variables): hook_process_HOOK() is invoked on
 937 *   on all implementing modules, so that modules that didn't define the theme
 938 *   hook can alter the variables.
 939 * - ENGINE_engine_process(&$variables, $hook): Allows the theme engine to
 940 *   process variables for all theme hooks with template implementations.
 941 * - ENGINE_engine_process_HOOK(&$variables): Allows the theme engine to process
 942 *   the variables specific to the theme hook.
 943 * - THEME_process(&$variables, $hook):  Allows the theme to process the
 944 *   variables for all theme hooks with template implementations.
 945 * - THEME_process_HOOK(&$variables):  Allows the theme to process the
 946 *   variables specific to the theme hook.
 947 *
 948 * If the implementation is a function, only the theme-hook-specific preprocess
 949 * and process functions (the ones ending in _HOOK) are called from the
 950 * list above. This is because theme hooks with function implementations
 951 * need to be fast, and calling the non-theme-hook-specific preprocess and
 952 * process functions for them would incur a noticeable performance penalty.
 953 *
 954 * There are two special variables that these preprocess and process functions
 955 * can set: 'theme_hook_suggestion' and 'theme_hook_suggestions'. These will be
 956 * merged together to form a list of 'suggested' alternate theme hooks to use,
 957 * in reverse order of priority. theme_hook_suggestion will always be a higher
 958 * priority than items in theme_hook_suggestions. theme() will use the
 959 * highest priority implementation that exists. If none exists, theme() will
 960 * use the implementation for the theme hook it was called with. These
 961 * suggestions are similar to and are used for similar reasons as calling
 962 * theme() with an array as the $hook parameter (see below). The difference
 963 * is whether the suggestions are determined by the code that calls theme() or
 964 * by a preprocess or process function.
 965 *
 966 * @param $hook
 967 *   The name of the theme hook to call. If the name contains a
 968 *   double-underscore ('__') and there isn't an implementation for the full
 969 *   name, the part before the '__' is checked. This allows a fallback to a
 970 *   more generic implementation. For example, if theme('links__node', ...) is
 971 *   called, but there is no implementation of that theme hook, then the
 972 *   'links' implementation is used. This process is iterative, so if
 973 *   theme('links__contextual__node', ...) is called, theme() checks for the
 974 *   following implementations, and uses the first one that exists:
 975 *   - links__contextual__node
 976 *   - links__contextual
 977 *   - links
 978 *   This allows themes to create specific theme implementations for named
 979 *   objects and contexts of otherwise generic theme hooks. The $hook parameter
 980 *   may also be an array, in which case the first theme hook that has an
 981 *   implementation is used. This allows for the code that calls theme() to
 982 *   explicitly specify the fallback order in a situation where using the '__'
 983 *   convention is not desired or is insufficient.
 984 * @param $variables
 985 *   An associative array of variables to merge with defaults from the theme
 986 *   registry, pass to preprocess and process functions for modification, and
 987 *   finally, pass to the function or template implementing the theme hook.
 988 *   Alternatively, this can be a renderable array, in which case, its
 989 *   properties are mapped to variables expected by the theme hook
 990 *   implementations.
 991 *
 992 * @return
 993 *   An HTML string representing the themed output.
 994 *
 995 * @see themeable
 996 * @see hook_theme()
 997 * @see template_preprocess()
 998 * @see template_process()
 999 */
1000function theme($hook, $variables = array()) {
1001  // If called before all modules are loaded, we do not necessarily have a full
1002  // theme registry to work with, and therefore cannot process the theme
1003  // request properly. See also _theme_load_registry().
1004  if (!module_load_all(NULL) && !defined('MAINTENANCE_MODE')) {
1005    throw new Exception(t('theme() may not be called until all modules are loaded.'));
1006  }
1007
1008  $hooks = theme_get_registry(FALSE);
1009
1010  // If an array of hook candidates were passed, use the first one that has an
1011  // implementation.
1012  if (is_array($hook)) {
1013    foreach ($hook as $candidate) {
1014      if (isset($hooks[$candidate])) {
1015        break;
1016      }
1017    }
1018    $hook = $candidate;
1019  }
1020
1021  // If there's no implementation, check for more generic fallbacks. If there's
1022  // still no implementation, log an error and return an empty string.
1023  if (!isset($hooks[$hook])) {
1024    // Iteratively strip everything after the last '__' delimiter, until an
1025    // implementation is found.
1026    while ($pos = strrpos($hook, '__')) {
1027      $hook = substr($hook, 0, $pos);
1028      if (isset($hooks[$hook])) {
1029        break;
1030      }
1031    }
1032    if (!isset($hooks[$hook])) {
1033      // Only log a message when not trying theme suggestions ($hook being an
1034      // array).
1035      if (!isset($candidate)) {
1036        watchdog('theme', 'Theme hook %hook not found.', array('%hook' => $hook), WATCHDOG_WARNING);
1037      }
1038      return '';
1039    }
1040  }
1041
1042  $info = $hooks[$hook];
1043  global $theme_path;
1044  $temp = $theme_path;
1045  // point path_to_theme() to the currently used theme path:
1046  $theme_path = $info['theme path'];
1047
1048  // Include a file if the theme function or variable processor is held
1049  // elsewhere.
1050  if (!empty($info['includes'])) {
1051    foreach ($info['includes'] as $include_file) {
1052      include_once DRUPAL_ROOT . '/' . $include_file;
1053    }
1054  }
1055
1056  // If a renderable array is passed as $variables, then set $variables to
1057  // the arguments expected by the theme function.
1058  if (isset($variables['#theme']) || isset($variables['#theme_wrappers'])) {
1059    $element = $variables;
1060    $variables = array();
1061    if (isset($info['variables'])) {
1062      foreach (array_keys($info['variables']) as $name) {
1063        if (isset($element["#$name"])) {
1064          $variables[$name] = $element["#$name"];
1065        }
1066      }
1067    }
1068    else {
1069      $variables[$info['render element']] = $element;
1070    }
1071  }
1072
1073  // Merge in argument defaults.
1074  if (!empty($info['variables'])) {
1075    $variables += $info['variables'];
1076  }
1077  elseif (!empty($info['render element'])) {
1078    $variables += array($info['render element'] => array());
1079  }
1080
1081  // Invoke the variable processors, if any. The processors may specify
1082  // alternate suggestions for which hook's template/function to use. If the
1083  // hook is a suggestion of a base hook, invoke the variable processors of
1084  // the base hook, but retain the suggestion as a high priority suggestion to
1085  // be used unless overridden by a variable processor function.
1086  if (isset($info['base hook'])) {
1087    $base_hook = $info['base hook'];
1088    $base_hook_info = $hooks[$base_hook];
1089    // Include files required by the base hook, since its variable processors
1090    // might reside there.
1091    if (!empty($base_hook_info['includes'])) {
1092      foreach ($base_hook_info['includes'] as $include_file) {
1093        include_once DRUPAL_ROOT . '/' . $include_file;
1094      }
1095    }
1096    if (isset($base_hook_info['preprocess functions']) || isset($base_hook_info['process functions'])) {
1097      $variables['theme_hook_suggestion'] = $hook;
1098      $hook = $base_hook;
1099      $info = $base_hook_info;
1100    }
1101  }
1102  if (isset($info['preprocess functions']) || isset($info['process functions'])) {
1103    $variables['theme_hook_suggestions'] = array();
1104    foreach (array('preprocess functions', 'process functions') as $phase) {
1105      if (!empty($info[$phase])) {
1106        foreach ($info[$phase] as $processor_function) {
1107          if (function_exists($processor_function)) {
1108            // We don't want a poorly behaved process function changing $hook.
1109            $hook_clone = $hook;
1110            $processor_function($variables, $hook_clone);
1111          }
1112        }
1113      }
1114    }
1115    // If the preprocess/process functions specified hook suggestions, and the
1116    // suggestion exists in the theme registry, use it instead of the hook that
1117    // theme() was called with. This allows the preprocess/process step to
1118    // route to a more specific theme hook. For example, a function may call
1119    // theme('node', ...), but a preprocess function can add 'node__article' as
1120    // a suggestion, enabling a theme to have an alternate template file for
1121    // article nodes. Suggestions are checked in the following order:
1122    // - The 'theme_hook_suggestion' variable is checked first. It overrides
1123    //   all others.
1124    // - The 'theme_hook_suggestions' variable is checked in FILO order, so the
1125    //   last suggestion added to the array takes precedence over suggestions
1126    //   added earlier.
1127    $suggestions = array();
1128    if (!empty($variables['theme_hook_suggestions'])) {
1129      $suggestions = $variables['theme_hook_suggestions'];
1130    }
1131    if (!empty($variables['theme_hook_suggestion'])) {
1132      $suggestions[] = $variables['theme_hook_suggestion'];
1133    }
1134    foreach (array_reverse($suggestions) as $suggestion) {
1135      if (isset($hooks[$suggestion])) {
1136        $info = $hooks[$suggestion];
1137        break;
1138      }
1139    }
1140  }
1141
1142  // Generate the output using either a function or a template.
1143  $output = '';
1144  if (isset($info['function'])) {
1145    if (function_exists($info['function'])) {
1146      $output = $info['function']($variables);
1147    }
1148  }
1149  else {
1150    // Default render function and extension.
1151    $render_function = 'theme_render_template';
1152    $extension = '.tpl.php';
1153
1154    // The theme engine may use a different extension and a different renderer.
1155    global $theme_engine;
1156    if (isset($theme_engine)) {
1157      if ($info['type'] != 'module') {
1158        if (function_exists($theme_engine . '_render_template')) {
1159          $render_function = $theme_engine . '_render_template';
1160        }
1161        $extension_function = $theme_engine . '_extension';
1162        if (function_exists($extension_function)) {
1163          $extension = $extension_function();
1164        }
1165      }
1166    }
1167
1168    // In some cases, a template implementation may not have had
1169    // template_preprocess() run (for example, if the default implementation is
1170    // a function, but a template overrides that default implementation). In
1171    // these cases, a template should still be able to expect to have access to
1172    // the variables provided by template_preprocess(), so we add them here if
1173    // they don't already exist. We don't want to run template_preprocess()
1174    // twice (it would be inefficient and mess up zebra striping), so we use the
1175    // 'directory' variable to determine if it has already run, which while not
1176    // completely intuitive, is reasonably safe, and allows us to save on the
1177    // overhead of adding some new variable to track that.
1178    if (!isset($variables['directory'])) {
1179      $default_template_variables = array();
1180      template_preprocess($default_template_variables, $hook);
1181      $variables += $default_template_variables;
1182    }
1183
1184    // Render the output using the template file.
1185    $template_file = $info['template'] . $extension;
1186    if (isset($info['path'])) {
1187      $template_file = $info['path'] . '/' . $template_file;
1188    }
1189    $output = $render_function($template_file, $variables);
1190  }
1191
1192  // restore path_to_theme()
1193  $theme_path = $temp;
1194  return $output;
1195}
1196
1197/**
1198 * Returns the path to the current themed element.
1199 *
1200 * It can point to the active theme or the module handling a themed
1201 * implementation. For example, when invoked within the scope of a theming call
1202 * it will depend on where the theming function is handled. If implemented from
1203 * a module, it will point to the module. If implemented from the active theme,
1204 * it will point to the active theme. When called outside the scope of a
1205 * theming call, it will always point to the active theme.
1206 */
1207function path_to_theme() {
1208  global $theme_path;
1209
1210  if (!isset($theme_path)) {
1211    drupal_theme_initialize();
1212  }
1213
1214  return $theme_path;
1215}
1216
1217/**
1218 * Allows themes and/or theme engines to discover overridden theme functions.
1219 *
1220 * @param $cache
1221 *   The existing cache of theme hooks to test against.
1222 * @param $prefixes
1223 *   An array of prefixes to test, in reverse order of importance.
1224 *
1225 * @return $implementations
1226 *   The functions found, suitable for returning from hook_theme;
1227 */
1228function drupal_find_theme_functions($cache, $prefixes) {
1229  $implementations = array();
1230  $functions = get_defined_functions();
1231
1232  foreach ($cache as $hook => $info) {
1233    foreach ($prefixes as $prefix) {
1234      // Find theme functions that implement possible "suggestion" variants of
1235      // registered theme hooks and add those as new registered theme hooks.
1236      // The 'pattern' key defines a common prefix that all suggestions must
1237      // start with. The default is the name of the hook followed by '__'. An
1238      // 'base hook' key is added to each entry made for a found suggestion,
1239      // so that common functionality can be implemented for all suggestions of
1240      // the same base hook. To keep things simple, deep hierarchy of
1241      // suggestions is not supported: each suggestion's 'base hook' key
1242      // refers to a base hook, not to another suggestion, and all suggestions
1243      // are found using the base hook's pattern, not a pattern from an
1244      // intermediary suggestion.
1245      $pattern = isset($info['pattern']) ? $info['pattern'] : ($hook . '__');
1246      if (!isset($info['base hook']) && !empty($pattern)) {
1247        $matches = preg_grep('/^' . $prefix . '_' . $pattern . '/', $functions['user']);
1248        if ($matches) {
1249          foreach ($matches as $match) {
1250            $new_hook = substr($match, strlen($prefix) + 1);
1251            $arg_name = isset($info['variables']) ? 'variables' : 'render element';
1252            $implementations[$new_hook] = array(
1253              'function' => $match,
1254              $arg_name => $info[$arg_name],
1255              'base hook' => $hook,
1256            );
1257          }
1258        }
1259      }
1260      // Find theme functions that implement registered theme hooks and include
1261      // that in what is returned so that the registry knows that the theme has
1262      // this implementation.
1263      if (function_exists($prefix . '_' . $hook)) {
1264        $implementations[$hook] = array(
1265          'function' => $prefix . '_' . $hook,
1266        );
1267      }
1268    }
1269  }
1270
1271  return $implementations;
1272}
1273
1274/**
1275 * Allows themes and/or theme engines to easily discover overridden templates.
1276 *
1277 * @param $cache
1278 *   The existing cache of theme hooks to test against.
1279 * @param $extension
1280 *   The extension that these templates will have.
1281 * @param $path
1282 *   The path to search.
1283 */
1284function drupal_find_theme_templates($cache, $extension, $path) {
1285  $implementations = array();
1286
1287  // Collect paths to all sub-themes grouped by base themes. These will be
1288  // used for filtering. This allows base themes to have sub-themes in its
1289  // folder hierarchy without affecting the base themes template discovery.
1290  $theme_paths = array();
1291  foreach (list_themes() as $theme_info) {
1292    if (!empty($theme_in

Large files files are truncated, but you can click here to view the full file