summaryrefslogtreecommitdiff
path: root/includes/theme.inc
diff options
context:
space:
mode:
Diffstat (limited to 'includes/theme.inc')
-rw-r--r--includes/theme.inc93
1 files changed, 57 insertions, 36 deletions
diff --git a/includes/theme.inc b/includes/theme.inc
index 6f436df02..f5812999f 100644
--- a/includes/theme.inc
+++ b/includes/theme.inc
@@ -382,15 +382,14 @@ function _theme_process_registry(&$cache, $name, $type, $theme, $path) {
$result[$hook]['includes'][] = $include_file;
}
- // If 'variables' have been defined previously, carry them forward.
- // This should happen if a theme overrides a Drupal defined theme
- // function, for example.
- if (!isset($info['variables']) && isset($cache[$hook]['variables'])) {
- $result[$hook]['variables'] = $cache[$hook]['variables'];
- }
- // Same for 'render element'.
- if (!isset($info['render element']) && isset($cache[$hook]['render element'])) {
- $result[$hook]['render element'] = $cache[$hook]['render element'];
+ // If these keys are left unspecified within overridden entries returned
+ // by hook_theme(), carry them forward from the prior entry. This is so
+ // that themes don't need to specify this information, since the module
+ // that registered the theme hook already has.
+ foreach (array('variables', 'render element', 'pattern', 'base hook') as $key) {
+ if (!array_key_exists($key, $info) && isset($cache[$hook][$key])) {
+ $result[$hook][$key] = $cache[$hook][$key];
+ }
}
// The following apply only to theming hooks implemented as templates.
@@ -837,7 +836,19 @@ function theme($hook, $variables = array()) {
}
// Invoke the variable processors, if any. The processors may specify
- // alternate suggestions for which hook's template/function to use.
+ // 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
+ // the base hook, but retain the suggestion as a high priority suggestion to
+ // be used unless overridden by a variable processor function.
+ if (isset($info['base hook'])) {
+ $base_hook = $info['base hook'];
+ $base_hook_info = $hooks[$base_hook];
+ if (isset($base_hook_info['preprocess functions']) || isset($base_hook_info['process functions'])) {
+ $variables['theme_hook_suggestion'] = $hook;
+ $hook = $base_hook;
+ $info = $base_hook_info;
+ }
+ }
if (isset($info['preprocess functions']) || isset($info['process functions'])) {
$variables['theme_hook_suggestions'] = array();
foreach (array('preprocess functions', 'process functions') as $phase) {
@@ -960,44 +971,53 @@ function path_to_theme() {
* @param $prefixes
* An array of prefixes to test, in reverse order of importance.
*
- * @return $templates
+ * @return $implementations
* The functions found, suitable for returning from hook_theme;
*/
function drupal_find_theme_functions($cache, $prefixes) {
- $templates = array();
+ $implementations = array();
$functions = get_defined_functions();
foreach ($cache as $hook => $info) {
foreach ($prefixes as $prefix) {
+ // Find theme functions that implement possible "suggestion" variants of
+ // registered theme hooks and add those as new registered theme hooks.
+ // The 'pattern' key defines a common prefix that all suggestions must
+ // start with. The default is the name of the hook followed by '__'. An
+ // 'base hook' key is added to each entry made for a found suggestion,
+ // so that common functionality can be implemented for all suggestions of
+ // the same base hook. To keep things simple, deep heirarchy of
+ // suggestions is not supported: each suggestion's 'base hook' key
+ // refers to a base hook, not to another suggestion, and all suggestions
+ // are found using the base hook's pattern, not a pattern from an
+ // intermediary suggestion.
$pattern = isset($info['pattern']) ? $info['pattern'] : ($hook . '__');
- if (!empty($pattern)) {
+ if (!isset($info['base hook']) && !empty($pattern)) {
$matches = preg_grep('/^' . $prefix . '_' . $pattern . '/', $functions['user']);
if ($matches) {
foreach ($matches as $match) {
$new_hook = str_replace($prefix . '_', '', $match);
$arg_name = isset($info['variables']) ? 'variables' : 'render element';
- $templates[$new_hook] = array(
+ $implementations[$new_hook] = array(
'function' => $match,
$arg_name => $info[$arg_name],
+ 'base hook' => $hook,
);
}
}
}
+ // Find theme functions that implement registered theme hooks and include
+ // that in what is returned so that the registry knows that the theme has
+ // this implementation.
if (function_exists($prefix . '_' . $hook)) {
- $templates[$hook] = array(
+ $implementations[$hook] = array(
'function' => $prefix . '_' . $hook,
);
- // Ensure that the pattern is maintained from base themes to its sub-themes.
- // Each sub-theme will have their functions scanned so the pattern must be
- // held for subsequent runs.
- if (isset($info['pattern'])) {
- $templates[$hook]['pattern'] = $info['pattern'];
- }
}
}
}
- return $templates;
+ return $implementations;
}
/**
@@ -1011,7 +1031,7 @@ function drupal_find_theme_functions($cache, $prefixes) {
* The path to search.
*/
function drupal_find_theme_templates($cache, $extension, $path) {
- $templates = array();
+ $implementations = array();
// Collect paths to all sub-themes grouped by base themes. These will be
// used for filtering. This allows base themes to have sub-themes in its
@@ -1034,9 +1054,12 @@ function drupal_find_theme_templates($cache, $extension, $path) {
// Escape the periods in the extension.
$regex = '/' . str_replace('.', '\.', $extension) . '$/';
- // Because drupal_system_listing works the way it does, we check for real
- // templates separately from checking for patterns.
+ // Get a listing of all template files in the path to search.
$files = drupal_system_listing($regex, $path, 'name', 0);
+
+ // Find templates that implement registered theme hooks and include that in
+ // what is returned so that the registry knows that the theme has this
+ // implementation.
foreach ($files as $template => $file) {
// Ignore sub-theme templates for the current theme.
if (strpos($file->uri, str_replace($subtheme_paths, '', $file->uri)) !== 0) {
@@ -1052,24 +1075,21 @@ function drupal_find_theme_templates($cache, $extension, $path) {
// for the purposes of searching.
$hook = strtr($template, '-', '_');
if (isset($cache[$hook])) {
- $templates[$hook] = array(
+ $implementations[$hook] = array(
'template' => $template,
'path' => dirname($file->uri),
);
}
- // Ensure that the pattern is maintained from base themes to its sub-themes.
- // Each sub-theme will have their templates scanned so the pattern must be
- // held for subsequent runs.
- if (isset($cache[$hook]['pattern'])) {
- $templates[$hook]['pattern'] = $cache[$hook]['pattern'];
- }
}
+ // Find templates that implement possible "suggestion" variants of registered
+ // theme hooks and add those as new registered theme hooks. @see
+ // drupal_find_theme_functions() for more information about suggestions and
+ // the use of 'pattern' and 'base hook'.
$patterns = array_keys($files);
-
foreach ($cache as $hook => $info) {
$pattern = isset($info['pattern']) ? $info['pattern'] : ($hook . '__');
- if (!empty($pattern)) {
+ if (!isset($info['base hook']) && !empty($pattern)) {
// Transform _ in pattern to - to match file naming scheme
// for the purposes of searching.
$pattern = strtr($pattern, '_', '-');
@@ -1080,16 +1100,17 @@ function drupal_find_theme_templates($cache, $extension, $path) {
$file = substr($match, 0, strpos($match, '.'));
// Put the underscores back in for the hook name and register this pattern.
$arg_name = isset($info['variables']) ? 'variables' : 'render element';
- $templates[strtr($file, '-', '_')] = array(
+ $implementations[strtr($file, '-', '_')] = array(
'template' => $file,
'path' => dirname($files[$match]->uri),
$arg_name => $info[$arg_name],
+ 'base hook' => $hook,
);
}
}
}
}
- return $templates;
+ return $implementations;
}
/**