Skip to content

Instantly share code, notes, and snippets.

@gagarine
Created August 26, 2011 03:35
Show Gist options
  • Save gagarine/1172621 to your computer and use it in GitHub Desktop.
Save gagarine/1172621 to your computer and use it in GitHub Desktop.
Drupal: Group more than one fields in a view
<?php
/**
* overwrite theme_views_view_list
*
* Group more than one fields in a view
*
*/
function template_views_view_list__VIEWNAME($view, $group, $row) {
//the field we want to group
$grouping_fields = array('node_node_data_field_mon_field', 'node_node_data_field_mon_field_2');
//Arrange in a nice array
foreach ($view->result as $index => $row) {
$grouping = array();
if (isset($view->result[$index]->$grouping_fields[0])) {
$grouping[0] = $view->result[$index]->$grouping_fields[0];
}
if (isset($view->result[$index]->$grouping_fields[1])) {
$grouping[1] = $view->result[$index]->$grouping_fields[1];
}
$sets[$grouping[0]][$grouping[1]][$index] = $grouping[1];
}
$sets_sets_sets = $sets;
$list = array();
//Go down the array and prepare a list
foreach ($sets_sets_sets as $main_group => $sets_sets) {
$group = array();
foreach ($sets_sets as $sets) {
$subgroup = array();
foreach ($sets as $sub_group => $title) {
//create a row
$row = '';
foreach ($view->field as $id => $handler) {
$row .= '<div>' . $view->style_plugin->rendered_fields[$sub_group][$id] . '</div>';
}
//add the row in the group
$subgroup[] = array('data' => $row);
}
$group[] = array($title, 'children' => $subgroup);
}
$list[] = array($main_group, 'children' => $group);
}
return theme('item_list', $list);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment