Skip to content

Instantly share code, notes, and snippets.

@jenter
Last active November 28, 2016 21:58
Show Gist options
  • Save jenter/e79b913e75c9345e42c496c126244fc4 to your computer and use it in GitHub Desktop.
Save jenter/e79b913e75c9345e42c496c126244fc4 to your computer and use it in GitHub Desktop.
A preprocess function that adds count wrappers in Drupal regions.
use Drupal\Component\Utility\Html;
use Drupal\Core\Render\Markup;
use Drupal\Core\Render\Element;
/**
* Implements hook_preprocess_region().
*/
function MYTHEME_preprocess_region(&$variables) {
$elements = $variables['elements'];
$region_children = Element::children($elements);
$variables['region'] = $elements['#region'];
// Utility regional classes for $content.
$variables['content'] = array();
foreach ($region_children as $k => $v) {
$region_classes = Html::cleanCssIdentifier('region--' . ($k + 1) . '-of-' . count($region_children));
$variables['content'][] = '<div class="' . $region_classes . '">' . $variables['elements'][$v]['#markup'] . '</div>';
}
$variables['content'] = Markup::create(implode($variables['content']));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment