PageRenderTime 55ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/theme.inc

https://bitbucket.org/sanjeevam/taxation
Pascal | 2043 lines | 1159 code | 112 blank | 772 comment | 166 complexity | d7c23d2f4e2d812259b6d76ca87de10d MD5 | raw file

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

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