PageRenderTime 129ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/theme.inc

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