PageRenderTime 32ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/theme.inc

https://github.com/martini7/Drupal-6.20
Pascal | 2031 lines | 1159 code | 112 blank | 760 comment | 166 complexity | 18d15cfefc30eb258c289ee2eabe0599 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0

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

  1. <?php
  2. // $Id: theme.inc,v 1.415.2.28 2010/11/03 19:03:19 goba Exp $
  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. * @ingroup themeable
  11. */
  12. /**
  13. * @name Content markers
  14. * @{
  15. * Markers used by theme_mark() and node_mark() to designate content.
  16. * @see theme_mark(), node_mark()
  17. */
  18. define('MARK_READ', 0);
  19. define('MARK_NEW', 1);
  20. define('MARK_UPDATED', 2);
  21. /**
  22. * @} End of "Content markers".
  23. */
  24. /**
  25. * Initialize the theme system by loading the theme.
  26. */
  27. function init_theme() {
  28. global $theme, $user, $custom_theme, $theme_key;
  29. // If $theme is already set, assume the others are set, too, and do nothing
  30. if (isset($theme)) {
  31. return;
  32. }
  33. drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
  34. $themes = list_themes();
  35. // Only select the user selected theme if it is available in the
  36. // list of enabled themes.
  37. $theme = !empty($user->theme) && !empty($themes[$user->theme]->status) ? $user->theme : variable_get('theme_default', 'garland');
  38. // Allow modules to override the present theme... only select custom theme
  39. // if it is available in the list of installed themes.
  40. $theme = $custom_theme && $themes[$custom_theme] ? $custom_theme : $theme;
  41. // Store the identifier for retrieving theme settings with.
  42. $theme_key = $theme;
  43. // Find all our ancestor themes and put them in an array.
  44. $base_theme = array();
  45. $ancestor = $theme;
  46. while ($ancestor && isset($themes[$ancestor]->base_theme)) {
  47. $base_theme[] = $new_base_theme = $themes[$themes[$ancestor]->base_theme];
  48. $ancestor = $themes[$ancestor]->base_theme;
  49. }
  50. _init_theme($themes[$theme], array_reverse($base_theme));
  51. }
  52. /**
  53. * Initialize the theme system given already loaded information. This
  54. * function is useful to initialize a theme when no database is present.
  55. *
  56. * @param $theme
  57. * An object with the following information:
  58. * filename
  59. * The .info file for this theme. The 'path' to
  60. * the theme will be in this file's directory. (Required)
  61. * owner
  62. * The path to the .theme file or the .engine file to load for
  63. * the theme. (Required)
  64. * stylesheet
  65. * The primary stylesheet for the theme. (Optional)
  66. * engine
  67. * The name of theme engine to use. (Optional)
  68. * @param $base_theme
  69. * An optional array of objects that represent the 'base theme' if the
  70. * theme is meant to be derivative of another theme. It requires
  71. * the same information as the $theme object. It should be in
  72. * 'oldest first' order, meaning the top level of the chain will
  73. * be first.
  74. * @param $registry_callback
  75. * The callback to invoke to set the theme registry.
  76. */
  77. function _init_theme($theme, $base_theme = array(), $registry_callback = '_theme_load_registry') {
  78. global $theme_info, $base_theme_info, $theme_engine, $theme_path;
  79. $theme_info = $theme;
  80. $base_theme_info = $base_theme;
  81. $theme_path = dirname($theme->filename);
  82. // Prepare stylesheets from this theme as well as all ancestor themes.
  83. // We work it this way so that we can have child themes override parent
  84. // theme stylesheets easily.
  85. $final_stylesheets = array();
  86. // Grab stylesheets from base theme
  87. foreach ($base_theme as $base) {
  88. if (!empty($base->stylesheets)) {
  89. foreach ($base->stylesheets as $media => $stylesheets) {
  90. foreach ($stylesheets as $name => $stylesheet) {
  91. $final_stylesheets[$media][$name] = $stylesheet;
  92. }
  93. }
  94. }
  95. }
  96. // Add stylesheets used by this theme.
  97. if (!empty($theme->stylesheets)) {
  98. foreach ($theme->stylesheets as $media => $stylesheets) {
  99. foreach ($stylesheets as $name => $stylesheet) {
  100. $final_stylesheets[$media][$name] = $stylesheet;
  101. }
  102. }
  103. }
  104. // And now add the stylesheets properly
  105. foreach ($final_stylesheets as $media => $stylesheets) {
  106. foreach ($stylesheets as $stylesheet) {
  107. drupal_add_css($stylesheet, 'theme', $media);
  108. }
  109. }
  110. // Do basically the same as the above for scripts
  111. $final_scripts = array();
  112. // Grab scripts from base theme
  113. foreach ($base_theme as $base) {
  114. if (!empty($base->scripts)) {
  115. foreach ($base->scripts as $name => $script) {
  116. $final_scripts[$name] = $script;
  117. }
  118. }
  119. }
  120. // Add scripts used by this theme.
  121. if (!empty($theme->scripts)) {
  122. foreach ($theme->scripts as $name => $script) {
  123. $final_scripts[$name] = $script;
  124. }
  125. }
  126. // Add scripts used by this theme.
  127. foreach ($final_scripts as $script) {
  128. drupal_add_js($script, 'theme');
  129. }
  130. $theme_engine = NULL;
  131. // Initialize the theme.
  132. if (isset($theme->engine)) {
  133. // Include the engine.
  134. include_once './'. $theme->owner;
  135. $theme_engine = $theme->engine;
  136. if (function_exists($theme_engine .'_init')) {
  137. foreach ($base_theme as $base) {
  138. call_user_func($theme_engine .'_init', $base);
  139. }
  140. call_user_func($theme_engine .'_init', $theme);
  141. }
  142. }
  143. else {
  144. // include non-engine theme files
  145. foreach ($base_theme as $base) {
  146. // Include the theme file or the engine.
  147. if (!empty($base->owner)) {
  148. include_once './'. $base->owner;
  149. }
  150. }
  151. // and our theme gets one too.
  152. if (!empty($theme->owner)) {
  153. include_once './'. $theme->owner;
  154. }
  155. }
  156. $registry_callback($theme, $base_theme, $theme_engine);
  157. }
  158. /**
  159. * Retrieve the stored theme registry. If the theme registry is already
  160. * in memory it will be returned; otherwise it will attempt to load the
  161. * registry from cache. If this fails, it will construct the registry and
  162. * cache it.
  163. */
  164. function theme_get_registry($registry = NULL) {
  165. static $theme_registry = NULL;
  166. if (isset($registry)) {
  167. $theme_registry = $registry;
  168. }
  169. return $theme_registry;
  170. }
  171. /**
  172. * Store the theme registry in memory.
  173. */
  174. function _theme_set_registry($registry) {
  175. // Pass through for setting of static variable.
  176. return theme_get_registry($registry);
  177. }
  178. /**
  179. * Get the theme_registry cache from the database; if it doesn't exist, build
  180. * it.
  181. *
  182. * @param $theme
  183. * The loaded $theme object.
  184. * @param $base_theme
  185. * An array of loaded $theme objects representing the ancestor themes in
  186. * oldest first order.
  187. * @param theme_engine
  188. * The name of the theme engine.
  189. */
  190. function _theme_load_registry($theme, $base_theme = NULL, $theme_engine = NULL) {
  191. // Check the theme registry cache; if it exists, use it.
  192. $cache = cache_get("theme_registry:$theme->name", 'cache');
  193. if (isset($cache->data)) {
  194. $registry = $cache->data;
  195. }
  196. else {
  197. // If not, build one and cache it.
  198. $registry = _theme_build_registry($theme, $base_theme, $theme_engine);
  199. _theme_save_registry($theme, $registry);
  200. }
  201. _theme_set_registry($registry);
  202. }
  203. /**
  204. * Write the theme_registry cache into the database.
  205. */
  206. function _theme_save_registry($theme, $registry) {
  207. cache_set("theme_registry:$theme->name", $registry);
  208. }
  209. /**
  210. * Force the system to rebuild the theme registry; this should be called
  211. * when modules are added to the system, or when a dynamic system needs
  212. * to add more theme hooks.
  213. */
  214. function drupal_rebuild_theme_registry() {
  215. cache_clear_all('theme_registry', 'cache', TRUE);
  216. }
  217. /**
  218. * Process a single invocation of the theme hook. $type will be one
  219. * of 'module', 'theme_engine', 'base_theme_engine', 'theme', or 'base_theme'
  220. * and it tells us some important information.
  221. *
  222. * Because $cache is a reference, the cache will be continually
  223. * expanded upon; new entries will replace old entries in the
  224. * array_merge, but we are careful to ensure some data is carried
  225. * forward, such as the arguments a theme hook needs.
  226. *
  227. * An override flag can be set for preprocess functions. When detected the
  228. * cached preprocessors for the hook will not be merged with the newly set.
  229. * This can be useful to themes and theme engines by giving them more control
  230. * over how and when the preprocess functions are run.
  231. */
  232. function _theme_process_registry(&$cache, $name, $type, $theme, $path) {
  233. $result = array();
  234. $function = $name .'_theme';
  235. if (function_exists($function)) {
  236. $result = $function($cache, $type, $theme, $path);
  237. foreach ($result as $hook => $info) {
  238. $result[$hook]['type'] = $type;
  239. $result[$hook]['theme path'] = $path;
  240. // if function and file are left out, default to standard naming
  241. // conventions.
  242. if (!isset($info['template']) && !isset($info['function'])) {
  243. $result[$hook]['function'] = ($type == 'module' ? 'theme_' : $name .'_') . $hook;
  244. }
  245. // Make sure include files is set so we don't generate notices later.
  246. if (!isset($info['include files'])) {
  247. $result[$hook]['include files'] = array();
  248. }
  249. // If a path is set in the info, use what was set. Otherwise use the
  250. // default path. This is mostly so system.module can declare theme
  251. // functions on behalf of core .include files.
  252. // All files are included to be safe. Conditionally included
  253. // files can prevent them from getting registered.
  254. if (isset($info['file']) && !isset($info['path'])) {
  255. // First, check to see if the fully qualified file exists.
  256. $filename = './'. $path .'/'. $info['file'];
  257. if (file_exists($filename)) {
  258. require_once $filename;
  259. $result[$hook]['include files'][] = $filename;
  260. }
  261. else {
  262. $filename = './'. $info['file'];
  263. if (file_exists($filename)) {
  264. require_once $filename;
  265. $result[$hook]['include files'][] = $filename;
  266. }
  267. }
  268. }
  269. elseif (isset($info['file']) && isset($info['path'])) {
  270. $filename = './'. $info['path'] .'/'. $info['file'];
  271. if (file_exists($filename)) {
  272. require_once $filename;
  273. $result[$hook]['include files'][] = $filename;
  274. }
  275. }
  276. if (isset($info['template']) && !isset($info['path'])) {
  277. $result[$hook]['template'] = $path .'/'. $info['template'];
  278. }
  279. // If 'arguments' have been defined previously, carry them forward.
  280. // This should happen if a theme overrides a Drupal defined theme
  281. // function, for example.
  282. if (!isset($info['arguments']) && isset($cache[$hook])) {
  283. $result[$hook]['arguments'] = $cache[$hook]['arguments'];
  284. }
  285. // Likewise with theme paths. These are used for template naming suggestions.
  286. // Theme implementations can occur in multiple paths. Suggestions should follow.
  287. if (!isset($info['theme paths']) && isset($cache[$hook])) {
  288. $result[$hook]['theme paths'] = $cache[$hook]['theme paths'];
  289. }
  290. // Check for sub-directories.
  291. $result[$hook]['theme paths'][] = isset($info['path']) ? $info['path'] : $path;
  292. // Check for default _preprocess_ functions. Ensure arrayness.
  293. if (!isset($info['preprocess functions']) || !is_array($info['preprocess functions'])) {
  294. $info['preprocess functions'] = array();
  295. $prefixes = array();
  296. if ($type == 'module') {
  297. // Default preprocessor prefix.
  298. $prefixes[] = 'template';
  299. // Add all modules so they can intervene with their own preprocessors. This allows them
  300. // to provide preprocess functions even if they are not the owner of the current hook.
  301. $prefixes += module_list();
  302. }
  303. elseif ($type == 'theme_engine' || $type == 'base_theme_engine') {
  304. // Theme engines get an extra set that come before the normally named preprocessors.
  305. $prefixes[] = $name .'_engine';
  306. // The theme engine also registers on behalf of the theme. The theme or engine name can be used.
  307. $prefixes[] = $name;
  308. $prefixes[] = $theme;
  309. }
  310. else {
  311. // This applies when the theme manually registers their own preprocessors.
  312. $prefixes[] = $name;
  313. }
  314. foreach ($prefixes as $prefix) {
  315. if (function_exists($prefix .'_preprocess')) {
  316. $info['preprocess functions'][] = $prefix .'_preprocess';
  317. }
  318. if (function_exists($prefix .'_preprocess_'. $hook)) {
  319. $info['preprocess functions'][] = $prefix .'_preprocess_'. $hook;
  320. }
  321. if (!empty($info['original hook']) && function_exists($prefix .'_preprocess_'. $info['original hook'])) {
  322. $info['preprocess functions'][] = $prefix .'_preprocess_'. $info['original hook'];
  323. }
  324. }
  325. }
  326. // Check for the override flag and prevent the cached preprocess functions from being used.
  327. // This allows themes or theme engines to remove preprocessors set earlier in the registry build.
  328. if (!empty($info['override preprocess functions'])) {
  329. // Flag not needed inside the registry.
  330. unset($result[$hook]['override preprocess functions']);
  331. }
  332. elseif (isset($cache[$hook]['preprocess functions']) && is_array($cache[$hook]['preprocess functions'])) {
  333. $info['preprocess functions'] = array_merge($cache[$hook]['preprocess functions'], $info['preprocess functions']);
  334. }
  335. elseif (isset($info['original hook']) && isset($cache[$info['original hook']]['preprocess functions']) && is_array($cache[$info['original hook']]['preprocess functions'])) {
  336. $info['preprocess functions'] = array_merge($cache[$info['original hook']]['preprocess functions'], $info['preprocess functions']);
  337. }
  338. $result[$hook]['preprocess functions'] = $info['preprocess functions'];
  339. }
  340. // Merge the newly created theme hooks into the existing cache.
  341. $cache = array_merge($cache, $result);
  342. }
  343. // Let themes have preprocess functions even if they didn't register a template.
  344. if ($type == 'theme' || $type == 'base_theme') {
  345. foreach ($cache as $hook => $info) {
  346. // Check only if it's a template and not registered by the theme or engine.
  347. if (!empty($info['template']) && empty($result[$hook])) {
  348. if (!isset($info['preprocess functions'])) {
  349. $cache[$hook]['preprocess functions'] = array();
  350. }
  351. if (function_exists($name .'_preprocess')) {
  352. $cache[$hook]['preprocess functions'][] = $name .'_preprocess';
  353. }
  354. if (function_exists($name .'_preprocess_'. $hook)) {
  355. $cache[$hook]['preprocess functions'][] = $name .'_preprocess_'. $hook;
  356. }
  357. // Ensure uniqueness.
  358. $cache[$hook]['preprocess functions'] = array_unique($cache[$hook]['preprocess functions']);
  359. }
  360. }
  361. }
  362. }
  363. /**
  364. * Rebuild the hook theme_registry cache.
  365. *
  366. * @param $theme
  367. * The loaded $theme object.
  368. * @param $base_theme
  369. * An array of loaded $theme objects representing the ancestor themes in
  370. * oldest first order.
  371. * @param theme_engine
  372. * The name of the theme engine.
  373. */
  374. function _theme_build_registry($theme, $base_theme, $theme_engine) {
  375. $cache = array();
  376. // First, process the theme hooks advertised by modules. This will
  377. // serve as the basic registry.
  378. foreach (module_implements('theme') as $module) {
  379. _theme_process_registry($cache, $module, 'module', $module, drupal_get_path('module', $module));
  380. }
  381. // Process each base theme.
  382. foreach ($base_theme as $base) {
  383. // If the base theme uses a theme engine, process its hooks.
  384. $base_path = dirname($base->filename);
  385. if ($theme_engine) {
  386. _theme_process_registry($cache, $theme_engine, 'base_theme_engine', $base->name, $base_path);
  387. }
  388. _theme_process_registry($cache, $base->name, 'base_theme', $base->name, $base_path);
  389. }
  390. // And then the same thing, but for the theme.
  391. if ($theme_engine) {
  392. _theme_process_registry($cache, $theme_engine, 'theme_engine', $theme->name, dirname($theme->filename));
  393. }
  394. // Finally, hooks provided by the theme itself.
  395. _theme_process_registry($cache, $theme->name, 'theme', $theme->name, dirname($theme->filename));
  396. // Let modules alter the registry
  397. drupal_alter('theme_registry', $cache);
  398. return $cache;
  399. }
  400. /**
  401. * Provides a list of currently available themes.
  402. *
  403. * If the database is active then it will be retrieved from the database.
  404. * Otherwise it will retrieve a new list.
  405. *
  406. * @param $refresh
  407. * Whether to reload the list of themes from the database.
  408. * @return
  409. * An array of the currently available themes.
  410. */
  411. function list_themes($refresh = FALSE) {
  412. static $list = array();
  413. if ($refresh) {
  414. $list = array();
  415. }
  416. if (empty($list)) {
  417. $list = array();
  418. $themes = array();
  419. // Extract from the database only when it is available.
  420. // Also check that the site is not in the middle of an install or update.
  421. if (db_is_active() && !defined('MAINTENANCE_MODE')) {
  422. $result = db_query("SELECT * FROM {system} WHERE type = '%s'", 'theme');
  423. while ($theme = db_fetch_object($result)) {
  424. if (file_exists($theme->filename)) {
  425. $theme->info = unserialize($theme->info);
  426. $themes[] = $theme;
  427. }
  428. }
  429. }
  430. else {
  431. // Scan the installation when the database should not be read.
  432. $themes = _system_theme_data();
  433. }
  434. foreach ($themes as $theme) {
  435. foreach ($theme->info['stylesheets'] as $media => $stylesheets) {
  436. foreach ($stylesheets as $stylesheet => $path) {
  437. $theme->stylesheets[$media][$stylesheet] = $path;
  438. }
  439. }
  440. foreach ($theme->info['scripts'] as $script => $path) {
  441. if (file_exists($path)) {
  442. $theme->scripts[$script] = $path;
  443. }
  444. }
  445. if (isset($theme->info['engine'])) {
  446. $theme->engine = $theme->info['engine'];
  447. }
  448. if (isset($theme->info['base theme'])) {
  449. $theme->base_theme = $theme->info['base theme'];
  450. }
  451. // Status is normally retrieved from the database. Add zero values when
  452. // read from the installation directory to prevent notices.
  453. if (!isset($theme->status)) {
  454. $theme->status = 0;
  455. }
  456. $list[$theme->name] = $theme;
  457. }
  458. }
  459. return $list;
  460. }
  461. /**
  462. * Generate the themed output.
  463. *
  464. * All requests for theme hooks must go through this function. It examines
  465. * the request and routes it to the appropriate theme function. The theme
  466. * registry is checked to determine which implementation to use, which may
  467. * be a function or a template.
  468. *
  469. * If the implementation is a function, it is executed and its return value
  470. * passed along.
  471. *
  472. * If the implementation is a template, the arguments are converted to a
  473. * $variables array. This array is then modified by the module implementing
  474. * the hook, theme engine (if applicable) and the theme. The following
  475. * functions may be used to modify the $variables array. They are processed in
  476. * this order when available:
  477. *
  478. * - template_preprocess(&$variables)
  479. * This sets a default set of variables for all template implementations.
  480. *
  481. * - template_preprocess_HOOK(&$variables)
  482. * This is the first preprocessor called specific to the hook; it should be
  483. * implemented by the module that registers it.
  484. *
  485. * - MODULE_preprocess(&$variables)
  486. * This will be called for all templates; it should only be used if there
  487. * is a real need. It's purpose is similar to template_preprocess().
  488. *
  489. * - MODULE_preprocess_HOOK(&$variables)
  490. * This is for modules that want to alter or provide extra variables for
  491. * theming hooks not registered to itself. For example, if a module named
  492. * "foo" wanted to alter the $submitted variable for the hook "node" a
  493. * preprocess function of foo_preprocess_node() can be created to intercept
  494. * and alter the variable.
  495. *
  496. * - ENGINE_engine_preprocess(&$variables)
  497. * This function should only be implemented by theme engines and exists
  498. * so that it can set necessary variables for all hooks.
  499. *
  500. * - ENGINE_engine_preprocess_HOOK(&$variables)
  501. * This is the same as the previous function, but it is called for a single
  502. * theming hook.
  503. *
  504. * - ENGINE_preprocess(&$variables)
  505. * This is meant to be used by themes that utilize a theme engine. It is
  506. * provided so that the preprocessor is not locked into a specific theme.
  507. * This makes it easy to share and transport code but theme authors must be
  508. * careful to prevent fatal re-declaration errors when using sub-themes that
  509. * have their own preprocessor named exactly the same as its base theme. In
  510. * the default theme engine (PHPTemplate), sub-themes will load their own
  511. * template.php file in addition to the one used for its parent theme. This
  512. * increases the risk for these errors. A good practice is to use the engine
  513. * name for the base theme and the theme name for the sub-themes to minimize
  514. * this possibility.
  515. *
  516. * - ENGINE_preprocess_HOOK(&$variables)
  517. * The same applies from the previous function, but it is called for a
  518. * specific hook.
  519. *
  520. * - THEME_preprocess(&$variables)
  521. * These functions are based upon the raw theme; they should primarily be
  522. * used by themes that do not use an engine or by sub-themes. It serves the
  523. * same purpose as ENGINE_preprocess().
  524. *
  525. * - THEME_preprocess_HOOK(&$variables)
  526. * The same applies from the previous function, but it is called for a
  527. * specific hook.
  528. *
  529. * There are two special variables that these hooks can set:
  530. * 'template_file' and 'template_files'. These will be merged together
  531. * to form a list of 'suggested' alternate template files to use, in
  532. * reverse order of priority. template_file will always be a higher
  533. * priority than items in template_files. theme() will then look for these
  534. * files, one at a time, and use the first one
  535. * that exists.
  536. * @param $hook
  537. * The name of the theme function to call. May be an array, in which
  538. * case the first hook that actually has an implementation registered
  539. * will be used. This can be used to choose 'fallback' theme implementations,
  540. * so that if the specific theme hook isn't implemented anywhere, a more
  541. * generic one will be used. This can allow themes to create specific theme
  542. * implementations for named objects.
  543. * @param ...
  544. * Additional arguments to pass along to the theme function.
  545. * @return
  546. * An HTML string that generates the themed output.
  547. */
  548. function theme() {
  549. $args = func_get_args();
  550. $hook = array_shift($args);
  551. static $hooks = NULL;
  552. if (!isset($hooks)) {
  553. init_theme();
  554. $hooks = theme_get_registry();
  555. }
  556. if (is_array($hook)) {
  557. foreach ($hook as $candidate) {
  558. if (isset($hooks[$candidate])) {
  559. break;
  560. }
  561. }
  562. $hook = $candidate;
  563. }
  564. if (!isset($hooks[$hook])) {
  565. return;
  566. }
  567. $info = $hooks[$hook];
  568. global $theme_path;
  569. $temp = $theme_path;
  570. // point path_to_theme() to the currently used theme path:
  571. $theme_path = $hooks[$hook]['theme path'];
  572. // Include a file if the theme function or preprocess function is held elsewhere.
  573. if (!empty($info['include files'])) {
  574. foreach ($info['include files'] as $include_file) {
  575. include_once($include_file);
  576. }
  577. }
  578. // Handle compatibility with theme_registry_alters to prevent failures.
  579. if (!empty($info['file'])) {
  580. static $included_files = array();
  581. $include_file = $info['file'];
  582. if (!empty($info['path'])) {
  583. $include_file = $info['path'] .'/'. $include_file;
  584. }
  585. if (empty($included_files[$include_file])) {
  586. // Statically cache files we've already tried to include so we don't
  587. // run unnecessary file_exists calls.
  588. $included_files[$include_file] = TRUE;
  589. if (file_exists('./'. $include_file)) {
  590. include_once('./'. $include_file);
  591. }
  592. }
  593. }
  594. if (isset($info['function'])) {
  595. // The theme call is a function.
  596. $output = call_user_func_array($info['function'], $args);
  597. }
  598. else {
  599. // The theme call is a template.
  600. $variables = array(
  601. 'template_files' => array()
  602. );
  603. if (!empty($info['arguments'])) {
  604. $count = 0;
  605. foreach ($info['arguments'] as $name => $default) {
  606. $variables[$name] = isset($args[$count]) ? $args[$count] : $default;
  607. $count++;
  608. }
  609. }
  610. // default render function and extension.
  611. $render_function = 'theme_render_template';
  612. $extension = '.tpl.php';
  613. // Run through the theme engine variables, if necessary
  614. global $theme_engine;
  615. if (isset($theme_engine)) {
  616. // If theme or theme engine is implementing this, it may have
  617. // a different extension and a different renderer.
  618. if ($hooks[$hook]['type'] != 'module') {
  619. if (function_exists($theme_engine .'_render_template')) {
  620. $render_function = $theme_engine .'_render_template';
  621. }
  622. $extension_function = $theme_engine .'_extension';
  623. if (function_exists($extension_function)) {
  624. $extension = $extension_function();
  625. }
  626. }
  627. }
  628. if (isset($info['preprocess functions']) && is_array($info['preprocess functions'])) {
  629. // This construct ensures that we can keep a reference through
  630. // call_user_func_array.
  631. $args = array(&$variables, $hook);
  632. foreach ($info['preprocess functions'] as $preprocess_function) {
  633. if (function_exists($preprocess_function)) {
  634. call_user_func_array($preprocess_function, $args);
  635. }
  636. }
  637. }
  638. // Get suggestions for alternate templates out of the variables
  639. // that were set. This lets us dynamically choose a template
  640. // from a list. The order is FILO, so this array is ordered from
  641. // least appropriate first to most appropriate last.
  642. $suggestions = array();
  643. if (isset($variables['template_files'])) {
  644. $suggestions = $variables['template_files'];
  645. }
  646. if (isset($variables['template_file'])) {
  647. $suggestions[] = $variables['template_file'];
  648. }
  649. if ($suggestions) {
  650. $template_file = drupal_discover_template($info['theme paths'], $suggestions, $extension);
  651. }
  652. if (empty($template_file)) {
  653. $template_file = $hooks[$hook]['template'] . $extension;
  654. if (isset($hooks[$hook]['path'])) {
  655. $template_file = $hooks[$hook]['path'] .'/'. $template_file;
  656. }
  657. }
  658. $output = $render_function($template_file, $variables);
  659. }
  660. // restore path_to_theme()
  661. $theme_path = $temp;
  662. // Add final markup to the full page.
  663. if ($hook == 'page' || $hook == 'book_export_html') {
  664. $output = drupal_final_markup($output);
  665. }
  666. return $output;
  667. }
  668. /**
  669. * Choose which template file to actually render. These are all suggested
  670. * templates from themes and modules. Theming implementations can occur on
  671. * multiple levels. All paths are checked to account for this.
  672. */
  673. function drupal_discover_template($paths, $suggestions, $extension = '.tpl.php') {
  674. global $theme_engine;
  675. // Remove slashes or null to prevent files from being included from
  676. // an unexpected location (especially on Windows servers).
  677. $extension = str_replace(array("/", "\\", "\0"), '', $extension);
  678. // Loop through all paths and suggestions in FIFO order.
  679. $suggestions = array_reverse($suggestions);
  680. $paths = array_reverse($paths);
  681. foreach ($suggestions as $suggestion) {
  682. if (!empty($suggestion)) {
  683. $suggestion = str_replace(array("/", "\\", "\0"), '', $suggestion);
  684. foreach ($paths as $path) {
  685. if (file_exists($file = $path .'/'. $suggestion . $extension)) {
  686. return $file;
  687. }
  688. }
  689. }
  690. }
  691. }
  692. /**
  693. * Return the path to the current themed element.
  694. *
  695. * It can point to the active theme or the module handling a themed implementation.
  696. * For example, when invoked within the scope of a theming call it will depend
  697. * on where the theming function is handled. If implemented from a module, it
  698. * will point to the module. If implemented from the active theme, it will point
  699. * to the active theme. When called outside the scope of a theming call, it will
  700. * always point to the active theme.
  701. */
  702. function path_to_theme() {
  703. global $theme_path;
  704. if (!isset($theme_path)) {
  705. init_theme();
  706. }
  707. return $theme_path;
  708. }
  709. /**
  710. * Find overridden theme functions. Called by themes and/or theme engines to
  711. * easily discover theme functions.
  712. *
  713. * @param $cache
  714. * The existing cache of theme hooks to test against.
  715. * @param $prefixes
  716. * An array of prefixes to test, in reverse order of importance.
  717. *
  718. * @return $templates
  719. * The functions found, suitable for returning from hook_theme;
  720. */
  721. function drupal_find_theme_functions($cache, $prefixes) {
  722. $templates = array();
  723. $functions = get_defined_functions();
  724. foreach ($cache as $hook => $info) {
  725. foreach ($prefixes as $prefix) {
  726. if (!empty($info['pattern'])) {
  727. $matches = preg_grep('/^'. $prefix .'_'. $info['pattern'] .'/', $functions['user']);
  728. if ($matches) {
  729. foreach ($matches as $match) {
  730. $new_hook = str_replace($prefix .'_', '', $match);
  731. $templates[$new_hook] = array(
  732. 'function' => $match,
  733. 'arguments' => $info['arguments'],
  734. 'original hook' => $hook,
  735. 'include files' => $info['include files'],
  736. );
  737. }
  738. }
  739. }
  740. if (function_exists($prefix .'_'. $hook)) {
  741. $templates[$hook] = array(
  742. 'function' => $prefix .'_'. $hook,
  743. 'include files' => $info['include files'],
  744. );
  745. // Ensure that the pattern is maintained from base themes to its sub-themes.
  746. // Each sub-theme will have their functions scanned so the pattern must be
  747. // held for subsequent runs.
  748. if (isset($info['pattern'])) {
  749. $templates[$hook]['pattern'] = $info['pattern'];
  750. }
  751. // Also ensure that the 'file' property is maintained, because it probably
  752. // contains the preprocess.
  753. }
  754. }
  755. }
  756. return $templates;
  757. }
  758. /**
  759. * Find overridden theme templates. Called by themes and/or theme engines to
  760. * easily discover templates.
  761. *
  762. * @param $cache
  763. * The existing cache of theme hooks to test against.
  764. * @param $extension
  765. * The extension that these templates will have.
  766. * @param $path
  767. * The path to search.
  768. */
  769. function drupal_find_theme_templates($cache, $extension, $path) {
  770. $templates = array();
  771. // Collect paths to all sub-themes grouped by base themes. These will be
  772. // used for filtering. This allows base themes to have sub-themes in its
  773. // folder hierarchy without affecting the base themes template discovery.
  774. $theme_paths = array();
  775. foreach (list_themes() as $theme_info) {
  776. if (!empty($theme_info->base_theme)) {
  777. $theme_paths[$theme_info->base_theme][$theme_info->name] = dirname($theme_info->filename);
  778. }
  779. }
  780. foreach ($theme_paths as $basetheme => $subthemes) {
  781. foreach ($subthemes as $subtheme => $subtheme_path) {
  782. if (isset($theme_paths[$subtheme])) {
  783. $theme_paths[$basetheme] = array_merge($theme_paths[$basetheme], $theme_paths[$subtheme]);
  784. }
  785. }
  786. }
  787. global $theme;
  788. $subtheme_paths = isset($theme_paths[$theme]) ? $theme_paths[$theme] : array();
  789. // Escape the periods in the extension.
  790. $regex = str_replace('.', '\.', $extension) .'$';
  791. // Because drupal_system_listing works the way it does, we check for real
  792. // templates separately from checking for patterns.
  793. $files = drupal_system_listing($regex, $path, 'name', 0);
  794. foreach ($files as $template => $file) {
  795. // Ignore sub-theme templates for the current theme.
  796. if (strpos($file->filename, str_replace($subtheme_paths, '', $file->filename)) !== 0) {
  797. continue;
  798. }
  799. // Chop off the remaining extensions if there are any. $template already
  800. // has the rightmost extension removed, but there might still be more,
  801. // such as with .tpl.php, which still has .tpl in $template at this point.
  802. if (($pos = strpos($template, '.')) !== FALSE) {
  803. $template = substr($template, 0, $pos);
  804. }
  805. // Transform - in filenames to _ to match function naming scheme
  806. // for the purposes of searching.
  807. $hook = strtr($template, '-', '_');
  808. if (isset($cache[$hook])) {
  809. $templates[$hook] = array(
  810. 'template' => $template,
  811. 'path' => dirname($file->filename),
  812. 'include files' => $cache[$hook]['include files'],
  813. );
  814. }
  815. // Ensure that the pattern is maintained from base themes to its sub-themes.
  816. // Each sub-theme will have their templates scanned so the pattern must be
  817. // held for subsequent runs.
  818. if (isset($cache[$hook]['pattern'])) {
  819. $templates[$hook]['pattern'] = $cache[$hook]['pattern'];
  820. }
  821. }
  822. $patterns = array_keys($files);
  823. foreach ($cache as $hook => $info) {
  824. if (!empty($info['pattern'])) {
  825. // Transform _ in pattern to - to match file naming scheme
  826. // for the purposes of searching.
  827. $pattern = strtr($info['pattern'], '_', '-');
  828. $matches = preg_grep('/^'. $pattern .'/', $patterns);
  829. if ($matches) {
  830. foreach ($matches as $match) {
  831. $file = substr($match, 0, strpos($match, '.'));
  832. // Put the underscores back in for the hook name and register this pattern.
  833. $templates[strtr($file, '-', '_')] = array(
  834. 'template' => $file,
  835. 'path' => dirname($files[$match]->filename),
  836. 'arguments' => $info['arguments'],
  837. 'original hook' => $hook,
  838. 'include files' => $info['include files'],
  839. );
  840. }
  841. }
  842. }
  843. }
  844. return $templates;
  845. }
  846. /**
  847. * Retrieve an associative array containing the settings for a theme.
  848. *
  849. * The final settings are arrived at by merging the default settings,
  850. * the site-wide settings, and the settings defined for the specific theme.
  851. * If no $key was specified, only the site-wide theme defaults are retrieved.
  852. *
  853. * The default values for each of settings are also defined in this function.
  854. * To add new settings, add their default values here, and then add form elements
  855. * to system_theme_settings() in system.module.
  856. *
  857. * @param $key
  858. * The template/style value for a given theme.
  859. *
  860. * @return
  861. * An associative array containing theme settings.
  862. */
  863. function theme_get_settings($key = NULL) {
  864. $defaults = array(
  865. 'mission' => '',
  866. 'default_logo' => 1,
  867. 'logo_path' => '',
  868. 'default_favicon' => 1,
  869. 'favicon_path' => '',
  870. 'primary_links' => 1,
  871. 'secondary_links' => 1,
  872. 'toggle_logo' => 1,
  873. 'toggle_favicon' => 1,
  874. 'toggle_name' => 1,
  875. 'toggle_search' => 1,
  876. 'toggle_slogan' => 0,
  877. 'toggle_mission' => 1,
  878. 'toggle_node_user_picture' => 0,
  879. 'toggle_comment_user_picture' => 0,
  880. 'toggle_primary_links' => 1,
  881. 'toggle_secondary_links' => 1,
  882. );
  883. if (module_exists('node')) {
  884. foreach (node_get_types() as $type => $name) {
  885. $defaults['toggle_node_info_'. $type] = 1;
  886. }
  887. }
  888. $settings = array_merge($defaults, variable_get('theme_settings', array()));
  889. if ($key) {
  890. $settings = array_merge($settings, variable_get(str_replace('/', '_', 'theme_'. $key .'_settings'), array()));
  891. }
  892. // Only offer search box if search.module is enabled.
  893. if (!module_exists('search') || !user_access('search content')) {
  894. $settings['toggle_search'] = 0;
  895. }
  896. return $settings;
  897. }
  898. /**
  899. * Retrieve a setting for the current theme.
  900. * This function is designed for use from within themes & engines
  901. * to determine theme settings made in the admin interface.
  902. *
  903. * Caches values for speed (use $refresh = TRUE to refresh cache)
  904. *
  905. * @param $setting_name
  906. * The name of the setting to be retrieved.
  907. *
  908. * @param $refresh
  909. * Whether to reload the cache of settings.
  910. *
  911. * @return
  912. * The value of the requested setting, NULL if the setting does not exist.
  913. */
  914. function theme_get_setting($setting_name, $refresh = FALSE) {
  915. global $theme_key;
  916. static $settings;
  917. if (empty($settings) || $refresh) {
  918. $settings = theme_get_settings($theme_key);
  919. $themes = list_themes();
  920. $theme_object = $themes[$theme_key];
  921. if ($settings['mission'] == '') {
  922. $settings['mission'] = variable_get('site_mission', '');
  923. }
  924. if (!$settings['toggle_mission']) {
  925. $settings['mission'] = '';
  926. }
  927. if ($settings['toggle_logo']) {
  928. if ($settings['default_logo']) {
  929. $settings['logo'] = base_path() . dirname($theme_object->filename) .'/logo.png';
  930. }
  931. elseif ($settings['logo_path']) {
  932. $settings['logo'] = base_path() . $settings['logo_path'];
  933. }
  934. }
  935. if ($settings['toggle_favicon']) {
  936. if ($settings['default_favicon']) {
  937. if (file_exists($favicon = dirname($theme_object->filename) .'/favicon.ico')) {
  938. $settings['favicon'] = base_path() . $favicon;
  939. }
  940. else {
  941. $settings['favicon'] = base_path() .'misc/favicon.ico';
  942. }
  943. }
  944. elseif ($settings['favicon_path']) {
  945. $settings['favicon'] = base_path() . $settings['favicon_path'];
  946. }
  947. else {
  948. $settings['toggle_favicon'] = FALSE;
  949. }
  950. }
  951. }
  952. return isset($settings[$setting_name]) ? $settings[$setting_name] : NULL;
  953. }
  954. /**
  955. * Render a system default template, which is essentially a PHP template.
  956. *
  957. * @param $template_file
  958. * The filename of the template to render. Note that this will overwrite
  959. * anything stored in $variables['template_file'] if using a preprocess hook.
  960. * @param $variables
  961. * A keyed array of variables that will appear in the output.
  962. *
  963. * @return
  964. * The output generated by the template.
  965. */
  966. function theme_render_template($template_file, $variables) {
  967. extract($variables, EXTR_SKIP); // Extract the variables to a local namespace
  968. ob_start(); // Start output buffering
  969. include "./$template_file"; // Include the template file
  970. $contents = ob_get_contents(); // Get the contents of the buffer
  971. ob_end_clean(); // End buffering and discard
  972. return $contents; // Return the contents
  973. }
  974. /**
  975. * @defgroup themeable Default theme implementations
  976. * @{
  977. * Functions and templates that present output to the user, and can be
  978. * implemented by themes.
  979. *
  980. * Drupal's presentation layer is a pluggable system known as the theme
  981. * layer. Each theme can take control over most of Drupal's output, and
  982. * has complete control over the CSS.
  983. *
  984. * Inside Drupal, the theme layer is utilized by the use of the theme()
  985. * function, which is passed the name of a component (the theme hook)
  986. * and several arguments. For example, theme('table', $header, $rows);
  987. * Additionally, the theme() function can take an array of theme
  988. * hooks, which can be used to provide 'fallback' implementations to
  989. * allow for more specific control of output. For example, the function:
  990. * theme(array('table__foo', 'table'), $header, $rows) would look to see if
  991. * 'table__foo' is registered anywhere; if it is not, it would 'fall back'
  992. * to the generic 'table' implementation. This can be used to attach specific
  993. * theme functions to named objects, allowing the themer more control over
  994. * specific types of output.
  995. *
  996. * As of Drupal 6, every theme hook is required to be registered by the
  997. * module that owns it, so that Drupal can tell what to do with it and
  998. * to make it simple for themes to identify and override the behavior
  999. * for these calls.
  1000. *
  1001. * The theme hooks are registered via hook_theme(), which returns an
  1002. * array of arrays with information about the hook. It describes the
  1003. * arguments the function or template will need, and provides
  1004. * defaults for the template in case they are not filled in. If the default
  1005. * implementation is a function, by convention it is named theme_HOOK().
  1006. *
  1007. * Each module should provide a default implementation for theme_hooks that
  1008. * it registers. This implementation may be either a function or a template;
  1009. * if it is a function it must be specified via hook_theme(). By convention,
  1010. * default implementations of theme hooks are named theme_HOOK. Default
  1011. * template implementations are stored in the module directory.
  1012. *
  1013. * Drupal's default template renderer is a simple PHP parsing engine that
  1014. * includes the template and stores the output. Drupal's theme engines
  1015. * can provide alternate template engines, such as XTemplate, Smarty and
  1016. * PHPTal. The most common template engine is PHPTemplate (included with
  1017. * Drupal and implemented in phptemplate.engine, which uses Drupal's default
  1018. * template renderer.
  1019. *
  1020. * In order to create theme-specific implementations of these hooks,
  1021. * themes can implement their own version of theme hooks, either as functions
  1022. * or templates. These implementations will be used instead of the default
  1023. * implementation. If using a pure .theme without an engine, the .theme is
  1024. * required to implement its own version of hook_theme() to tell Drupal what
  1025. * it is implementing; themes utilizing an engine will have their well-named
  1026. * theming functions automatically registered for them. While this can vary
  1027. * based upon the theme engine, the standard set by phptemplate is that theme
  1028. * functions should be named either phptemplate_HOOK or THEMENAME_HOOK. For
  1029. * example, for Drupal's default theme (Garland) to implement the 'table' hook,
  1030. * the phptemplate.engine would find phptemplate_table() or garland_table().
  1031. * The ENGINE_HOOK() syntax is preferred, as this can be used by sub-themes
  1032. * (which are themes that share code but use different stylesheets).
  1033. *
  1034. * The theme system is described and defined in theme.inc.
  1035. *
  1036. * @see theme()
  1037. * @see hook_theme()
  1038. */
  1039. /**
  1040. * Formats text for emphasized display in a placeholder inside a sentence.
  1041. * Used automatically by t().
  1042. *
  1043. * @param $text
  1044. * The text to format (plain-text).
  1045. * @return
  1046. * The formatted text (html).
  1047. */
  1048. function theme_placeholder($text) {
  1049. return '<em>'. check_plain($text) .'</em>';
  1050. }
  1051. /**
  1052. * Return a themed set of status and/or error messages. The messages are grouped
  1053. * by type.
  1054. *
  1055. * @param $display
  1056. * (optional) Set to 'status' or 'error' to display only messages of that type.
  1057. *
  1058. * @return
  1059. * A string containing the messages.
  1060. */
  1061. function theme_status_messages($display = NULL) {
  1062. $output = '';
  1063. foreach (drupal_get_messages($display) as $type => $messages) {
  1064. $output .= "<div class=\"messages $type\">\n";
  1065. if (count($messages) > 1) {
  1066. $output .= " <ul>\n";
  1067. foreach ($messages as $message) {
  1068. $output .= ' <li>'. $message ."</li>\n";
  1069. }
  1070. $output .= " </ul>\n";
  1071. }
  1072. else {
  1073. $output .= $messages[0];
  1074. }
  1075. $output .= "</div>\n";
  1076. }
  1077. return $output;
  1078. }
  1079. /**
  1080. * Return a themed set of links.
  1081. *
  1082. * @param $links
  1083. * A keyed array of links to be themed.
  1084. * @param $attributes
  1085. * A keyed array of attributes
  1086. * @return
  1087. * A string containing an unordered list of links.
  1088. */
  1089. function theme_links($links, $attributes = array('class' => 'links')) {
  1090. global $language;
  1091. $output = '';
  1092. if (count($links) > 0) {
  1093. $output = '<ul'. drupal_attributes($attributes) .'>';
  1094. $num_links = count($links);
  1095. $i = 1;
  1096. foreach ($links as $key => $link) {
  1097. $class = $key;
  1098. // Add first, last and active classes to the list of links to help out themers.
  1099. if ($i == 1) {
  1100. $class .= ' first';
  1101. }
  1102. if ($i == $num_links) {
  1103. $class .= ' last';
  1104. }
  1105. if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
  1106. && (empty($link['language']) || $link['language']->language == $language->language)) {
  1107. $class .= ' active';
  1108. }
  1109. $output .= '<li'. drupal_attributes(array('class' => $class)) .'>';
  1110. if (isset($link['href'])) {
  1111. // Pass in $link as $options, they share the same keys.
  1112. $output .= l($link['title'], $link['href'], $link);
  1113. }
  1114. else if (!empty($link['title'])) {
  1115. // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
  1116. if (empty($link['html'])) {
  1117. $link['title'] = check_plain($link['title']);
  1118. }
  1119. $span_attributes = '';
  1120. if (isset($link['attributes'])) {
  1121. $span_attributes = drupal_attributes($link['attributes']);
  1122. }
  1123. $output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
  1124. }
  1125. $i++;
  1126. $output .= "</li>\n";
  1127. }
  1128. $output .= '</ul>';
  1129. }
  1130. return $output;
  1131. }
  1132. /**
  1133. * Return a themed image.
  1134. *
  1135. * @param $path
  1136. * Either the path of the image file (relative to base_path()) or a full URL.
  1137. * If this is a full URL, $getsize must be set to FALSE or nothing will be returned.
  1138. * @param $alt
  1139. * The alternative text for text-based browsers.
  1140. * @param $title
  1141. * The title text is displayed when the image is hovered in some popular browsers.
  1142. * @param $attributes
  1143. * Associative array of attributes to be placed in the img tag.
  1144. * @param $getsize
  1145. * If set to TRUE, the image's dimension are fetched and added as width/height attributes.
  1146. * Defaults to TRUE. Must be set to FALSE if $path is a full URL.
  1147. *
  1148. * @return
  1149. * A string containing the image tag.
  1150. */
  1151. function theme_image($path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
  1152. if (!$getsize || (is_file($path) && (list($width, $height, $type, $image_attributes) = @getimagesize($path)))) {
  1153. $attributes = drupal_attributes($attributes);
  1154. $url = (url($path) == $path) ? $path : (base_path() . $path);
  1155. return '<img src="'. check_url($url) .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. (isset($image_attributes) ? $image_attributes : '') . $attributes .' />';
  1156. }
  1157. }
  1158. /**
  1159. * Return a themed breadcrumb trail.
  1160. *
  1161. * @param $breadcrumb
  1162. * An array containing the breadcrumb links.
  1163. * @return a string containing the breadcrumb output.
  1164. */
  1165. function theme_breadcrumb($breadcrumb) {
  1166. if (!empty($breadcrumb)) {
  1167. return '<div class="breadcrumb">'. implode(' Âť ', $breadcrumb) .'</div>';
  1168. }
  1169. }
  1170. /**
  1171. * Return a themed help message.
  1172. *
  1173. * @return a string containing the helptext for the current page.
  1174. */
  1175. function theme_help() {
  1176. if ($help = menu_get_active_help()) {
  1177. return '<div class="help">'. $help .'</div>';
  1178. }
  1179. }
  1180. /**
  1181. * Return a themed submenu, typically displayed under the tabs.
  1182. *
  1183. * @param $links
  1184. * An array of links.
  1185. */
  1186. function theme_submenu($links) {
  1187. return '<div class="submenu">'. implode(' | ', $links) .'</div>';
  1188. }
  1189. /**
  1190. * Return a themed table.
  1191. *
  1192. * @param $header
  1193. * An array containing the table headers. Each element of the array can be
  1194. * either a localized string or an associative array with the following keys:
  1195. * - "data": The localized title of the table column.
  1196. * - "field": The database field represented in the table column (required if
  1197. * user is to be able to sort on this column).
  1198. * - "sort": A default sort order for this column ("asc" or "desc").
  1199. * - Any HTML attributes, such as "colspan", to apply to the column header cell.
  1200. * @param $rows
  1201. * An array of table rows. Every row is an array of cells, or an associative
  1202. * array with the following keys:
  1203. * - "data": an array of cells
  1204. * - Any HTML attributes, such as "class", to apply to the table row.
  1205. *
  1206. * Each cell can be either a string or an associative array with the following keys:
  1207. * - "data": The string to display in the table cell.
  1208. * - "header": Indicates this cell is a header.
  1209. * - Any HTML attributes, such as "colspan", to apply to the table cell.
  1210. *
  1211. * Here's an example for $rows:
  1212. * @code
  1213. * $rows = array(
  1214. * // Simple row
  1215. * array(
  1216. * 'Cell 1', 'Cell 2', 'Cell 3'
  1217. * ),
  1218. * // Row with attributes on the row and some of its cells.
  1219. * array(
  1220. * 'data' => array('Cell 1', array('data' => 'Cell 2', 'colspan' => 2)), 'class' => 'funky'
  1221. * )
  1222. * );
  1223. * @endcode
  1224. *
  1225. * @param $attributes
  1226. * An array of HTML attributes to apply to the table tag.
  1227. * @param $caption
  1228. * A localized string to use for the <caption> tag.
  1229. * @return
  1230. * An HTML string representing the table.
  1231. */
  1232. function theme_table($header, $rows, $attributes = array(), $caption = NULL) {
  1233. // Add sticky headers, if applicable.
  1234. if (count($header)) {
  1235. drupal_add_js('misc/tableheader.js');
  1236. // Add 'sticky-enabled' class to the table to identify it for JS.
  1237. // This is needed to target tables constructed by this function.
  1238. $attributes['class'] = empty($attributes['class']) ? 'sticky-enabled' : ($attributes['class'] .' sticky-enabled');
  1239. }
  1240. $output = '<table'. drupal_attributes($attributes) .">\n";
  1241. if (isset($caption)) {
  1242. $output .= '<caption>'. $caption ."</caption>\n";
  1243. }
  1244. // Format the table header:
  1245. if (count($header)) {
  1246. $ts = tablesort_init($header);
  1247. // HTML requires that the thead tag has tr tags in it followed by tbody
  1248. // tags. Using ternary operator to check and see if we have any rows.
  1249. $output .= (count($rows) ? ' <thead><tr>' : ' <tr>');
  1250. foreach ($header as $cell) {
  1251. $cell = tablesort_header($cell, $header, $ts);
  1252. $output .= _theme_table_cell($cell, TRUE);
  1253. }
  1254. // Using ternary operator to close the tags based on whether or not there are rows
  1255. $output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
  1256. }
  1257. else {
  1258. $ts = array();
  1259. }
  1260. // Format the table rows:
  1261. if (count($rows)) {
  1262. $output .= "<tbody>\n";
  1263. $flip = array('even' => 'odd', 'odd' => 'even');
  1264. $class = 'even';
  1265. foreach ($rows as $number => $row) {
  1266. $attributes = array();
  1267. // Check if we're dealing with a simple or complex row
  1268. if (isset($row['data'])) {
  1269. foreach ($row as $key => $value) {
  1270. if ($key == 'data') {
  1271. $cells = $value;
  1272. }
  1273. else {
  1274. $attributes[$key] = $value;
  1275. }
  1276. }
  1277. }
  1278. else {
  1279. $cells = $row;
  1280. }
  1281. if (count($cells)) {
  1282. // Add odd/even class
  1283. $class = $flip[$class];
  1284. if (isset($attributes['class'])) {

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