PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/theme.inc

https://bitbucket.org/antisocnet/drupal
Pascal | 2905 lines | 1614 code | 142 blank | 1149 comment | 245 complexity | fa82d6b59f8fb0e685a89e3d3cfd3e3a MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

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

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

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