Last active
August 25, 2016 10:20
-
-
Save lcube45/781c6c84acb2acde4480cb17e1fec084 to your computer and use it in GitHub Desktop.
Drupal theme function for generating a bootstrap grid of nodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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