PageRenderTime 68ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/theme.inc

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