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

/includes/theme.inc

https://bitbucket.org/robbiethegeek/robbie-drupal7
Pascal | 2579 lines | 1498 code | 123 blank | 958 comment | 204 complexity | 3ef1224115552e38fec3098bc9875374 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // $Id: theme.inc,v 1.618 2010/10/05 19:59:10 dries Exp $
  3. /**
  4. * @file
  5. * The theme system, which controls the output of Drupal.
  6. *
  7. * The theme system allows for nearly all output of the Drupal system to be
  8. * customized by user themes.
  9. */
  10. /**
  11. * @name Content markers
  12. * @{
  13. * Markers used by theme_mark() and node_mark() to designate content.
  14. * @see theme_mark(), node_mark()
  15. */
  16. /**
  17. * Mark content as read.
  18. */
  19. define('MARK_READ', 0);
  20. /**
  21. * Mark content as being new.
  22. */
  23. define('MARK_NEW', 1);
  24. /**
  25. * Mark content as being updated.
  26. */
  27. define('MARK_UPDATED', 2);
  28. /**
  29. * @} End of "Content markers".
  30. */
  31. /**
  32. * Determines if a theme is available to use.
  33. *
  34. * @param $theme
  35. * Either the name of a theme or a full theme object.
  36. *
  37. * @return
  38. * Boolean TRUE if the theme is enabled or is the site administration theme;
  39. * FALSE otherwise.
  40. */
  41. function drupal_theme_access($theme) {
  42. if (is_object($theme)) {
  43. return _drupal_theme_access($theme);
  44. }
  45. else {
  46. $themes = list_themes();
  47. return isset($themes[$theme]) && _drupal_theme_access($themes[$theme]);
  48. }
  49. }
  50. /**
  51. * Helper function for determining access to a theme.
  52. *
  53. * @see drupal_theme_access()
  54. */
  55. function _drupal_theme_access($theme) {
  56. $admin_theme = variable_get('admin_theme');
  57. return !empty($theme->status) || ($admin_theme && $theme->name == $admin_theme);
  58. }
  59. /**
  60. * Initialize the theme system by loading the theme.
  61. */
  62. function drupal_theme_initialize() {
  63. global $theme, $user, $theme_key;
  64. // If $theme is already set, assume the others are set, too, and do nothing
  65. if (isset($theme)) {
  66. return;
  67. }
  68. drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
  69. $themes = list_themes();
  70. // Only select the user selected theme if it is available in the
  71. // list of themes that can be accessed.
  72. $theme = !empty($user->theme) && drupal_theme_access($user->theme) ? $user->theme : variable_get('theme_default', 'bartik');
  73. // Allow modules to override the theme. Validation has already been performed
  74. // inside menu_get_custom_theme(), so we do not need to check it again here.
  75. $custom_theme = menu_get_custom_theme();
  76. $theme = !empty($custom_theme) ? $custom_theme : $theme;
  77. // Store the identifier for retrieving theme settings with.
  78. $theme_key = $theme;
  79. // Find all our ancestor themes and put them in an array.
  80. $base_theme = array();
  81. $ancestor = $theme;
  82. while ($ancestor && isset($themes[$ancestor]->base_theme)) {
  83. $ancestor = $themes[$ancestor]->base_theme;
  84. $base_theme[] = $themes[$ancestor];
  85. }
  86. _drupal_theme_initialize($themes[$theme], array_reverse($base_theme));
  87. // Themes can have alter functions, so reset the drupal_alter() cache.
  88. drupal_static_reset('drupal_alter');
  89. // Provide the page with information about the theme that's used, so that a
  90. // later AJAX request can be rendered using the same theme.
  91. // @see ajax_base_page_theme()
  92. $setting['ajaxPageState'] = array(
  93. 'theme' => $theme_key,
  94. 'themeToken' => drupal_get_token($theme_key),
  95. );
  96. drupal_add_js($setting, 'setting');
  97. }
  98. /**
  99. * Initialize the theme system given already loaded information. This
  100. * function is useful to initialize a theme when no database is present.
  101. *
  102. * @param $theme
  103. * An object with the following information:
  104. * filename
  105. * The .info file for this theme. The 'path' to
  106. * the theme will be in this file's directory. (Required)
  107. * owner
  108. * The path to the .theme file or the .engine file to load for
  109. * the theme. (Required)
  110. * stylesheet
  111. * The primary stylesheet for the theme. (Optional)
  112. * engine
  113. * The name of theme engine to use. (Optional)
  114. * @param $base_theme
  115. * An optional array of objects that represent the 'base theme' if the
  116. * theme is meant to be derivative of another theme. It requires
  117. * the same information as the $theme object. It should be in
  118. * 'oldest first' order, meaning the top level of the chain will
  119. * be first.
  120. * @param $registry_callback
  121. * The callback to invoke to set the theme registry.
  122. */
  123. function _drupal_theme_initialize($theme, $base_theme = array(), $registry_callback = '_theme_load_registry') {
  124. global $theme_info, $base_theme_info, $theme_engine, $theme_path;
  125. $theme_info = $theme;
  126. $base_theme_info = $base_theme;
  127. $theme_path = dirname($theme->filename);
  128. // Prepare stylesheets from this theme as well as all ancestor themes.
  129. // We work it this way so that we can have child themes override parent
  130. // theme stylesheets easily.
  131. $final_stylesheets = array();
  132. // Grab stylesheets from base theme
  133. foreach ($base_theme as $base) {
  134. if (!empty($base->stylesheets)) {
  135. foreach ($base->stylesheets as $media => $stylesheets) {
  136. foreach ($stylesheets as $name => $stylesheet) {
  137. $final_stylesheets[$media][$name] = $stylesheet;
  138. }
  139. }
  140. }
  141. }
  142. // Add stylesheets used by this theme.
  143. if (!empty($theme->stylesheets)) {
  144. foreach ($theme->stylesheets as $media => $stylesheets) {
  145. foreach ($stylesheets as $name => $stylesheet) {
  146. $final_stylesheets[$media][$name] = $stylesheet;
  147. }
  148. }
  149. }
  150. // And now add the stylesheets properly
  151. foreach ($final_stylesheets as $media => $stylesheets) {
  152. foreach ($stylesheets as $stylesheet) {
  153. drupal_add_css($stylesheet, array('group' => CSS_THEME, 'every_page' => TRUE, 'media' => $media));
  154. }
  155. }
  156. // Do basically the same as the above for scripts
  157. $final_scripts = array();
  158. // Grab scripts from base theme
  159. foreach ($base_theme as $base) {
  160. if (!empty($base->scripts)) {
  161. foreach ($base->scripts as $name => $script) {
  162. $final_scripts[$name] = $script;
  163. }
  164. }
  165. }
  166. // Add scripts used by this theme.
  167. if (!empty($theme->scripts)) {
  168. foreach ($theme->scripts as $name => $script) {
  169. $final_scripts[$name] = $script;
  170. }
  171. }
  172. // Add scripts used by this theme.
  173. foreach ($final_scripts as $script) {
  174. drupal_add_js($script, array('group' => JS_THEME, 'every_page' => TRUE));
  175. }
  176. $theme_engine = NULL;
  177. // Initialize the theme.
  178. if (isset($theme->engine)) {
  179. // Include the engine.
  180. include_once DRUPAL_ROOT . '/' . $theme->owner;
  181. $theme_engine = $theme->engine;
  182. if (function_exists($theme_engine . '_init')) {
  183. foreach ($base_theme as $base) {
  184. call_user_func($theme_engine . '_init', $base);
  185. }
  186. call_user_func($theme_engine . '_init', $theme);
  187. }
  188. }
  189. else {
  190. // include non-engine theme files
  191. foreach ($base_theme as $base) {
  192. // Include the theme file or the engine.
  193. if (!empty($base->owner)) {
  194. include_once DRUPAL_ROOT . '/' . $base->owner;
  195. }
  196. }
  197. // and our theme gets one too.
  198. if (!empty($theme->owner)) {
  199. include_once DRUPAL_ROOT . '/' . $theme->owner;
  200. }
  201. }
  202. if (isset($registry_callback)) {
  203. _theme_registry_callback($registry_callback, array($theme, $base_theme, $theme_engine));
  204. }
  205. }
  206. /**
  207. * Get the theme registry.
  208. *
  209. * @return
  210. * The theme registry array if it has been stored in memory, NULL otherwise.
  211. */
  212. function theme_get_registry() {
  213. static $theme_registry = NULL;
  214. if (!isset($theme_registry)) {
  215. list($callback, $arguments) = _theme_registry_callback();
  216. $theme_registry = call_user_func_array($callback, $arguments);
  217. }
  218. return $theme_registry;
  219. }
  220. /**
  221. * Set the callback that will be used by theme_get_registry() to fetch the registry.
  222. *
  223. * @param $callback
  224. * The name of the callback function.
  225. * @param $arguments
  226. * The arguments to pass to the function.
  227. */
  228. function _theme_registry_callback($callback = NULL, array $arguments = array()) {
  229. static $stored;
  230. if (isset($callback)) {
  231. $stored = array($callback, $arguments);
  232. }
  233. return $stored;
  234. }
  235. /**
  236. * Get the theme_registry cache from the database; if it doesn't exist, build it.
  237. *
  238. * @param $theme
  239. * The loaded $theme object as returned by list_themes().
  240. * @param $base_theme
  241. * An array of loaded $theme objects representing the ancestor themes in
  242. * oldest first order.
  243. * @param theme_engine
  244. * The name of the theme engine.
  245. */
  246. function _theme_load_registry($theme, $base_theme = NULL, $theme_engine = NULL) {
  247. // Check the theme registry cache; if it exists, use it.
  248. $cache = cache_get("theme_registry:$theme->name", 'cache');
  249. if (isset($cache->data)) {
  250. $registry = $cache->data;
  251. }
  252. else {
  253. // If not, build one and cache it.
  254. $registry = _theme_build_registry($theme, $base_theme, $theme_engine);
  255. // Only persist this registry if all modules are loaded. This assures a
  256. // complete set of theme hooks.
  257. if (module_load_all(NULL)) {
  258. _theme_save_registry($theme, $registry);
  259. }
  260. }
  261. return $registry;
  262. }
  263. /**
  264. * Write the theme_registry cache into the database.
  265. */
  266. function _theme_save_registry($theme, $registry) {
  267. cache_set("theme_registry:$theme->name", $registry);
  268. }
  269. /**
  270. * Force the system to rebuild the theme registry; this should be called
  271. * when modules are added to the system, or when a dynamic system needs
  272. * to add more theme hooks.
  273. */
  274. function drupal_theme_rebuild() {
  275. cache_clear_all('theme_registry', 'cache', TRUE);
  276. }
  277. /**
  278. * Process a single implementation of hook_theme().
  279. *
  280. * @param $cache
  281. * The theme registry that will eventually be cached; It is an associative
  282. * array keyed by theme hooks, whose values are associative arrays describing
  283. * the hook:
  284. * - 'type': The passed in $type.
  285. * - 'theme path': The passed in $path.
  286. * - 'function': The name of the function generating output for this theme
  287. * hook. Either defined explicitly in hook_theme() or, if neither 'function'
  288. * nor 'template' is defined, then the default theme function name is used.
  289. * The default theme function name is the theme hook prefixed by either
  290. * 'theme_' for modules or '$name_' for everything else. If 'function' is
  291. * defined, 'template' is not used.
  292. * - 'template': The filename of the template generating output for this
  293. * theme hook. The template is in the directory defined by the 'path' key of
  294. * hook_theme() or defaults to $path.
  295. * - 'variables': The variables for this theme hook as defined in
  296. * hook_theme(). If there is more than one implementation and 'variables' is
  297. * not specified in a later one, then the previous definition is kept.
  298. * - 'render element': The renderable element for this theme hook as defined
  299. * in hook_theme(). If there is more than one implementation and
  300. * 'render element' is not specified in a later one, then the previous
  301. * definition is kept.
  302. * - 'preprocess functions': See theme() for detailed documentation.
  303. * - 'process functions': See theme() for detailed documentation.
  304. * @param $name
  305. * The name of the module, theme engine, base theme engine, theme or base
  306. * theme implementing hook_theme().
  307. * @param $type
  308. * One of 'module', 'theme_engine', 'base_theme_engine', 'theme', or
  309. * 'base_theme'. Unlike regular hooks that can only be implemented by modules,
  310. * each of these can implement hook_theme(). _theme_process_registry() is
  311. * called in aforementioned order and new entries override older ones. For
  312. * example, if a theme hook is both defined by a module and a theme, then the
  313. * definition in the theme will be used.
  314. * @param $theme
  315. * The loaded $theme object as returned from list_themes().
  316. * @param $path
  317. * The directory where $name is. For example, modules/system or
  318. * themes/bartik.
  319. *
  320. * @see theme()
  321. * @see _theme_process_registry()
  322. * @see hook_theme()
  323. * @see list_themes()
  324. */
  325. function _theme_process_registry(&$cache, $name, $type, $theme, $path) {
  326. $result = array();
  327. $function = $name . '_theme';
  328. // Processor functions work in two distinct phases with the process
  329. // functions always being executed after the preprocess functions.
  330. $variable_process_phases = array(
  331. 'preprocess functions' => 'preprocess',
  332. 'process functions' => 'process',
  333. );
  334. if (function_exists($function)) {
  335. $result = $function($cache, $type, $theme, $path);
  336. foreach ($result as $hook => $info) {
  337. $result[$hook]['type'] = $type;
  338. $result[$hook]['theme path'] = $path;
  339. // if function and file are left out, default to standard naming
  340. // conventions.
  341. if (!isset($info['template']) && !isset($info['function'])) {
  342. $result[$hook]['function'] = ($type == 'module' ? 'theme_' : $name . '_') . $hook;
  343. }
  344. // If a path is set in the info, use what was set. Otherwise use the
  345. // default path. This is mostly so system.module can declare theme
  346. // functions on behalf of core .include files.
  347. // All files are included to be safe. Conditionally included
  348. // files can prevent them from getting registered.
  349. if (isset($cache[$hook]['includes'])) {
  350. $result[$hook]['includes'] = $cache[$hook]['includes'];
  351. }
  352. if (isset($info['file'])) {
  353. $include_file = isset($info['path']) ? $info['path'] : $path;
  354. $include_file .= '/' . $info['file'];
  355. include_once DRUPAL_ROOT . '/' . $include_file;
  356. $result[$hook]['includes'][] = $include_file;
  357. }
  358. // If these keys are left unspecified within overridden entries returned
  359. // by hook_theme(), carry them forward from the prior entry. This is so
  360. // that themes don't need to specify this information, since the module
  361. // that registered the theme hook already has.
  362. foreach (array('variables', 'render element', 'pattern', 'base hook') as $key) {
  363. if (!array_key_exists($key, $info) && isset($cache[$hook][$key])) {
  364. $result[$hook][$key] = $cache[$hook][$key];
  365. }
  366. }
  367. // The following apply only to theming hooks implemented as templates.
  368. if (isset($info['template'])) {
  369. // Prepend the current theming path when none is set.
  370. if (!isset($info['path'])) {
  371. $result[$hook]['template'] = $path . '/' . $info['template'];
  372. }
  373. }
  374. // Allow variable processors for all theming hooks, whether the hook is
  375. // implemented as a template or as a function.
  376. foreach ($variable_process_phases as $phase_key => $phase) {
  377. // Check for existing variable processors. Ensure arrayness.
  378. if (!isset($info[$phase_key]) || !is_array($info[$phase_key])) {
  379. $info[$phase_key] = array();
  380. $prefixes = array();
  381. if ($type == 'module') {
  382. // Default variable processor prefix.
  383. $prefixes[] = 'template';
  384. // Add all modules so they can intervene with their own variable
  385. // processors. This allows them to provide variable processors even
  386. // if they are not the owner of the current hook.
  387. $prefixes += module_list();
  388. }
  389. elseif ($type == 'theme_engine' || $type == 'base_theme_engine') {
  390. // Theme engines get an extra set that come before the normally
  391. // named variable processors.
  392. $prefixes[] = $name . '_engine';
  393. // The theme engine registers on behalf of the theme using the
  394. // theme's name.
  395. $prefixes[] = $theme;
  396. }
  397. else {
  398. // This applies when the theme manually registers their own variable
  399. // processors.
  400. $prefixes[] = $name;
  401. }
  402. foreach ($prefixes as $prefix) {
  403. // Only use non-hook-specific variable processors for theming hooks
  404. // implemented as templates. See theme().
  405. if (isset($info['template']) && function_exists($prefix . '_' . $phase)) {
  406. $info[$phase_key][] = $prefix . '_' . $phase;
  407. }
  408. if (function_exists($prefix . '_' . $phase . '_' . $hook)) {
  409. $info[$phase_key][] = $prefix . '_' . $phase . '_' . $hook;
  410. }
  411. }
  412. }
  413. // Check for the override flag and prevent the cached variable
  414. // processors from being used. This allows themes or theme engines to
  415. // remove variable processors set earlier in the registry build.
  416. if (!empty($info['override ' . $phase_key])) {
  417. // Flag not needed inside the registry.
  418. unset($result[$hook]['override ' . $phase_key]);
  419. }
  420. elseif (isset($cache[$hook][$phase_key]) && is_array($cache[$hook][$phase_key])) {
  421. $info[$phase_key] = array_merge($cache[$hook][$phase_key], $info[$phase_key]);
  422. }
  423. $result[$hook][$phase_key] = $info[$phase_key];
  424. }
  425. }
  426. // Merge the newly created theme hooks into the existing cache.
  427. $cache = array_merge($cache, $result);
  428. }
  429. // Let themes have variable processors even if they didn't register a template.
  430. if ($type == 'theme' || $type == 'base_theme') {
  431. foreach ($cache as $hook => $info) {
  432. // Check only if not registered by the theme or engine.
  433. if (empty($result[$hook])) {
  434. foreach ($variable_process_phases as $phase_key => $phase) {
  435. if (!isset($info[$phase_key])) {
  436. $cache[$hook][$phase_key] = array();
  437. }
  438. // Only use non-hook-specific variable processors for theming hooks
  439. // implemented as templates. See theme().
  440. if (isset($info['template']) && function_exists($name . '_' . $phase)) {
  441. $cache[$hook][$phase_key][] = $name . '_' . $phase;
  442. }
  443. if (function_exists($name . '_' . $phase . '_' . $hook)) {
  444. $cache[$hook][$phase_key][] = $name . '_' . $phase . '_' . $hook;
  445. $cache[$hook]['theme path'] = $path;
  446. }
  447. // Ensure uniqueness.
  448. $cache[$hook][$phase_key] = array_unique($cache[$hook][$phase_key]);
  449. }
  450. }
  451. }
  452. }
  453. }
  454. /**
  455. * Rebuild the theme registry cache.
  456. *
  457. * @param $theme
  458. * The loaded $theme object as returned by list_themes().
  459. * @param $base_theme
  460. * An array of loaded $theme objects representing the ancestor themes in
  461. * oldest first order.
  462. * @param theme_engine
  463. * The name of the theme engine.
  464. */
  465. function _theme_build_registry($theme, $base_theme, $theme_engine) {
  466. $cache = array();
  467. // First, process the theme hooks advertised by modules. This will
  468. // serve as the basic registry.
  469. foreach (module_implements('theme') as $module) {
  470. _theme_process_registry($cache, $module, 'module', $module, drupal_get_path('module', $module));
  471. }
  472. // Process each base theme.
  473. foreach ($base_theme as $base) {
  474. // If the base theme uses a theme engine, process its hooks.
  475. $base_path = dirname($base->filename);
  476. if ($theme_engine) {
  477. _theme_process_registry($cache, $theme_engine, 'base_theme_engine', $base->name, $base_path);
  478. }
  479. _theme_process_registry($cache, $base->name, 'base_theme', $base->name, $base_path);
  480. }
  481. // And then the same thing, but for the theme.
  482. if ($theme_engine) {
  483. _theme_process_registry($cache, $theme_engine, 'theme_engine', $theme->name, dirname($theme->filename));
  484. }
  485. // Finally, hooks provided by the theme itself.
  486. _theme_process_registry($cache, $theme->name, 'theme', $theme->name, dirname($theme->filename));
  487. // Let modules alter the registry.
  488. drupal_alter('theme_registry', $cache);
  489. // Optimize the registry to not have empty arrays for functions.
  490. foreach ($cache as $hook => $info) {
  491. foreach (array('preprocess functions', 'process functions') as $phase) {
  492. if (empty($info[$phase])) {
  493. unset($cache[$hook][$phase]);
  494. }
  495. }
  496. }
  497. return $cache;
  498. }
  499. /**
  500. * Return a list of all currently available themes.
  501. *
  502. * Retrieved from the database, if available and the site is not in maintenance
  503. * mode; otherwise compiled freshly from the filesystem.
  504. *
  505. * @param $refresh
  506. * Whether to reload the list of themes from the database. Defaults to FALSE.
  507. *
  508. * @return
  509. * An associative array of the currently available themes. The keys are the
  510. * names of the themes and the values are objects having the following
  511. * properties:
  512. * - 'filename': The name of the .info file.
  513. * - 'name': The name of the theme.
  514. * - 'status': 1 for enabled, 0 for disabled themes.
  515. * - 'info': The contents of the .info file.
  516. * - 'stylesheets': A two dimensional array, using the first key for the
  517. * 'media' attribute (e.g. 'all'), the second for the name of the file
  518. * (e.g. style.css). The value is a complete filepath
  519. * (e.g. themes/bartik/style.css).
  520. * - 'scripts': An associative array of JavaScripts, using the filename as key
  521. * and the complete filepath as value.
  522. * - 'engine': The name of the theme engine.
  523. * - 'base theme': The name of the base theme.
  524. */
  525. function list_themes($refresh = FALSE) {
  526. $list = &drupal_static(__FUNCTION__, array());
  527. if ($refresh) {
  528. $list = array();
  529. system_list_reset();
  530. }
  531. if (empty($list)) {
  532. $list = array();
  533. $themes = array();
  534. // Extract from the database only when it is available.
  535. // Also check that the site is not in the middle of an install or update.
  536. if (!defined('MAINTENANCE_MODE')) {
  537. try {
  538. $themes = system_list('theme');
  539. }
  540. catch (Exception $e) {
  541. // If the database is not available, rebuild the theme data.
  542. $themes = _system_rebuild_theme_data();
  543. }
  544. }
  545. else {
  546. // Scan the installation when the database should not be read.
  547. $themes = _system_rebuild_theme_data();
  548. }
  549. foreach ($themes as $theme) {
  550. foreach ($theme->info['stylesheets'] as $media => $stylesheets) {
  551. foreach ($stylesheets as $stylesheet => $path) {
  552. $theme->stylesheets[$media][$stylesheet] = $path;
  553. }
  554. }
  555. foreach ($theme->info['scripts'] as $script => $path) {
  556. $theme->scripts[$script] = $path;
  557. }
  558. if (isset($theme->info['engine'])) {
  559. $theme->engine = $theme->info['engine'];
  560. }
  561. if (isset($theme->info['base theme'])) {
  562. $theme->base_theme = $theme->info['base theme'];
  563. }
  564. // Status is normally retrieved from the database. Add zero values when
  565. // read from the installation directory to prevent notices.
  566. if (!isset($theme->status)) {
  567. $theme->status = 0;
  568. }
  569. $list[$theme->name] = $theme;
  570. }
  571. }
  572. return $list;
  573. }
  574. /**
  575. * Generates themed output.
  576. *
  577. * All requests for themed output must go through this function. It examines
  578. * the request and routes it to the appropriate theme function or template, by
  579. * checking the theme registry.
  580. *
  581. * The first argument to this function is the name of the theme hook. For
  582. * instance, to theme a table, the theme hook name is 'table'. By default, this
  583. * theme hook could be implemented by a function called 'theme_table' or a
  584. * template file called 'table.tpl.php', but hook_theme() can override the
  585. * default function or template name.
  586. *
  587. * If the implementation is a template file, several functions are called
  588. * before the template file is invoked, to modify the $variables array. These
  589. * fall into the "preprocessing" phase and the "processing" phase, and are
  590. * executed (if they exist), in the following order (note that in the following
  591. * list, HOOK indicates the theme hook name, MODULE indicates a module name,
  592. * THEME indicates a theme name, and ENGINE indicates a theme engine name):
  593. * - template_preprocess(&$variables, $hook): Creates a default set of variables
  594. * for all theme hooks.
  595. * - template_preprocess_HOOK(&$variables): Should be implemented by
  596. * the module that registers the theme hook, to set up default variables.
  597. * - MODULE_preprocess(&$variables, $hook): hook_preprocess() is invoked on all
  598. * implementing modules.
  599. * - MODULE_preprocess_HOOK(&$variables): hook_preprocess_HOOK() is invoked on
  600. * all implementing modules, so that modules that didn't define the theme hook
  601. * can alter the variables.
  602. * - ENGINE_engine_preprocess(&$variables, $hook): Allows the theme engine to
  603. * set necessary variables for all theme hooks.
  604. * - ENGINE_engine_preprocess_HOOK(&$variables): Allows the theme engine to set
  605. * necessary variables for the particular theme hook.
  606. * - THEME_preprocess(&$variables, $hook): Allows the theme to set necessary
  607. * variables for all theme hooks.
  608. * - THEME_preprocess_HOOK(&$variables): Allows the theme to set necessary
  609. * variables specific to the particular theme hook.
  610. * - template_process(&$variables, $hook): Creates a default set of variables
  611. * for all theme hooks.
  612. * - template_process_HOOK(&$variables): This is the first processor specific
  613. * to the theme hook; it should be implemented by the module that registers
  614. * it.
  615. * - MODULE_process(&$variables, $hook): hook_process() is invoked on all
  616. * implementing modules.
  617. * - MODULE_process_HOOK(&$variables): hook_process_HOOK() is invoked on
  618. * on all implementing modules, so that modules that didn't define the theme
  619. * hook can alter the variables.
  620. * - ENGINE_engine_process(&$variables, $hook): Allows the theme engine to set
  621. * necessary variables for all theme hooks.
  622. * - ENGINE_engine_process_HOOK(&$variables): Allows the theme engine to set
  623. * necessary variables for the particular theme hook.
  624. * - ENGINE_process(&$variables, $hook): Allows the theme engine to process the
  625. * variables.
  626. * - ENGINE_process_HOOK(&$variables): Allows the theme engine to process the
  627. * variables specific to the theme hook.
  628. * - THEME_process(&$variables, $hook): Allows the theme to process the
  629. * variables.
  630. * - THEME_process_HOOK(&$variables): Allows the theme to process the
  631. * variables specific to the theme hook.
  632. *
  633. * If the implementation is a function, only the theme-hook-specific preprocess
  634. * and process functions (the ones ending in _HOOK) are called from the
  635. * list above. This is because theme hooks with function implementations
  636. * need to be fast, and calling the non-theme-hook-specific preprocess and
  637. * process functions for them would incur a noticeable performance penalty.
  638. *
  639. * There are two special variables that these preprocess and process functions
  640. * can set: 'theme_hook_suggestion' and 'theme_hook_suggestions'. These will be
  641. * merged together to form a list of 'suggested' alternate theme hooks to use,
  642. * in reverse order of priority. theme_hook_suggestion will always be a higher
  643. * priority than items in theme_hook_suggestions. theme() will use the
  644. * highest priority implementation that exists. If none exists, theme() will
  645. * use the implementation for the theme hook it was called with. These
  646. * suggestions are similar to and are used for similar reasons as calling
  647. * theme() with an array as the $hook parameter (see below). The difference
  648. * is whether the suggestions are determined by the code that calls theme() or
  649. * by a preprocess or process function.
  650. *
  651. * @param $hook
  652. * The name of the theme hook to call. If the name contains a
  653. * double-underscore ('__') and there isn't an implementation for the full
  654. * name, the part before the '__' is checked. This allows a fallback to a more
  655. * generic implementation. For example, if theme('links__node', ...) is
  656. * called, but there is no implementation of that theme hook, then the 'links'
  657. * implementation is used. This process is iterative, so if
  658. * theme('links__contextual__node', ...) is called, theme() checks for the
  659. * following implementations, and uses the first one that exists:
  660. * - links__contextual__node
  661. * - links__contextual
  662. * - links
  663. * This allows themes to create specific theme implementations for named
  664. * objects and contexts of otherwise generic theme hooks. The $hook parameter
  665. * may also be an array, in which case the first theme hook that has an
  666. * implementation is used. This allows for the code that calls theme() to
  667. * explicitly specify the fallback order in a situation where using the '__'
  668. * convention is not desired or is insufficient.
  669. * @param $variables
  670. * An associative array of variables to merge with defaults from the theme
  671. * registry, pass to preprocess and process functions for modification, and
  672. * finally, pass to the function or template implementing the theme hook.
  673. * Alternatively, this can be a renderable array, in which case, its
  674. * properties are mapped to variables expected by the theme hook
  675. * implementations.
  676. *
  677. * @return
  678. * An HTML string representing the themed output.
  679. */
  680. function theme($hook, $variables = array()) {
  681. static $hooks = NULL;
  682. // If called before all modules are loaded, we do not necessarily have a full
  683. // theme registry to work with, and therefore cannot process the theme
  684. // request properly. See also _theme_load_registry().
  685. if (!module_load_all(NULL) && !defined('MAINTENANCE_MODE')) {
  686. throw new Exception(t('theme() may not be called until all modules are loaded.'));
  687. }
  688. if (!isset($hooks)) {
  689. drupal_theme_initialize();
  690. $hooks = theme_get_registry();
  691. }
  692. // If an array of hook candidates were passed, use the first one that has an
  693. // implementation.
  694. if (is_array($hook)) {
  695. foreach ($hook as $candidate) {
  696. if (isset($hooks[$candidate])) {
  697. break;
  698. }
  699. }
  700. $hook = $candidate;
  701. }
  702. // If there's no implementation, check for more generic fallbacks. If there's
  703. // still no implementation, log an error and return an empty string.
  704. if (!isset($hooks[$hook])) {
  705. // Iteratively strip everything after the last '__' delimiter, until an
  706. // implementation is found.
  707. while ($pos = strrpos($hook, '__')) {
  708. $hook = substr($hook, 0, $pos);
  709. if (isset($hooks[$hook])) {
  710. break;
  711. }
  712. }
  713. if (!isset($hooks[$hook])) {
  714. // Only log a message when not trying theme suggestions ($hook being an
  715. // array).
  716. if (!isset($candidate)) {
  717. watchdog('theme', 'Theme key "@key" not found.', array('@key' => $hook), WATCHDOG_WARNING);
  718. }
  719. return '';
  720. }
  721. }
  722. $info = $hooks[$hook];
  723. global $theme_path;
  724. $temp = $theme_path;
  725. // point path_to_theme() to the currently used theme path:
  726. $theme_path = $info['theme path'];
  727. // Include a file if the theme function or variable processor is held elsewhere.
  728. if (!empty($info['includes'])) {
  729. foreach ($info['includes'] as $include_file) {
  730. include_once DRUPAL_ROOT . '/' . $include_file;
  731. }
  732. }
  733. // If a renderable array is passed as $variables, then set $variables to
  734. // the arguments expected by the theme function.
  735. if (isset($variables['#theme']) || isset($variables['#theme_wrappers'])) {
  736. $element = $variables;
  737. $variables = array();
  738. if (isset($info['variables'])) {
  739. foreach (array_keys($info['variables']) as $name) {
  740. if (isset($element["#$name"])) {
  741. $variables[$name] = $element["#$name"];
  742. }
  743. }
  744. }
  745. else {
  746. $variables[$info['render element']] = $element;
  747. }
  748. }
  749. // Merge in argument defaults.
  750. if (!empty($info['variables'])) {
  751. $variables += $info['variables'];
  752. }
  753. elseif (!empty($info['render element'])) {
  754. $variables += array($info['render element'] => array());
  755. }
  756. // Invoke the variable processors, if any. The processors may specify
  757. // alternate suggestions for which hook's template/function to use. If the
  758. // hook is a suggestion of a base hook, invoke the variable processors of
  759. // the base hook, but retain the suggestion as a high priority suggestion to
  760. // be used unless overridden by a variable processor function.
  761. if (isset($info['base hook'])) {
  762. $base_hook = $info['base hook'];
  763. $base_hook_info = $hooks[$base_hook];
  764. if (isset($base_hook_info['preprocess functions']) || isset($base_hook_info['process functions'])) {
  765. $variables['theme_hook_suggestion'] = $hook;
  766. $hook = $base_hook;
  767. $info = $base_hook_info;
  768. }
  769. }
  770. if (isset($info['preprocess functions']) || isset($info['process functions'])) {
  771. $variables['theme_hook_suggestions'] = array();
  772. foreach (array('preprocess functions', 'process functions') as $phase) {
  773. if (!empty($info[$phase])) {
  774. foreach ($info[$phase] as $processor_function) {
  775. if (function_exists($processor_function)) {
  776. // We don't want a poorly behaved process function changing $hook.
  777. $hook_clone = $hook;
  778. $processor_function($variables, $hook_clone);
  779. }
  780. }
  781. }
  782. }
  783. // If the preprocess/process functions specified hook suggestions, and the
  784. // suggestion exists in the theme registry, use it instead of the hook that
  785. // theme() was called with. This allows the preprocess/process step to
  786. // route to a more specific theme hook. For example, a function may call
  787. // theme('node', ...), but a preprocess function can add 'node__article' as
  788. // a suggestion, enabling a theme to have an alternate template file for
  789. // article nodes. Suggestions are checked in the following order:
  790. // - The 'theme_hook_suggestion' variable is checked first. It overrides
  791. // all others.
  792. // - The 'theme_hook_suggestions' variable is checked in FILO order, so the
  793. // last suggestion added to the array takes precedence over suggestions
  794. // added earlier.
  795. $suggestions = array();
  796. if (!empty($variables['theme_hook_suggestions'])) {
  797. $suggestions = $variables['theme_hook_suggestions'];
  798. }
  799. if (!empty($variables['theme_hook_suggestion'])) {
  800. $suggestions[] = $variables['theme_hook_suggestion'];
  801. }
  802. foreach (array_reverse($suggestions) as $suggestion) {
  803. if (isset($hooks[$suggestion])) {
  804. $info = $hooks[$suggestion];
  805. break;
  806. }
  807. }
  808. }
  809. // Generate the output using either a function or a template.
  810. $output = '';
  811. if (isset($info['function'])) {
  812. if (function_exists($info['function'])) {
  813. $output = $info['function']($variables);
  814. }
  815. }
  816. else {
  817. // Default render function and extension.
  818. $render_function = 'theme_render_template';
  819. $extension = '.tpl.php';
  820. // The theme engine may use a different extension and a different renderer.
  821. global $theme_engine;
  822. if (isset($theme_engine)) {
  823. if ($info['type'] != 'module') {
  824. if (function_exists($theme_engine . '_render_template')) {
  825. $render_function = $theme_engine . '_render_template';
  826. }
  827. $extension_function = $theme_engine . '_extension';
  828. if (function_exists($extension_function)) {
  829. $extension = $extension_function();
  830. }
  831. }
  832. }
  833. // In some cases, a template implementation may not have had
  834. // template_preprocess() run (for example, if the default implementation is
  835. // a function, but a template overrides that default implementation). In
  836. // these cases, a template should still be able to expect to have access to
  837. // the variables provided by template_preprocess(), so we add them here if
  838. // they don't already exist. We don't want to run template_preprocess()
  839. // twice (it would be inefficient and mess up zebra striping), so we use the
  840. // 'directory' variable to determine if it has already run, which while not
  841. // completely intuitive, is reasonably safe, and allows us to save on the
  842. // overhead of adding some new variable to track that.
  843. if (!isset($variables['directory'])) {
  844. $default_template_variables = array();
  845. template_preprocess($default_template_variables, $hook);
  846. $variables += $default_template_variables;
  847. }
  848. // Render the output using the template file.
  849. $template_file = $info['template'] . $extension;
  850. if (isset($info['path'])) {
  851. $template_file = $info['path'] . '/' . $template_file;
  852. }
  853. $output = $render_function($template_file, $variables);
  854. }
  855. // restore path_to_theme()
  856. $theme_path = $temp;
  857. return $output;
  858. }
  859. /**
  860. * Return the path to the current themed element.
  861. *
  862. * It can point to the active theme or the module handling a themed implementation.
  863. * For example, when invoked within the scope of a theming call it will depend
  864. * on where the theming function is handled. If implemented from a module, it
  865. * will point to the module. If implemented from the active theme, it will point
  866. * to the active theme. When called outside the scope of a theming call, it will
  867. * always point to the active theme.
  868. */
  869. function path_to_theme() {
  870. global $theme_path;
  871. if (!isset($theme_path)) {
  872. drupal_theme_initialize();
  873. }
  874. return $theme_path;
  875. }
  876. /**
  877. * Allow themes and/or theme engines to easily discover overridden theme functions.
  878. *
  879. * @param $cache
  880. * The existing cache of theme hooks to test against.
  881. * @param $prefixes
  882. * An array of prefixes to test, in reverse order of importance.
  883. *
  884. * @return $implementations
  885. * The functions found, suitable for returning from hook_theme;
  886. */
  887. function drupal_find_theme_functions($cache, $prefixes) {
  888. $implementations = array();
  889. $functions = get_defined_functions();
  890. foreach ($cache as $hook => $info) {
  891. foreach ($prefixes as $prefix) {
  892. // Find theme functions that implement possible "suggestion" variants of
  893. // registered theme hooks and add those as new registered theme hooks.
  894. // The 'pattern' key defines a common prefix that all suggestions must
  895. // start with. The default is the name of the hook followed by '__'. An
  896. // 'base hook' key is added to each entry made for a found suggestion,
  897. // so that common functionality can be implemented for all suggestions of
  898. // the same base hook. To keep things simple, deep heirarchy of
  899. // suggestions is not supported: each suggestion's 'base hook' key
  900. // refers to a base hook, not to another suggestion, and all suggestions
  901. // are found using the base hook's pattern, not a pattern from an
  902. // intermediary suggestion.
  903. $pattern = isset($info['pattern']) ? $info['pattern'] : ($hook . '__');
  904. if (!isset($info['base hook']) && !empty($pattern)) {
  905. $matches = preg_grep('/^' . $prefix . '_' . $pattern . '/', $functions['user']);
  906. if ($matches) {
  907. foreach ($matches as $match) {
  908. $new_hook = str_replace($prefix . '_', '', $match);
  909. $arg_name = isset($info['variables']) ? 'variables' : 'render element';
  910. $implementations[$new_hook] = array(
  911. 'function' => $match,
  912. $arg_name => $info[$arg_name],
  913. 'base hook' => $hook,
  914. );
  915. }
  916. }
  917. }
  918. // Find theme functions that implement registered theme hooks and include
  919. // that in what is returned so that the registry knows that the theme has
  920. // this implementation.
  921. if (function_exists($prefix . '_' . $hook)) {
  922. $implementations[$hook] = array(
  923. 'function' => $prefix . '_' . $hook,
  924. );
  925. }
  926. }
  927. }
  928. return $implementations;
  929. }
  930. /**
  931. * Allow themes and/or theme engines to easily discover overridden templates.
  932. *
  933. * @param $cache
  934. * The existing cache of theme hooks to test against.
  935. * @param $extension
  936. * The extension that these templates will have.
  937. * @param $path
  938. * The path to search.
  939. */
  940. function drupal_find_theme_templates($cache, $extension, $path) {
  941. $implementations = array();
  942. // Collect paths to all sub-themes grouped by base themes. These will be
  943. // used for filtering. This allows base themes to have sub-themes in its
  944. // folder hierarchy without affecting the base themes template discovery.
  945. $theme_paths = array();
  946. foreach (list_themes() as $theme_info) {
  947. if (!empty($theme_info->base_theme)) {
  948. $theme_paths[$theme_info->base_theme][$theme_info->name] = dirname($theme_info->filename);
  949. }
  950. }
  951. foreach ($theme_paths as $basetheme => $subthemes) {
  952. foreach ($subthemes as $subtheme => $subtheme_path) {
  953. if (isset($theme_paths[$subtheme])) {
  954. $theme_paths[$basetheme] = array_merge($theme_paths[$basetheme], $theme_paths[$subtheme]);
  955. }
  956. }
  957. }
  958. global $theme;
  959. $subtheme_paths = isset($theme_paths[$theme]) ? $theme_paths[$theme] : array();
  960. // Escape the periods in the extension.
  961. $regex = '/' . str_replace('.', '\.', $extension) . '$/';
  962. // Get a listing of all template files in the path to search.
  963. $files = drupal_system_listing($regex, $path, 'name', 0);
  964. // Find templates that implement registered theme hooks and include that in
  965. // what is returned so that the registry knows that the theme has this
  966. // implementation.
  967. foreach ($files as $template => $file) {
  968. // Ignore sub-theme templates for the current theme.
  969. if (strpos($file->uri, str_replace($subtheme_paths, '', $file->uri)) !== 0) {
  970. continue;
  971. }
  972. // Chop off the remaining extensions if there are any. $template already
  973. // has the rightmost extension removed, but there might still be more,
  974. // such as with .tpl.php, which still has .tpl in $template at this point.
  975. if (($pos = strpos($template, '.')) !== FALSE) {
  976. $template = substr($template, 0, $pos);
  977. }
  978. // Transform - in filenames to _ to match function naming scheme
  979. // for the purposes of searching.
  980. $hook = strtr($template, '-', '_');
  981. if (isset($cache[$hook])) {
  982. $implementations[$hook] = array(
  983. 'template' => $template,
  984. 'path' => dirname($file->uri),
  985. );
  986. }
  987. }
  988. // Find templates that implement possible "suggestion" variants of registered
  989. // theme hooks and add those as new registered theme hooks. See
  990. // drupal_find_theme_functions() for more information about suggestions and
  991. // the use of 'pattern' and 'base hook'.
  992. $patterns = array_keys($files);
  993. foreach ($cache as $hook => $info) {
  994. $pattern = isset($info['pattern']) ? $info['pattern'] : ($hook . '__');
  995. if (!isset($info['base hook']) && !empty($pattern)) {
  996. // Transform _ in pattern to - to match file naming scheme
  997. // for the purposes of searching.
  998. $pattern = strtr($pattern, '_', '-');
  999. $matches = preg_grep('/^' . $pattern . '/', $patterns);
  1000. if ($matches) {
  1001. foreach ($matches as $match) {
  1002. $file = substr($match, 0, strpos($match, '.'));
  1003. // Put the underscores back in for the hook name and register this pattern.
  1004. $arg_name = isset($info['variables']) ? 'variables' : 'render element';
  1005. $implementations[strtr($file, '-', '_')] = array(
  1006. 'template' => $file,
  1007. 'path' => dirname($files[$match]->uri),
  1008. $arg_name => $info[$arg_name],
  1009. 'base hook' => $hook,
  1010. );
  1011. }
  1012. }
  1013. }
  1014. }
  1015. return $implementations;
  1016. }
  1017. /**
  1018. * Retrieve a setting for the current theme or for a given theme.
  1019. *
  1020. * The final setting is obtained from the last value found in the following
  1021. * sources:
  1022. * - the default global settings specified in this function
  1023. * - the default theme-specific settings defined in any base theme's .info file
  1024. * - the default theme-specific settings defined in the theme's .info file
  1025. * - the saved values from the global theme settings form
  1026. * - the saved values from the theme's settings form
  1027. * To only retrieve the default global theme setting, an empty string should be
  1028. * given for $theme.
  1029. *
  1030. * @param $setting_name
  1031. * The name of the setting to be retrieved.
  1032. * @param $theme
  1033. * The name of a given theme; defaults to the current theme.
  1034. *
  1035. * @return
  1036. * The value of the requested setting, NULL if the setting does not exist.
  1037. */
  1038. function theme_get_setting($setting_name, $theme = NULL) {
  1039. $cache = &drupal_static(__FUNCTION__, array());
  1040. // If no key is given, use the current theme if we can determine it.
  1041. if (!isset($theme)) {
  1042. $theme = !empty($GLOBALS['theme_key']) ? $GLOBALS['theme_key'] : '';
  1043. }
  1044. if (empty($cache[$theme])) {
  1045. // Set the default values for each global setting.
  1046. // To add new global settings, add their default values below, and then
  1047. // add form elements to system_theme_settings() in system.admin.inc.
  1048. $cache[$theme] = array(
  1049. 'default_logo' => 1,
  1050. 'logo_path' => '',
  1051. 'default_favicon' => 1,
  1052. 'favicon_path' => '',
  1053. // Use the IANA-registered MIME type for ICO files as default.
  1054. 'favicon_mimetype' => 'image/vnd.microsoft.icon',
  1055. );
  1056. // Turn on all default features.
  1057. $features = _system_default_theme_features();
  1058. foreach ($features as $feature) {
  1059. $cache[$theme]['toggle_' . $feature] = 1;
  1060. }
  1061. // Get the values for the theme-specific settings from the .info files of
  1062. // the theme and all its base themes.
  1063. if ($theme) {
  1064. $themes = list_themes();
  1065. $theme_object = $themes[$theme];
  1066. // Create a list which includes the current theme and all its base themes.
  1067. if (isset($theme_object->base_themes)) {
  1068. $theme_keys = array_keys($theme_object->base_themes);
  1069. $theme_keys[] = $theme;
  1070. }
  1071. else {
  1072. $theme_keys = array($theme);
  1073. }
  1074. foreach ($theme_keys as $theme_key) {
  1075. if (!empty($themes[$theme_key]->info['settings'])) {
  1076. $cache[$theme] = array_merge($cache[$theme], $themes[$theme_key]->info['settings']);
  1077. }
  1078. }
  1079. }
  1080. // Get the saved global settings from the database.
  1081. $cache[$theme] = array_merge($cache[$theme], variable_get('theme_settings', array()));
  1082. if ($theme) {
  1083. // Get the saved theme-specific settings from the database.
  1084. $cache[$theme] = array_merge($cache[$theme], variable_get('theme_' . $theme . '_settings', array()));
  1085. // If the theme does not support a particular feature, override the global
  1086. // setting and set the value to NULL.
  1087. if (!empty($theme_object->info['features'])) {
  1088. foreach ($features as $feature) {
  1089. if (!in_array($feature, $theme_object->info['features'])) {
  1090. $cache[$theme]['toggle_' . $feature] = NULL;
  1091. }
  1092. }
  1093. }
  1094. // Generate the path to the logo image.
  1095. if ($cache[$theme]['toggle_logo']) {
  1096. if ($cache[$theme]['default_logo']) {
  1097. $cache[$theme]['logo'] = file_create_url(dirname($theme_object->filename) . '/logo.png');
  1098. }
  1099. elseif ($cache[$theme]['logo_path']) {
  1100. $cache[$theme]['logo'] = file_create_url($cache[$theme]['logo_path']);
  1101. }
  1102. }
  1103. // Generate the path to the favicon.
  1104. if ($cache[$theme]['toggle_favicon']) {
  1105. if ($cache[$theme]['default_favicon']) {
  1106. if (file_exists($favicon = dirname($theme_object->filename) . '/favicon.ico')) {
  1107. $cache[$theme]['favicon'] = file_create_url($favicon);
  1108. }
  1109. else {
  1110. $cache[$theme]['favicon'] = file_create_url('misc/favicon.ico');
  1111. }
  1112. }
  1113. elseif ($cache[$theme]['favicon_path']) {
  1114. $cache[$theme]['favicon'] = file_create_url($cache[$theme]['favicon_path']);
  1115. }
  1116. else {
  1117. $cache[$theme]['toggle_favicon'] = FALSE;
  1118. }
  1119. }
  1120. }
  1121. }
  1122. return isset($cache[$theme][$setting_name]) ? $cache[$theme][$setting_name] : NULL;
  1123. }
  1124. /**
  1125. * Render a system default template, which is essentially a PHP template.
  1126. *
  1127. * @param $template_file
  1128. * The filename of the template to render.
  1129. * @param $variables
  1130. * A keyed array of variables that will appear in the output.
  1131. *
  1132. * @return
  1133. * The output generated by the template.
  1134. */
  1135. function theme_render_template($template_file, $variables) {
  1136. extract($variables, EXTR_SKIP); // Extract the variables to a local namespace
  1137. ob_start(); // Start output buffering
  1138. include DRUPAL_ROOT . '/' . $template_file; // Include the template file
  1139. return ob_get_clean(); // End buffering and return its contents
  1140. }
  1141. /**
  1142. * Enable a given list of themes.
  1143. *
  1144. * @param $theme_list
  1145. * An array of theme names.
  1146. */
  1147. function theme_enable($theme_list) {
  1148. drupal_clear_css_cache();
  1149. foreach ($theme_list as $key) {
  1150. db_update('system')
  1151. ->fields(array('status' => 1))
  1152. ->condition('type', 'theme')
  1153. ->condition('name', $key)
  1154. ->execute();
  1155. }
  1156. list_themes(TRUE);
  1157. menu_rebuild();
  1158. drupal_theme_rebuild();
  1159. // Notify locale module about new themes being enabled, so translations can
  1160. // be imported. This might start a batch, and only return to the redirect
  1161. // path after that.
  1162. module_invoke('locale', 'system_update', $theme_list);
  1163. // Invoke hook_themes_enabled after the themes have been enabled.
  1164. module_invoke_all('themes_enabled', $theme_list);
  1165. return;
  1166. }
  1167. /**
  1168. * Disable a given list of themes.
  1169. *
  1170. * @param $theme_list
  1171. * An array of theme names.
  1172. */
  1173. function theme_disable($theme_list) {
  1174. // Don't disable the default theme.
  1175. if ($pos = array_search(variable_get('theme_default', 'bartik'), $theme_list) !== FALSE) {
  1176. unset($theme_list[$pos]);
  1177. if (empty($theme_list)) {
  1178. return;
  1179. }
  1180. }
  1181. drupal_clear_css_cache();
  1182. foreach ($theme_list as $key) {
  1183. db_update('system')
  1184. ->fields(array('status' => 0))
  1185. ->condition('type', 'theme')
  1186. ->condition('name', $key)
  1187. ->execute();
  1188. }
  1189. list_themes(TRUE);
  1190. menu_rebuild();
  1191. drupal_theme_rebuild();
  1192. // Invoke hook_themes_enabled after the themes have been enabled.
  1193. module_invoke_all('themes_disabled', $theme_list);
  1194. return;
  1195. }
  1196. /**
  1197. * @ingroup themeable
  1198. * @{
  1199. */
  1200. /**
  1201. * Returns HTML for status and/or error messages, grouped by type.
  1202. *
  1203. * An invisible heading identifies the messages for assistive technology.
  1204. * Sighted users see a colored box. See http://www.w3.org/TR/WCAG-TECHS/H69.html
  1205. * for info.
  1206. *
  1207. * @param $variables
  1208. * An associative array containing:
  1209. * - display: (optional) Set to 'status' or 'error' to display only messages
  1210. * of that type.
  1211. */
  1212. function theme_status_messages($variables) {
  1213. $display = $variables['display'];
  1214. $output = '';
  1215. $status_heading = array(
  1216. 'status' => t('Status message'),
  1217. 'error' => t('Error message'),
  1218. 'warning' => t('Warning message'),
  1219. );
  1220. foreach (drupal_get_messages($display) as $type => $messages) {
  1221. $output .= "<div class=\"messages $type\">\n";

Large files files are truncated, but you can click here to view the full file