Skip to content

Instantly share code, notes, and snippets.

@ezheidtmann
Created January 29, 2014 20:49
Show Gist options
  • Save ezheidtmann/8696753 to your computer and use it in GitHub Desktop.
Save ezheidtmann/8696753 to your computer and use it in GitHub Desktop.
Add region-based suggestions for bean blocks (Drupal 7, bean module)
<?php
/**
* Implements hook_preprocess_block()
*
* For each bean block, add a template suggestion containing the name of the
* region where the block will appear.
*
* Requires "bean" module (fieldable blocks)
*/
function THEME_preprocess_entity(&$vars, $hook) {
if ($vars['entity_type'] == 'bean') {
if (isset($vars['elements']['#block'])) {
$region = $vars['elements']['#block']->region;
if ($region) {
$new_suggestions = array();
// Regions are usually lowercase and don't contain dashes, but it can't
// hurt to be sure.
$lc_region = str_replace('-', '_', strtolower($region));
$new_suggestions[] = $hook . '__region_' . $lc_region;
foreach ($vars['theme_hook_suggestions'] as $suggestion) {
$new_suggestions[] = $suggestion;
$new_suggestions[] = $suggestion . '__region_' . $lc_region;
}
$vars['theme_hook_suggestions'] = $new_suggestions;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment