Index: includes/theme.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/theme.inc,v
retrieving revision 1.371
diff -u -p -r1.371 theme.inc
--- includes/theme.inc	26 Aug 2007 07:46:10 -0000	1.371
+++ includes/theme.inc	26 Aug 2007 23:18:19 -0000
@@ -256,6 +256,14 @@ function _theme_process_registry(&$cache
       if (!isset($info['arguments']) && isset($cache[$hook])) {
         $result[$hook]['arguments'] = $cache[$hook]['arguments'];
       }
+      // Likewise with theme paths. These are used for template naming suggestions.
+      // Theme implementations can occur in multiple paths. Suggestions should follow.
+      if (!isset($info['theme paths']) && isset($cache[$hook])) {
+        $result[$hook]['theme paths'] = $cache[$hook]['theme paths'];
+      }
+      // Check for sub-directories.
+      $result[$hook]['theme paths'][] = isset($info['path']) ? $info['path'] : $path;
+
       // Check for default _preprocess_ functions. Ensure arrayness.
       if (!isset($info['preprocess functions']) || !is_array($info['preprocess functions'])) {
         $info['preprocess functions'] = array();
@@ -572,7 +580,7 @@ function theme() {
     }
 
     if ($suggestions) {
-      $template_file = drupal_discover_template($suggestions, $extension);
+      $template_file = drupal_discover_template($info['theme paths'], $suggestions, $extension);
     }
 
     if (empty($template_file)) {
@@ -589,17 +597,23 @@ function theme() {
 }
 
 /**
- * Choose which template file to actually render; these are all
- * suggested templates from the theme.
+ * Choose which template file to actually render. These are all suggested
+ * templates from themes and modules. Theming implementations can occur on
+ * multiple levels. All paths are checked to account for this.
  */
-function drupal_discover_template($suggestions, $extension = '.tpl.php') {
+function drupal_discover_template($paths, $suggestions, $extension = '.tpl.php') {
   global $theme_engine;
 
-  // Loop through any suggestions in FIFO order.
+  // Loop through all paths and suggestions in FIFO order.
   $suggestions = array_reverse($suggestions);
+  $paths = array_reverse($paths);
   foreach ($suggestions as $suggestion) {
-    if (!empty($suggestion) && file_exists($file = path_to_theme() .'/'. $suggestion . $extension)) {
-      return $file;
+    if (!empty($suggestion)) {
+      foreach ($paths as $path) {
+        if (file_exists($file = $path .'/'. $suggestion . $extension)) {
+          return $file;
+        }
+      }
     }
   }
 }
