Skip to content

Instantly share code, notes, and snippets.

@LionsAd
Last active December 16, 2015 18:38
Show Gist options
  • Select an option

  • Save LionsAd/5478771 to your computer and use it in GitHub Desktop.

Select an option

Save LionsAd/5478771 to your computer and use it in GitHub Desktop.
Make profiling Drupal's theme system fun!
diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index 47c99d9..30380f8 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -11,6 +11,7 @@
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Utility\ThemeRegistry;
+use Drupal\Component\PhpStorage\PhpStorageFactory;
/**
* @defgroup content_flags Content markers
@@ -961,6 +962,23 @@ function drupal_find_base_themes($themes, $key, $used_keys = array()) {
* @see template_process()
*/
function theme($hook, $variables = array()) {
+ $real_hook = $hook;
+ if (is_array($hook)) {
+ $real_hook = current($hook);
+ }
+ return profiler_debug_call_function('theme2_core', $real_hook, $variables);
+}
+
+function theme2_core($hook, $variables = array()) {
+ return theme2($hook, $variables);
+}
+
+function theme2_twig($hook, $variables = array()) {
+ return theme2($hook, $variables);
+}
+
+
+function theme2($hook, $variables = array()) {
// If called before all modules are loaded, we do not necessarily have a full
// theme registry to work with, and therefore cannot process the theme
// request properly. See also _theme_load_registry().
@@ -1177,6 +1195,33 @@ function theme($hook, $variables = array()) {
return $output;
}
+function profiler_debug_call_function($function, $hook, $variables = array()) {
+ $functions = &drupal_static(__FUNCTION__);
+
+ $func = 'profiler_debug_' . $hook;
+ if (!isset($functions)) {
+ $functions = array();
+ }
+ if (!isset($functions[$func])) {
+ $storage = PhpStorageFactory::get('profiler_debug');
+
+ if (!$storage->load($func)) {
+ $str =<<<EOF
+<?php
+
+ function $func(\$hook, \$variables) {
+ return $function(\$hook, \$variables);
+ }
+
+EOF;
+ $storage->save($func, $str);
+ $storage->load($func);
+ }
+ $functions[$func] = TRUE;
+ }
+ return $func($hook, $variables);
+}
+
/**
* Returns the path to the current themed element.
*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment