Skip to content

Instantly share code, notes, and snippets.

@lcube45
Last active August 25, 2016 10:20
Show Gist options
  • Save lcube45/781c6c84acb2acde4480cb17e1fec084 to your computer and use it in GitHub Desktop.
Save lcube45/781c6c84acb2acde4480cb17e1fec084 to your computer and use it in GitHub Desktop.
Drupal theme function for generating a bootstrap grid of nodes
/**
* Implements hook_theme()
*/
function example_theme() {
return array(
'bootstrap_grid' => array(
'render element' => 'elements'
),
);
}
/**
* Generate a render array for bootstrap grid
* @param array $variables
* @return array $build (render array)
*/
function theme_bootstrap_grid($variables) {
$css_class = 12 / $variables['bootstrap_columns'];
unset($variables['nodes']['#sorted']);
$rows = array_chunk($variables['nodes'],$variables['bootstrap_columns'],true);
$build = array();
foreach($rows as $id => $nodes) {
$build[$id] = array(
'#prefix' => '<div class="row">',
'#suffix' => '</div>'
);
foreach($nodes as $nid => $node) {
$build[$id][$nid] = array(
'#prefix' => '<div class="col-md-'.$css_class.'">',
'#suffix' => '</div>',
'content' => $node
);
}
}
return $build;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment