Skip to content

Instantly share code, notes, and snippets.

@pixelwhip
Created July 7, 2013 23:57
Show Gist options
  • Save pixelwhip/5945421 to your computer and use it in GitHub Desktop.
Save pixelwhip/5945421 to your computer and use it in GitHub Desktop.
Drupal cTools/panels custom layout. Creating dynamic layout classes based on whether or not regions have content.
//
// @file
// Sidebars After Layout.
// One main content area with two sidebars after.
// ----------------------------------------------------------------------------
.l--3up {
@include breakpoint(41em, $no-query: '.lt-ie9') {
.l-main {
@include zen-grid-item(8, 1);
padding-left: 0;
padding-right: 30px;
}
.l-sidebar-first {
@include zen-grid-item(8, 9);
padding-left: 15px;
padding-right: 15px;
}
.l-sidebar-second {
@include zen-clear(right);
@include zen-grid-item(8, 1, right);
padding-left: 30px;
}
&.l--1-0-0 .l-main,
&.l--0-1-0 .l-sidebar-first,
&.l--0-0-1 .l-sidebar-second {
@include zen-grid-item(8, 9);
padding-left: 15px;
padding-right: 15px;
}
&.l--1-1-0 .l-main,
&.l--1-0-1 .l-main,
&.l--0-1-1 .l-sidebar-first {
@include zen-grid-item(8, 5);
padding-left: 0;
padding-right: 30px;
}
&.l--1-1-0 .l-sidebar-first,
&.l--1-0-1 .l-sidebar-second,
&.l--0-1-1 .l-sidebar-second {
@include zen-grid-item(8, 5, right);
padding-left: 30px;
padding-right: 0;
}
}
@include breakpoint($bp-two-sidebars, $no-query: '.lt-ie9') {
.l-main {
padding-right: 50px;
}
.l-sidebar-first {
padding-left: 25px;
padding-right: 25px;
}
.l-sidebar-second {
padding-left: 50px;
}
}
}
<?php
// cTools Layout definition
// Plugin definition
$plugin = array(
'title' => t('Resource'),
'category' => t('WRI'),
'icon' => 'resource.png',
'theme' => 'wri_layouts_resource',
'admin css' => 'resource.css',
'regions' => array(
'header' => t('Header'),
'main' => t('Main'),
'meta' => t('Meta'),
'supplement_main' => t('Supplement Main'),
'supplement_sidebar_first' => t('Supplement Sidebar First'),
'supplement_sidebar_second' => t('Supplement Sidebar Second'),
),
);
/**
* Implements template_preprocess_wri_layouts_project_level_2
*/
function wri_layouts_preprocess_wri_layouts_resource(&$vars) {
$content = $vars['content'];
// Create a dynamic class for the Supplement regions
$vars['supplement_attributes_array']['class'][] = wri_layouts_create_region_collection_classes(
array(
$content['supplement_main'],
$content['supplement_sidebar_first'],
$content['supplement_sidebar_second'],
)
);
$vars['supplement_classes'] = implode(' ', $vars['supplement_attributes_array']['class']);
}
<?php
/**
* @file
* Template for a resource layout.
*
*/
?>
<article class="l--resource" <?php print $attributes; ?>>
<?php if ($content['header']): ?>
<header class="l--constrained resource-header">
<?php print $content['header']; ?>
</header>
<?php endif; ?>
<?php if ($content['main'] || $content['meta']): ?>
<div class="l--sidebar-after l--constrained">
<?php if ($content['main']): ?>
<div class="l-main resource-main">
<?php print $content['main']; ?>
</div>
<?php endif; ?>
<?php if ($content['meta']): ?>
<footer class="l-sidebar resource-meta">
<?php print $content['meta']; ?>
</footer>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if ($content['supplement_main'] || $content['supplement_sidebar_first'] || $content['supplement_sidebar_second']): ?>
<section class="l--3up l--constrained resource-supplement <?php print $supplement_classes; ?>">
<?php if ($content['supplement_main']): ?>
<aside class="l-main">
<?php print $content['supplement_main']; ?>
</aside>
<?php endif; ?>
<?php if ($content['supplement_sidebar_first']): ?>
<aside class="l-sidebar-first">
<?php print $content['supplement_sidebar_first']; ?>
</aside>
<?php endif; ?>
<?php if ($content['supplement_sidebar_second']): ?>
<aside class="l-sidebar-second">
<?php print $content['supplement_sidebar_second']; ?>
</aside>
<?php endif; ?>
</section>
<?php endif; ?>
</article>
<?php
/** ... */
/**
* Creates a layout class based on the availability of regions in a given array.
*/
function wri_layouts_create_region_collection_classes($regions) {
$class = 'l-';
// Construct a class based on if regions are empty or not.
foreach ($regions as $key => $region) {
$class .= empty($region) ? '-0' : '-1';
}
return $class;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment