Add a Drupal template suggestion for Panels: page-panel.tpl.php

Drupal has a standard array of template suggestions that let you specify which TPL file should be used according to node type, node id, etc. However, there is no default template suggestion for pages generated by Panels.

You can easily fix that by adding a little snippet to your template.php files' preprocess_page() function.

<?php
function grasmash_preprocess_page(&$vars, $hook) {
 
// if this is a panel page, add template suggestions
 
if($panel_page = page_manager_get_current_page()) {
    
     
// add a generic suggestion for all panel pages
     
$suggestions[] = 'page-panel';
     
     
// add the panel page machine name to the template suggestions
     
$suggestions[] = 'page-' . $panel_page['name'];
     
     
// merge the suggestions in to the existing suggestions (as more specific than the existing suggestions)
     
$vars['template_files'] = array_merge($vars['template_files'], $suggestions);
     
     
//add a body class for good measure
     
$body_classes[] = 'page-panel';
    }
  } 
}
?>

You can also preprocess the the page for specific Panels layouts:

<?php
function grasmash_preprocess_page(&$vars, $hook) {
 
// if this is a panel page, add template suggestions
 
if($panel_page = page_manager_get_current_page()) {
    
     
// add a generic suggestion for all panel pages
     
$suggestions[] = 'page-panel';
     
     
// add the panel page machine name to the template suggestions
     
$suggestions[] = 'page-' . $panel_page['name'];
     
     
// merge the suggestions in to the existing suggestions (as more specific than the existing suggestions)
     
$vars['template_files'] = array_merge($vars['template_files'], $suggestions);
  
     
$display = panels_get_current_page_display();   
     
$layout = $display->layout;
     
$body_classes[] = 'panel-layout-' . $layout;
     
     
//add a body class for good measure
     
$body_classes[] = 'page-panel';
    }
  } 
}
?>

And there you have it.

Drupal Version Compatibility: 

Comments

I'm not sure why you'd need this, since you can already write your own Panels plugins for your custom theme.
Just add plugins[panels][layouts] = to your THEME.info
Then, when panels is running, it will look for display layouts in there.

Nice to find some decent content for once, I am getting tired of the continual drivel I find on a daily basis, respect.

I'm getting a white screen error:
Cannot redeclare xxx_preprocess_page() ()... in
all/themes/NaiveArtOnlineColorful/drupal7_theme_methods.php on line 59

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.