devel_themer.module 24 KB

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