<?php/**
* Theming a common wrapper to supply html attributes without modifying templates.
*
* Wrapper templates have an extension of ".wrapper.php".
*//**
* These variables will be available to all templates owned by this theme or
* any sub-themes.
*/functionTHEME_NAME_preprocess(&$vars,$hook) {
global$theme_key;// From general to more specific, add these wrapper paths and template names.
// Applies to all templated hooks. More can be added through hook specific
// preprocess but these should be good enough for most cases. We want less
// templates, not more.
// Add paths to your wrapper templates. The default will sit in the base of
// your theme. path_to_theme() not used since it can lead to module owned templates.
$vars['template_wrapper_paths'][] =drupal_get_path('theme', $theme_key);$vars['template_wrappers'][] ='common';$vars['template_wrappers'][] =$hook;// Element attributes can be added either way but this is easier to enter...
$vars['wrapper_id'] ='';$vars['wrapper_classes'][] =$hook.'-wrapper';// ...While this structure allows passing through drupal_attributes().
$vars['wrapper_attributes_array']['id'] =&$vars['wrapper_id'];$vars['wrapper_attributes_array']['class'] =&$vars['wrapper_classes'];// Simple switch to trigger.
// You definitely don't want this on the page hook.
$vars['enable_wrapper'] =$hook!=='page' ? TRUE : FALSE;
}
/**
* Override of theme_render_template. This allows for template wrappers.
*/functionphptemplate_render_template($template_file, &$variables) {
process_template_wrapper($variables);// Template render.
extract($variables, EXTR_SKIP&&EXTR_REFS);ob_start();include"./$template_file";$contents=ob_get_contents();ob_end_clean();// Template wrapper.
if (!empty($variables['template_wrapper']) &&!empty($contents)) {
ob_start();include'./'.$variables['template_wrapper'];$contents=ob_get_contents();ob_end_clean();
}
return$contents;
}
/**
* A little helper to find the right wrapper template and to clean html attributes.
*/functionprocess_template_wrapper(&$variables) {
$variables['template_wrapper'] =$variables['wrapper_attributes'] ='';if (!empty($variables['enable_wrapper']) &&$template_wrapper=drupal_discover_template($variables['template_wrapper_paths'], $variables['template_wrappers']), '.wrapper.php') {
$attributes=array();foreach ($variables['wrapper_attributes_array'] as$attribute=>$value) {
if (empty($value)) {
continue;
}
if (is_array($value)) {
$value=implode('', $value);
}
$attributes[$attribute] =$value;
}
$variables['wrapper_attributes'] =drupal_attributes($attributes);$variables['template_wrapper'] =$template_wrapper;
}
}
?><?php/**
* Within 'common.wrapper.php' or 'HOOK.wrapper.php':
*/?>
<div<?phpprint$wrapper_attributes?>>
<?phpprint$contents?></div>