theme.inc 113 KB

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