devel_themer.module 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. <?php
  2. /**
  3. * The name of the attribute used to store the thmr id.
  4. */
  5. define ('DEVEL_THEMER_ATTRIBUTE', 'data-thmr');
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function devel_themer_menu() {
  10. $items = array();
  11. $items['admin/config/development/devel_themer'] = array(
  12. 'title' => 'Devel Themer',
  13. 'description' => 'Display or hide the textual template log',
  14. 'page callback' => 'drupal_get_form',
  15. 'page arguments' => array('devel_themer_admin_settings'),
  16. 'access arguments' => array('administer site configuration'),
  17. 'type' => MENU_NORMAL_ITEM,
  18. );
  19. $items['devel_themer/variables/%/%'] = array(
  20. 'title' => 'Theme Development AJAX variables',
  21. 'page callback' => 'devel_themer_ajax_variables',
  22. 'page arguments' => array(2, 3),
  23. 'delivery callback' => 'ajax_deliver',
  24. 'access arguments' => array('access devel information'),
  25. 'type' => MENU_CALLBACK,
  26. );
  27. return $items;
  28. }
  29. /**
  30. * A menu callback used by popup to retrieve variables from cache for a recent page.
  31. *
  32. * @param $request_id
  33. * A unique key that is sent to the browser in Drupal.Settings.devel_themer_request_id
  34. * @param $call
  35. * The theme call for which you wish to retrieve variables.
  36. * @return string
  37. * A chunk of HTML with the devel_print_object() rendering of the variables.
  38. */
  39. function devel_themer_ajax_variables($request_id, $call) {
  40. $file = "temporary://devel_themer_$request_id";
  41. if ($data = unserialize(file_get_contents($file))) {
  42. $variables = $data[$call]['variables'];
  43. if (has_krumo()) {
  44. $content = krumo_ob($variables);
  45. }
  46. elseif ($data[$call]['type'] == 'func') {
  47. $content = $variables;
  48. }
  49. else {
  50. $content = $variables;
  51. }
  52. }
  53. else {
  54. $content = 'Ajax variables file not found. -' . check_plain($file);
  55. }
  56. $commands[] = ajax_command_replace('div.themer-variables', '<div class="themer-variables">' . $content . '</div>');
  57. return array('#type' => 'ajax', '#commands' => $commands);
  58. }
  59. function devel_themer_admin_settings() {
  60. $form['devel_themer_log'] = array('#type' => 'checkbox',
  61. '#title' => t('Display theme log'),
  62. '#default_value' => variable_get('devel_themer_log', FALSE),
  63. '#description' => t('Display the list of theme templates and theme functions which could have been be used for a given page. The one that was actually used is bolded. This is the same data as the represented in the popup, but all calls are listed in chronological order and can alternately be sorted by time.'),
  64. );
  65. return system_settings_form($form);
  66. }
  67. function devel_themer_init() {
  68. if (user_access('access devel information')) {
  69. // Add requisite libraries.
  70. drupal_add_library('system', 'jquery.form');
  71. drupal_add_library('system', 'drupal.ajax');
  72. drupal_add_library('system', 'ui.draggable');
  73. drupal_add_library('system', 'ui.resizable');
  74. // Add this module's JS and CSS.
  75. $path = drupal_get_path('module', 'devel_themer');
  76. drupal_add_js($path . '/devel_themer.js');
  77. drupal_add_css($path . '/devel_themer.css');
  78. drupal_add_css($path . '/devel_themer_ie_fix.css', array(
  79. 'browsers' => array('IE' => 'lt IE 7', '!IE' => FALSE),
  80. 'media' => 'screen',
  81. 'weight' => 20,
  82. 'preprocess' => FALSE,
  83. ));
  84. // Add krumo JS and CSS. We can't rely on krumo automatically doing this,
  85. // because we add our HTML dynamically after initial page load.
  86. if (has_krumo()) {
  87. $path_to_devel = drupal_get_path('module', 'devel');
  88. // Krumo files don't work correctly when aggregated.
  89. drupal_add_js($path_to_devel . '/krumo/krumo.js', array('preprocess' => FALSE));
  90. drupal_add_css($path_to_devel . '/krumo/skins/default/skin.css', array('preprocess' => FALSE));
  91. }
  92. devel_themer_popup();
  93. if (!devel_silent() && variable_get('devel_themer_log', FALSE)) {
  94. register_shutdown_function('devel_themer_shutdown');
  95. }
  96. }
  97. }
  98. function devel_themer_shutdown() {
  99. print devel_themer_log();
  100. }
  101. /**
  102. * Show all theme templates and functions that could have been used on this page.
  103. */
  104. function devel_themer_log() {
  105. if (isset($GLOBALS['devel_theme_calls'])) {
  106. foreach ($GLOBALS['devel_theme_calls'] as $counter => $call) {
  107. // Sometimes $call is a string. Not sure why.
  108. if (is_array($call)) {
  109. $id = "devel_theme_log_link_$counter";
  110. $marker = "<div id=\"$id\" class=\"devel_theme_log_link\"></div>\n";
  111. $used = $call['used'];
  112. if ($call['type'] == 'func') {
  113. $name = $call['name'] . '()';
  114. foreach ($call['candidates'] as $item) {
  115. if ($item == $used) {
  116. $items[] = "<strong>$used</strong>";
  117. }
  118. else {
  119. $items[] = $item;
  120. }
  121. }
  122. }
  123. else {
  124. $name = $call['name'];
  125. foreach ($call['candidates'] as $item) {
  126. if ($item == basename($used)) {
  127. $items[] = "<strong>$used</strong>";
  128. }
  129. else {
  130. $items[] = $item;
  131. }
  132. }
  133. }
  134. $rows[] = array($call['duration'], $marker . $name, implode(', ', $items));
  135. unset($items);
  136. }
  137. }
  138. $header = array('Duration (ms)', 'Template/Function', "Candidate template files or function names");
  139. $output = theme('table', $header, $rows);
  140. return $output;
  141. }
  142. }
  143. /**
  144. * Implements hook_theme_registry_alter().
  145. *
  146. * Route all theme hooks to devel_themer_catch_function().
  147. */
  148. function devel_themer_theme_registry_alter(&$theme_registry) {
  149. foreach ($theme_registry as $hook => $data) {
  150. if (isset($theme_registry[$hook]['function'])) {
  151. // If the hook is a function, store it so it can be run after it has been intercepted.
  152. // This does not apply to template calls.
  153. $theme_registry[$hook]['devel_function_intercept'] = $theme_registry[$hook]['function'];
  154. }
  155. // Add our catch function to intercept functions as well as templates.
  156. $theme_registry[$hook]['function'] = 'devel_themer_catch_function';
  157. // Remove the process and preprocess functions so they are
  158. // only called by devel_themer_theme_twin().
  159. $theme_registry[$hook]['devel_function_preprocess_intercept'] = isset($theme_registry[$hook]['preprocess functions']) ? $theme_registry[$hook]['preprocess functions'] : array();
  160. $theme_registry[$hook]['devel_function_process_intercept'] = isset($theme_registry[$hook]['process functions']) ? $theme_registry[$hook]['process functions'] : array();
  161. $theme_registry[$hook]['preprocess functions'] = array();
  162. $theme_registry[$hook]['process functions'] = array();
  163. }
  164. }
  165. /**
  166. * Implements hook_module_implements_alter().
  167. *
  168. * Ensure devel_themer_theme_registry_alter() runs as late as possible.
  169. */
  170. function devel_themer_module_implements_alter(&$implementations, $hook) {
  171. if ($hook == 'theme_registry_alter') {
  172. // Unsetting and resetting moves the item to the end of the array.
  173. $group = $implementations['devel_themer'];
  174. unset($implementations['devel_themer']);
  175. $implementations['devel_themer'] = $group;
  176. }
  177. }
  178. /**
  179. * Injects markers into the html returned by theme functions/templates.
  180. *
  181. * Uses simplehtmldom to add a thmr attribute to toplevel html elements.
  182. * A toplevel text element will be wrapped in a span.
  183. *
  184. * @param string $html
  185. * @param string $marker
  186. */
  187. function devel_themer_inject_markers($html, $marker) {
  188. if (!module_exists('simplehtmldom')) {
  189. drupal_set_message(t('<a href="http://drupal.org/project/simplehtmldom">Simplehtmldom</a> module is missing and required by Theme developer.'), 'error', FALSE);
  190. return $html;
  191. }
  192. $html_dom = new simple_html_dom();
  193. $html_dom->load($html);
  194. foreach ($html_dom->root->nodes as $element) {
  195. if ($element->nodetype == HDOM_TYPE_TEXT) {
  196. if (trim($element->innertext) !== '') {
  197. $element->innertext = "<span " . DEVEL_THEMER_ATTRIBUTE . "='$marker' class='devel-themer-wrapper'>{$element->innertext}</span>";
  198. }
  199. }
  200. elseif ($element->hasAttribute(DEVEL_THEMER_ATTRIBUTE)) {
  201. $element->setAttribute(DEVEL_THEMER_ATTRIBUTE, "$marker " . $element->getAttribute(DEVEL_THEMER_ATTRIBUTE));
  202. }
  203. else {
  204. $element->setAttribute(DEVEL_THEMER_ATTRIBUTE, $marker);
  205. }
  206. }
  207. $html = (string)$html_dom;
  208. // Release memory.
  209. $html_dom->clear();
  210. unset($html_dom);
  211. return $html;
  212. }
  213. /**
  214. * Intercepts all theme calls (including templates), adds to template log, and dispatches to original theme function.
  215. */
  216. function devel_themer_catch_function() {
  217. $trace = debug_backtrace(FALSE);
  218. $hook = $trace[1]['args'][0];
  219. if (sizeof($trace[1]['args']) > 1) {
  220. $variables = $trace[1]['args'][1];
  221. if (!is_array($variables)) {
  222. watchdog('devel_themer', 'Variables should be passed as an associative array to the theme hook !hook.', array('!hook' => is_array($hook) ? implode(', ', $hook) : $hook), WATCHDOG_ERROR);
  223. }
  224. }
  225. else {
  226. $variables = array();
  227. }
  228. $counter = devel_counter();
  229. $variables['thmr_key'] = "thmr_" . $counter;
  230. $timer_name = "thmr_$counter";
  231. timer_start($timer_name);
  232. // The twin of theme(). All rendering done through here.
  233. list($return, $meta) = devel_themer_theme_twin($hook, $variables);
  234. $time = timer_stop($timer_name);
  235. if (!empty($return) && !is_array($return) && !is_object($return) && user_access('access devel information')) {
  236. $key = "thmr_$counter";
  237. // Check for themer attribute in content returned. Apply word boundaries so
  238. // that 'thmr_10' doesn't match 'thmr_1'.
  239. if (!preg_match("/\\b$key\\b/", $return)) {
  240. // Exclude wrapping a SPAN around content returned by theme functions
  241. // whose result is not intended for HTML usage.
  242. $exclude = array('options_none');
  243. // theme_html_tag() is a low-level theme function intended primarily for
  244. // markup added to the document HEAD.
  245. $exclude[] = 'html_tag';
  246. // DATE MODULE: Inline labels for date select lists shouldn't be wrapped.
  247. if (strpos($meta['hook'], 'date_part_label_') === 0 && $variables['element']['#type'] == 'date_select' && $variables['element']['#date_label_position'] == 'within') {
  248. $exclude[] = $hook;
  249. }
  250. if (!in_array($hook, $exclude)) {
  251. $return = devel_themer_inject_markers($return, $key);
  252. }
  253. }
  254. if ($meta['type'] == 'func') {
  255. $name = $meta['hook'];
  256. $used = $meta['used'];
  257. global $theme;
  258. $candidates = array();
  259. foreach (array_reverse($meta['suggestions']) as $suggestion) {
  260. $candidates[] = "{$theme}_{$suggestion}";
  261. }
  262. $candidates[] = "{$theme}_{$name}";
  263. if (empty($meta['variables'])) {
  264. $variables = array();
  265. }
  266. }
  267. else {
  268. $name = strtr($meta['hook'], '_', '-') . $meta['extension'];
  269. $candidates = array();
  270. foreach (array_reverse($meta['suggestions']) as $suggestion) {
  271. $candidates[] = strtr($suggestion, '_', '-') . $meta['extension'];
  272. }
  273. $candidates[] = $name;
  274. $used = $meta['used'];
  275. }
  276. // Set preprocessors.
  277. if (isset($meta['processor_functions']['preprocess functions'])) {
  278. $preprocessors = $meta['processor_functions']['preprocess functions'];
  279. }
  280. else {
  281. $preprocessors = array();
  282. }
  283. // Set processors.
  284. if (isset($meta['processor_functions']['process functions'])) {
  285. $processors = $meta['processor_functions']['process functions'];
  286. }
  287. else {
  288. $processors = array();
  289. }
  290. // This variable gets sent to the browser in Drupal.settings.
  291. $GLOBALS['devel_theme_calls'][$key] = array(
  292. 'id' => $key,
  293. 'name' => $name,
  294. 'used' => $used,
  295. 'type' => $meta['type'],
  296. 'duration' => $time['time'],
  297. 'candidates' => $candidates,
  298. 'preprocessors' => $preprocessors,
  299. 'processors' => $processors,
  300. );
  301. // Remove some things from variables to reduce size of item in crumo
  302. unset($meta['variables']['page']);
  303. // This variable gets serialized and cached on the server.
  304. $GLOBALS['devel_themer_server'][$key] = array(
  305. 'variables' => $meta['variables'],
  306. 'type' => $meta['type'],
  307. );
  308. }
  309. return $return;
  310. }
  311. /**
  312. * Nearly clones the Drupal API theme() function, but returns meta information in addition to the rendered output.
  313. *
  314. * Usage:
  315. * @code
  316. * list($output, $meta) = devel_themer_theme_twin($hook, $variables);
  317. * @endcode
  318. *
  319. * @return
  320. * An array with two items:
  321. * - $output: The rendered HTML.
  322. * - $meta: An associative array with the following keys:
  323. * - 'wildcards': Candidate hooks which have been looked at but don't have
  324. * an implementation.
  325. * - 'hook': The first found hook with an implementation.
  326. * - 'path': The path to the theme that implements $meta['hook'].
  327. * @todo Document remaining keys.
  328. */
  329. function devel_themer_theme_twin($hook, $variables) {
  330. static $hooks = NULL;
  331. $thmr_key = $variables['thmr_key'];
  332. unset($variables['thmr_key']);
  333. $meta = array();
  334. $meta['name'] = $hook;
  335. // If called before all modules are loaded, we do not necessarily have a full
  336. // theme registry to work with, and therefore cannot process the theme
  337. // request properly. See also _theme_load_registry().
  338. if (!module_load_all(NULL) && !defined('MAINTENANCE_MODE')) {
  339. throw new Exception(t('theme() may not be called until all modules are loaded.'));
  340. }
  341. if (!isset($hooks)) {
  342. drupal_theme_initialize();
  343. $hooks = theme_get_registry();
  344. }
  345. // If an array of hook candidates were passed, use the first one that has an
  346. // implementation.
  347. if (is_array($hook)) {
  348. foreach ($hook as $candidate) {
  349. if (isset($hooks[$candidate])) {
  350. break;
  351. }
  352. $meta['wildcards'][] = $candidate;
  353. }
  354. $hook = $candidate;
  355. }
  356. // If there's no implementation, check for more generic fallbacks. If there's
  357. // still no implementation, log an error and return an empty string.
  358. if (!isset($hooks[$hook])) {
  359. // Iteratively strip everything after the last '__' delimiter, until an
  360. // implementation is found.
  361. while ($pos = strrpos($hook, '__')) {
  362. $hook = substr($hook, 0, $pos);
  363. if (isset($hooks[$hook])) {
  364. break;
  365. }
  366. $meta['wildcards'][] = $hook;
  367. }
  368. if (!isset($hooks[$hook])) {
  369. // Only log a message when not trying theme suggestions ($hook being an
  370. // array).
  371. if (!isset($candidate)) {
  372. watchdog('theme', 'Theme key "@key" not found.', array('@key' => $hook), WATCHDOG_WARNING);
  373. }
  374. return array('', $meta);
  375. }
  376. }
  377. $meta['hook'] = $hook;
  378. $info = $hooks[$hook];
  379. $meta['path'] = isset($info['theme path']) ? $info['theme path'] : NULL;
  380. global $theme_path;
  381. $temp = $theme_path;
  382. // point path_to_theme() to the currently used theme path:
  383. $theme_path = isset($info['theme path']) ? $info['theme path'] : NULL;
  384. // Include a file if the theme function or variable processor is held elsewhere.
  385. if (!empty($info['includes'])) {
  386. foreach ($info['includes'] as $include_file) {
  387. include_once DRUPAL_ROOT . '/' . $include_file;
  388. }
  389. }
  390. // If a renderable array is passed as $variables, then set $variables to
  391. // the arguments expected by the theme function.
  392. if (isset($variables['#theme']) || isset($variables['#theme_wrappers'])) {
  393. $element = $variables;
  394. $variables = array();
  395. if (isset($info['variables'])) {
  396. foreach (array_keys($info['variables']) as $name) {
  397. if (isset($element["#$name"])) {
  398. $variables[$name] = $element["#$name"];
  399. }
  400. }
  401. }
  402. else {
  403. $variables[$info['render element']] = $element;
  404. }
  405. }
  406. // Merge in argument defaults.
  407. if (!empty($info['variables'])) {
  408. $variables += $info['variables'];
  409. }
  410. elseif (!empty($info['render element'])) {
  411. $variables += array($info['render element'] => array());
  412. }
  413. // Add the themer attribute here
  414. $variables['attributes_array'][DEVEL_THEMER_ATTRIBUTE] = $thmr_key;
  415. // Invoke the variable processors, if any. The processors may specify
  416. // alternate suggestions for which hook's template/function to use. If the
  417. // hook is a suggestion of a base hook, invoke the variable processors of
  418. // the base hook, but retain the suggestion as a high priority suggestion to
  419. // be used unless overridden by a variable processor function.
  420. if (isset($info['base hook'])) {
  421. $base_hook = $info['base hook'];
  422. $base_hook_info = $hooks[$base_hook];
  423. // Return the preprocessor functions to their original state
  424. $base_hook_info['preprocess functions'] = $base_hook_info['devel_function_preprocess_intercept'];
  425. $base_hook_info['process functions'] = $base_hook_info['devel_function_process_intercept'];
  426. if (isset($base_hook_info['preprocess functions']) || isset($base_hook_info['process functions'])) {
  427. $variables['theme_hook_suggestion'] = $hook;
  428. $hook = $base_hook;
  429. $info = $base_hook_info;
  430. }
  431. }
  432. $info['preprocess functions'] = $info['devel_function_preprocess_intercept'];
  433. $info['process functions'] = $info['devel_function_process_intercept'];
  434. if (isset($info['preprocess functions']) || isset($info['process functions'])) {
  435. $variables['theme_hook_suggestions'] = array();
  436. foreach (array('preprocess functions', 'process functions') as $phase) {
  437. if (!empty($info[$phase])) {
  438. foreach ($info[$phase] as $processor_function) {
  439. $meta['processor_functions'][$phase][] = $processor_function;
  440. if (function_exists($processor_function)) {
  441. // We don't want a poorly behaved process function changing $hook.
  442. $hook_clone = $hook;
  443. $processor_function($variables, $hook_clone);
  444. }
  445. }
  446. }
  447. }
  448. // If the preprocess/process functions specified hook suggestions, and the
  449. // suggestion exists in the theme registry, use it instead of the hook that
  450. // theme() was called with. This allows the preprocess/process step to
  451. // route to a more specific theme hook. For example, a function may call
  452. // theme('node', ...), but a preprocess function can add 'node__article' as
  453. // a suggestion, enabling a theme to have an alternate template file for
  454. // article nodes. Suggestions are checked in the following order:
  455. // - The 'theme_hook_suggestion' variable is checked first. It overrides
  456. // all others.
  457. // - The 'theme_hook_suggestions' variable is checked in FILO order, so the
  458. // last suggestion added to the array takes precedence over suggestions
  459. // added earlier.
  460. $suggestions = array();
  461. if (!empty($variables['theme_hook_suggestions'])) {
  462. $suggestions = $variables['theme_hook_suggestions'];
  463. }
  464. if (!empty($variables['theme_hook_suggestion'])) {
  465. $suggestions[] = $variables['theme_hook_suggestion'];
  466. }
  467. foreach (array_reverse($suggestions) as $suggestion) {
  468. if (isset($hooks[$suggestion])) {
  469. $info = $hooks[$suggestion];
  470. break;
  471. }
  472. }
  473. }
  474. // Tidy up the theme things here.
  475. if (isset($info)) {
  476. unset($info['function']);
  477. if (isset($info['devel_function_intercept'])) {
  478. $info['function'] = $info['devel_function_intercept'];
  479. }
  480. $meta['hook'] = $hook;
  481. $meta['path'] = isset($info['theme path']) ? $info['theme path'] : NULL;
  482. }
  483. // Generate the output using either a function or a template.
  484. $output = '';
  485. if (isset($info['function'])) {
  486. if (function_exists($info['function'])) {
  487. $output = $info['function']($variables);
  488. $meta['type'] = 'func';
  489. $meta['used'] = $info['function'];
  490. $meta['suggestions'] = $suggestions;
  491. }
  492. }
  493. else {
  494. // Default render function and extension.
  495. $render_function = 'theme_render_template';
  496. $extension = '.tpl.php';
  497. // The theme engine may use a different extension and a different renderer.
  498. global $theme_engine;
  499. if (isset($theme_engine)) {
  500. if ($info['type'] != 'module') {
  501. if (function_exists($theme_engine . '_render_template')) {
  502. $render_function = $theme_engine . '_render_template';
  503. }
  504. $extension_function = $theme_engine . '_extension';
  505. if (function_exists($extension_function)) {
  506. $extension = $extension_function();
  507. }
  508. }
  509. }
  510. // In some cases, a template implementation may not have had
  511. // template_preprocess() run (for example, if the default implementation is
  512. // a function, but a template overrides that default implementation). In
  513. // these cases, a template should still be able to expect to have access to
  514. // the variables provided by template_preprocess(), so we add them here if
  515. // they don't already exist. We don't want to run template_preprocess()
  516. // twice (it would be inefficient and mess up zebra striping), so we use the
  517. // 'directory' variable to determine if it has already run, which while not
  518. // completely intuitive, is reasonably safe, and allows us to save on the
  519. // overhead of adding some new variable to track that.
  520. if (!isset($variables['directory'])) {
  521. $default_template_variables = array();
  522. template_preprocess($default_template_variables, $hook);
  523. $variables += $default_template_variables;
  524. }
  525. // Render the output using the template file.
  526. $template_file = $info['template'] . $extension;
  527. if (isset($info['path'])) {
  528. $template_file = $info['path'] . '/' . $template_file;
  529. }
  530. $meta['type'] = 'tpl';
  531. $meta['used'] = $template_file;
  532. $meta['extension'] = $extension;
  533. $meta['suggestions'] = $suggestions;
  534. $meta['template_file'] = $template_file;
  535. $meta['preprocessors'] = isset($info['preprocess functions']) ? $info['preprocess functions'] : array();
  536. $output = $render_function($template_file, $variables);
  537. }
  538. $meta['variables'] = $variables;
  539. // restore path_to_theme()
  540. $theme_path = $temp;
  541. return array($output, $meta);
  542. }
  543. // We save the huge js array here instead of hook_footer so we can catch theme('page')
  544. function devel_themer_exit() {
  545. // TODO: limit to html pages only $router_item = menu_get_item();
  546. //delivery_callback
  547. if (!empty($GLOBALS['devel_theme_calls']) && $_SERVER['REQUEST_METHOD'] != 'POST') {
  548. // A random string that is sent to the browser. It enables the popup to retrieve params/variables from this request.
  549. $request_id = uniqid(rand());
  550. // Write the variables information to the a file. It will be retrieved on demand via AJAX.
  551. // We used to write this to DB but was getting 'Warning: Got a packet bigger than 'max_allowed_packet' bytes'
  552. // Writing to temp dir means we don't worry about folder existence/perms and cleanup is free.
  553. try {
  554. $file = file_save_data(serialize($GLOBALS['devel_themer_server']), "temporary://devel_themer_$request_id", FILE_EXISTS_REPLACE);
  555. }
  556. catch (Exception $e) {
  557. $file = file_save_data(serialize(array("unables to save variables, probably due to pdo")), "temporary://devel_themer_$request_id", FILE_EXISTS_REPLACE);
  558. }
  559. // file_save_data() saves the file with permanent status. Change this to
  560. // temporary so that system_cron() garbage collects them.
  561. $file->status = 0;
  562. file_save($file);
  563. $GLOBALS['devel_theme_calls']['request_id'] = $request_id;
  564. $GLOBALS['devel_theme_calls']['devel_themer_uri'] = url("devel_themer/variables/$request_id");
  565. print '<script type="text/javascript">jQuery.extend(Drupal.settings, ' . drupal_json_encode($GLOBALS['devel_theme_calls']) . ");</script>\n";
  566. }
  567. }
  568. // just hand out next counter, or return current value
  569. function devel_counter($increment = TRUE) {
  570. static $counter = 0;
  571. if ($increment) {
  572. $counter++;
  573. }
  574. return $counter;
  575. }
  576. /**
  577. * Return the popup template
  578. * placed here for easy editing
  579. */
  580. function devel_themer_popup() {
  581. $majorver = substr(VERSION, 0, strpos(VERSION, '.'));
  582. // add translatable strings
  583. drupal_add_js(array('thmrStrings' =>
  584. array(
  585. 'themer_info' => t('Themer info'),
  586. 'toggle_throbber' => ' <img src="' . base_path() . drupal_get_path('module', 'devel_themer') . '/loader-little.gif' . '" alt="' . t('loading') . '" class="throbber" width="16" height="16" style="display:none" />',
  587. 'parents' => t('Parents:') . ' ',
  588. 'function_called' => t('Function called:') . ' ',
  589. 'template_called' => t('Template called:') . ' ',
  590. 'candidate_files' => t('Candidate template files:') . ' ',
  591. 'preprocessors' => t('Preprocess functions:') . ' ',
  592. 'processors' => t('Process functions:') . ' ',
  593. 'candidate_functions' => t('Candidate function names:') . ' ',
  594. 'drupal_api_docs' => t('link to Drupal API documentation'),
  595. 'source_link_title' => t('link to source code'),
  596. 'function_arguments' => t('Function Arguments'),
  597. 'template_variables' => t('Template Variables'),
  598. 'file_used' => t('File used:') . ' ',
  599. 'duration' => t('Duration:') . ' ',
  600. 'api_site' => variable_get('devel_api_site', 'http://api.drupal.org/'),
  601. 'drupal_version' => $majorver,
  602. 'source_link' => url('devel/source', array('query' => array('file' => ''))),
  603. ))
  604. , 'setting');
  605. $title = t('Drupal Themer Information');
  606. $intro = t('Click on any element to see information about the Drupal theme function or template that created it.');
  607. $popup = <<<EOT
  608. <div id="themer-fixeder">
  609. <div id="themer-relativer">
  610. <div id="themer-popup">
  611. <div class="topper">
  612. <span class="close">X</span> $title
  613. </div>
  614. <div id="parents" class="row">
  615. </div>
  616. <div class="info row">
  617. <div class="starter">$intro</div>
  618. <dl>
  619. <dt class="key-type">
  620. </dt>
  621. <dd class="key">
  622. </dd>
  623. <div class="used">
  624. </div>
  625. <dt class="candidates-type">
  626. </dt>
  627. <dd class="candidates">
  628. </dd>
  629. <dt class="preprocessors-type">
  630. </dt>
  631. <dd class="preprocessors">
  632. </dd>
  633. <dt class="processors-type">
  634. </dt>
  635. <dd class="processors">
  636. </dd>
  637. <div class="duration"></div>
  638. </dl>
  639. </div><!-- /info -->
  640. <div class="attributes row">
  641. <div class="themer-variables"></div>
  642. </div><!-- /attributes -->
  643. </div><!-- /themer-popup -->
  644. </div>
  645. </div>
  646. EOT;
  647. drupal_add_js(array('thmr_popup' => $popup), 'setting');
  648. }