From 4443a3070b80971e636c7ef5ba0fbb5ebfc8d024 Mon Sep 17 00:00:00 2001 From: David Rothstein Date: Sat, 1 Nov 2014 18:22:45 -0400 Subject: Issue #2307505 by Cottser, Fabianx: Port twig_debug output to Drupal 7. --- includes/theme.inc | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) (limited to 'includes/theme.inc') diff --git a/includes/theme.inc b/includes/theme.inc index ebe5817b2..d2a142b64 100644 --- a/includes/theme.inc +++ b/includes/theme.inc @@ -1029,6 +1029,7 @@ function theme($hook, $variables = array()) { } $hook = $candidate; } + $theme_hook_original = $hook; // If there's no implementation, check for more generic fallbacks. If there's // still no implementation, log an error and return an empty string. @@ -1090,6 +1091,8 @@ function theme($hook, $variables = array()) { $variables += array($info['render element'] => array()); } + $variables['theme_hook_original'] = $theme_hook_original; + // Invoke the variable processors, if any. The processors may specify // alternate suggestions for which hook's template/function to use. If the // hook is a suggestion of a base hook, invoke the variable processors of @@ -1198,7 +1201,12 @@ function theme($hook, $variables = array()) { if (isset($info['path'])) { $template_file = $info['path'] . '/' . $template_file; } - $output = $render_function($template_file, $variables); + if (variable_get('theme_debug', FALSE)) { + $output = _theme_render_template_debug($render_function, $template_file, $variables, $extension); + } + else { + $output = $render_function($template_file, $variables); + } } // restore path_to_theme() @@ -1520,6 +1528,72 @@ function theme_render_template($template_file, $variables) { return ob_get_clean(); } +/** + * Renders a template for any engine. + * + * Includes the possibility to get debug output by setting the + * theme_debug variable to TRUE. + * + * @param string $template_function + * The function to call for rendering the template. + * @param string $template_file + * The filename of the template to render. + * @param array $variables + * A keyed array of variables that will appear in the output. + * @param string $extension + * The extension used by the theme engine for template files. + * + * @return string + * The output generated by the template including debug information. + */ +function _theme_render_template_debug($template_function, $template_file, $variables, $extension) { + $output = array( + 'debug_prefix' => '', + 'debug_info' => '', + 'rendered_markup' => call_user_func($template_function, $template_file, $variables), + 'debug_suffix' => '', + ); + $output['debug_prefix'] .= "\n\n"; + $output['debug_prefix'] .= "\n"; + // If there are theme suggestions, reverse the array so more specific + // suggestions are shown first. + if (!empty($variables['theme_hook_suggestions'])) { + $variables['theme_hook_suggestions'] = array_reverse($variables['theme_hook_suggestions']); + } + // Add debug output for directly called suggestions like + // '#theme' => 'comment__node__article'. + if (strpos($variables['theme_hook_original'], '__') !== FALSE) { + $derived_suggestions[] = $hook = $variables['theme_hook_original']; + while ($pos = strrpos($hook, '__')) { + $hook = substr($hook, 0, $pos); + $derived_suggestions[] = $hook; + } + // Get the value of the base hook (last derived suggestion) and append it + // to the end of all theme suggestions. + $base_hook = array_pop($derived_suggestions); + $variables['theme_hook_suggestions'] = array_merge($derived_suggestions, $variables['theme_hook_suggestions']); + $variables['theme_hook_suggestions'][] = $base_hook; + } + if (!empty($variables['theme_hook_suggestions'])) { + $current_template = basename($template_file); + $suggestions = $variables['theme_hook_suggestions']; + // Only add the original theme hook if it wasn't a directly called + // suggestion. + if (strpos($variables['theme_hook_original'], '__') === FALSE) { + $suggestions[] = $variables['theme_hook_original']; + } + foreach ($suggestions as &$suggestion) { + $template = strtr($suggestion, '_', '-') . $extension; + $prefix = ($template == $current_template) ? 'x' : '*'; + $suggestion = $prefix . ' ' . $template; + } + $output['debug_info'] .= "\n"; + } + $output['debug_info'] .= "\n\n"; + $output['debug_suffix'] .= "\n\n\n"; + return implode('', $output); +} + /** * Enables a given list of themes. * -- cgit v1.2.3