From a20904402b541be352c1b76778d3b46bb47a483d Mon Sep 17 00:00:00 2001 From: Michael Fairchild Date: Wed, 1 Jul 2015 16:25:32 -0500 Subject: [PATCH 1/2] Run all preprocess functions to make sure correct variables are passed to the template --- tims.module | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tims.module b/tims.module index 5057da0..d2af8a0 100644 --- a/tims.module +++ b/tims.module @@ -50,6 +50,23 @@ function tims_init() { $info['function'] = 'tims_render'; $info['theme path'] = drupal_get_path('module', 'tims') . '/theme'; $info['process functions'][] = 'tims_set_template'; + + foreach ($theme_registry_array as $key=>$item) { + if (!isset($item['pattern'])) { + //We only want items with patterns... maybe? (trying to be super conservative here so that we don't break anything) + //I'm also not sure how to get the closest match if there are unspecific patterns... + continue; + } + + if (strpos($hook, $item['pattern']) !== 0) { + //Make sure that the pattern matches (again, I haven't thought about specificity yet) + continue; + } + + //We should merge all of the regsitry items into our new one so that all + //normal preprocess functions are called. + $info = array_merge_recursive($item, $info); + } $theme_registry_object[$hook] = $theme_registry_array[$hook] = $info; } From 97e10604b088ddb697601238e866642915e7bd32 Mon Sep 17 00:00:00 2001 From: Michael Fairchild Date: Thu, 2 Jul 2015 11:46:15 -0500 Subject: [PATCH 2/2] Find the best match to merge Use the logic found in the drupal theme() function as a guideline here. This should handle specificity issues. --- tims.module | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/tims.module b/tims.module index d2af8a0..2f20f37 100644 --- a/tims.module +++ b/tims.module @@ -51,21 +51,33 @@ function tims_init() { $info['theme path'] = drupal_get_path('module', 'tims') . '/theme'; $info['process functions'][] = 'tims_set_template'; - foreach ($theme_registry_array as $key=>$item) { - if (!isset($item['pattern'])) { - //We only want items with patterns... maybe? (trying to be super conservative here so that we don't break anything) - //I'm also not sure how to get the closest match if there are unspecific patterns... - continue; - } - - if (strpos($hook, $item['pattern']) !== 0) { - //Make sure that the pattern matches (again, I haven't thought about specificity yet) - continue; + + // Use the drupal theme() logic for determining the closest match + // If there's no implementation, check for more generic fallbacks. + $match = $hook; + if (!isset($theme_registry_array[$match])) { + // Iteratively strip everything after the last '__' delimiter, until an + // implementation is found. + while ($pos = strrpos($match, '__')) { + $match = substr($match, 0, $pos); + if (isset($theme_registry_array[$match])) { + if (isset($theme_registry_array[$match]['function']) + && $theme_registry_array[$match]['function'] == 'tims_render') { + //Don't merge our own TIMS hooks + continue; + } + + //We found a match! + break; + } } + if (isset($theme_registry_array[$match])) { + //Only merge the hook if it exists - //We should merge all of the regsitry items into our new one so that all - //normal preprocess functions are called. - $info = array_merge_recursive($item, $info); + //We should merge all of the regsitry items into our new one so that all + //normal preprocess functions are called. + $info = array_merge_recursive($theme_registry_array[$match], $info); + } } $theme_registry_object[$hook] = $theme_registry_array[$hook] = $info;