Panels magic

This blog post is more than 7 years old, so the content may be out of date.

I love unexpected discoveries.

Drupal's Panels module seems to find its way into most of my site builds, along with custom layouts, panel-panes and all sorts of goodness.

I used to declare custom panel layouts this way:

<?php
 
/**
 * Implementation of hook_panels_layouts().
 */
function modulename_layoutname_panels_layouts() {
  $items['layoutname'] = array(
    'title' => t('NAME OF LAYOUT'),
    'icon'  => 'layoutname.png',
    'theme' => 'panel_modulename_layoutname',
    'css'   => 'layoutname.css',
    'panels' => array(
      // 'regions' used in the layout
      'topleft'     => t('Top Left pane'),
      'topmiddle'   => t('Top Middle pane'),
      'topright'    => t('Top Right pane'),
      'bottomleft'  => t('Bottom Left pane'),
      'bottomright' => t('Bottom Right pane'),
     ),  
  );  
  return $items;
}

Turns out we don't need that excessively long function definition...it can be declared as a simple variable instead:

<?php
 
/**
 * Implementation of hook_panels_layouts().
 */
$plugin = array(
  'title' => t('NAME OF LAYOUT'),
  'icon'  => 'layoutname.png',
  'theme' => 'panel_modulename_layoutname',
  'css'   => 'layoutname.css',
  'panels' => array(
    // 'regions' used in the layout
    'topleft'     => t('Top Left pane'),
    'topmiddle'   => t('Top Middle pane'),
    'topright'    => t('Top Right pane'),
    'bottomleft'  => t('Bottom Left pane'),
    'bottomright' => t('Bottom Right pane'),
   ),  
);  

This relies on some ctools plugin-processing magic, which specifically looks for the $plugin variable (and this approach should work for all ctools plugins). I can see myself using this a lot :-)

Comments

Hey - I am really glad to find this. Good job!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <apache>, <bash>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.