theme.inc 107 KB

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