Skip to content

Instantly share code, notes, and snippets.

@jameswilson
Last active August 29, 2015 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameswilson/74c2dc3ee2df0efc2a5c to your computer and use it in GitHub Desktop.
Save jameswilson/74c2dc3ee2df0efc2a5c to your computer and use it in GitHub Desktop.
Move content from a panel pane region into a Drupal theme region using drupal_add_region_content(). This requires a small core patch https://www.drupal.org/node/713462.
<?php
// path/to/mytheme/plugins/layouts/pull_hero_region_layout/pull_hero_region_layout.tpl.php
/**
* @file
* Template for the "pull_hero_region_layout" layout.
*
*
* Variables:
* - $has_content: Boolean TRUE if any regions have content to display.
* - $attributes: A list of attributes for the layout container.
* - $content: An array of content, each item in the array is keyed to one
* region of the layout. For example, $content['hero'] or $content['main'].
*/
?>
<?php if ($has_content) : ?>
<div<?php print $attributes; ?>>
<?php if ($content['hero']) : ?>
<div class="l-region--hero">
<?php print render($content['hero']); ?>
</div>
<?php endif; ?>
<div class="l-row-main">
<?php if ($content['main']) : ?>
<div class="l-region--main">
<?php print render($content['main']); ?>
</div>
<?php endif; ?>
<?php if ($content['sidebar']) : ?>
<div class="l-region--sidebar">
<?php print render($content['sidebar']); ?>
</div>
<?php endif; ?>
</div>
</div>
<?php endif ?>
<?php
// path/to/mytheme/plugins/layouts/pull_hero_region_layout/pull_hero_region_layout.inc
/**
* This example assumes that our theme has a 'Hero' region placed above the
* normal 'Content' region and we want to allow site admins to add slideshows
* or images into this region through Panels admin UI.
*/
$plugin = array(
'title' => t('Pull Hero Region Layout'),
'category' => t('Custom'),
'icon' => 'icon.png',
'theme' => 'pull_hero_region_layout',
'css' => '../../css/styles.css',
'regions' => array(
'hero' => t('Hero'),
'main' => t('Main'),
),
);
/**
* Implements hook_preprocess_HOOK().
*/
function template_preprocess_pull_hero_region_content(&$variables, $hook) {
$variables['attributes_array']['class'] = array('pull-hero-region-content');
// Extract panes in the hero panel region and place them in the theme region.
// This should only be done when viewing the panel on the front-end, not while
// in the Panels editor admin UI.
if (get_class($variables['renderer']) != 'panels_renderer_editor'
&& isset($variables['content']['hero'])) {
drupal_add_region_content('hero', $variables['content']['highlighted']);
$variables['content']['hero'] = '';
}
// Provide a way to use the hero region without printing the rest
// of the panel regions.
$variables['has_content'] = FALSE;
foreach($variables['content'] as $region => $content) {
$variables['has_content'] |= !empty($content);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment