Skip navigation
Help

theme.inc

  1. drupal
    1. 6 drupal/includes/theme.inc
    2. 7 drupal/includes/theme.inc

The theme system, which controls the output of Drupal.

The theme system allows for nearly all output of the Drupal system to be customized by user themes.

Functions & methods

NameDescription
drupal_find_theme_functionsAllow themes and/or theme engines to easily discover overridden theme functions.
drupal_find_theme_templatesAllow themes and/or theme engines to easily discover overridden templates.
drupal_theme_accessDetermines if a theme is available to use.
drupal_theme_initializeInitialize the theme system by loading the theme.
drupal_theme_rebuildForce the system to rebuild the theme registry; this should be called when modules are added to the system, or when a dynamic system needs to add more theme hooks.
list_themesReturn a list of all currently available themes.
path_to_themeReturn the path to the current themed element.
template_preprocessAdds a default set of helper variables for variable processors and templates. This comes in before any other preprocess function which makes it possible to be used in default theme implementations (non-overridden theme functions).
template_preprocess_htmlPreprocess variables for html.tpl.php
template_preprocess_maintenance_pageThe variables array generated here is a mirror of template_preprocess_page(). This preprocessor will run its course when theme_maintenance_page() is invoked.
template_preprocess_pagePreprocess variables for page.tpl.php
template_preprocess_regionPreprocess variables for region.tpl.php
template_preprocess_usernamePreprocesses variables for theme_username().
template_processA default process function used to alter variables as late as possible.
template_process_htmlProcess variables for html.tpl.php
template_process_maintenance_pageThe variables array generated here is a mirror of template_process_html(). This processor will run its course when theme_maintenance_page() is invoked.
template_process_pageProcess variables for page.tpl.php
template_process_usernameProcesses variables for theme_username().
themeGenerates themed output.
theme_breadcrumbReturns HTML for a breadcrumb trail.
theme_disableDisable a given list of themes.
theme_enableEnable a given list of themes.
theme_feed_iconReturns HTML for a feed icon.
theme_get_registryGet the theme registry.
theme_get_settingRetrieve a setting for the current theme or for a given theme.
theme_get_suggestionsGenerate an array of suggestions from path arguments.
theme_html_tagReturns HTML for a generic HTML tag with attributes.
theme_imageReturns HTML for an image.
theme_indentationReturns HTML for an indentation div; used for drag and drop tables.
theme_item_listReturns HTML for a list or nested list of items.
theme_linkReturns HTML for a link.
theme_linksReturns HTML for a set of links.
theme_markReturns HTML for a marker for new or updated content.
theme_more_help_linkReturns HTML for a "more help" link.
theme_more_linkReturns HTML for a "more" link, like those used in blocks.
theme_progress_barReturns HTML for a progress bar.
theme_render_templateRender a system default template, which is essentially a PHP template.
theme_status_messagesReturns HTML for status and/or error messages, grouped by type.
theme_tableReturns HTML for a table.
theme_tablesort_indicatorReturns HTML for a sort icon.
theme_usernameReturns HTML for a username, potentially linked to the user's page.
_drupal_theme_accessHelper function for determining access to a theme.
_drupal_theme_initializeInitialize the theme system given already loaded information. This function is useful to initialize a theme when no database is present.
_template_preprocess_default_variablesReturns hook-independant variables to template_preprocess().
_theme_build_registryRebuild the theme registry cache.
_theme_load_registryGet the theme_registry cache from the database; if it doesn't exist, build it.
_theme_process_registryProcess a single implementation of hook_theme().
_theme_registry_callbackSet the callback that will be used by theme_get_registry() to fetch the registry.
_theme_save_registryWrite the theme_registry cache into the database.
_theme_table_cellReturns HTML output for a single table cell for theme_table().

Constants

NameDescription
MARK_NEWMark content as being new.
MARK_READMark content as read.
MARK_UPDATEDMark content as being updated.

File

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