New View Type : Sorted by taxonomy - Results in a table
jmary - March 11, 2009 - 21:49
| Project: | Views 'Group-By' Pack |
| Version: | 5.x-1.3 |
| Component: | Code |
| Category: | task |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
| Issue tags: | features |
Description
Add
<?php
$items['taxonomygroup_table'] = array(
'name' => t("Grouped By Taxonomy Term, Table"),
'theme' => "taxonomygroup_group_table",
'summary_theme' => "views_summary",
);
}
?>in the function views_group_pack_views_style_plugins in the views_group_pack_module
Then add this in taxonomygroup.inc and clear the views cache.
<?php
function theme_taxonomygroup_group_table($view, $nodes, $type) {
drupal_add_css(drupal_get_path('module', 'views_group_pack').'/taxonomygroup.css');
if (isset($view->sort[0]['field'])) {
$sort_field = $view->sort[0]['field'];
} // if it's got a sort field
if ($sort_field != 'term_data.weight') {
drupal_set_message(t("The view must be sorted first by a 'taxonomy term' field, in order to be Grouped By Taxonomy Term."), 'error');
return theme('views_view_teasers', $view, $nodes, $type);
} // if the sort field isn't set
$by_taxonomy = array();
foreach ($nodes as $proto_node) {
$node = node_load($proto_node->nid);
$val = array_values($node->taxonomy);
$term = $val[0]->name;
$by_taxonomy[$term][] = $node;
} // foreach node
return theme("taxonomygroup_taxonomylist", $view, $by_taxonomy, $type);
}
function theme_taxonomygroup_taxonomytable($view, $by_taxonomy, $type) {
$output = "<div id=\"taxonomygroup\" class=\"taxonomygroup\">";
foreach ($by_taxonomy as $term => $termlist) {
$output .= theme('taxonomygroup_listterm', $termlist, $term, $view);
} // foreach term
$output .= "</div><!-- taxonomygroup -->";
return $output;
}
function theme_taxonomygroup_tableterm($termtable, $term, $view) {
$output = "";
$output .= "<div id=\"taxonomygroup-{$term}-header\" class=\"taxonomygroup-header taxonomygroup-{$term}-header\">{$term}</div>";
$output .= "<div class=\"taxonomygroup-content taxonomygroup-{$term}-content\">";
$fields = _views_get_fields();
foreach ($termlist as $node) {
$row = array();
foreach ($view->field as $field) {
if ($fields[$field['id']]['visible'] !== FALSE) {
// THE FOLLOWING LINE IS EXTREMELY DIRTY
$field['queryname']='title'; // EXTREMELY DIRTY
// THE PRECEDENT LINE IS EXTREMELY DIRTY
/* THE REASON OF THAT DIRTY TRICK IS THAT views_handler_field_nodelink will be called with $node -> $field['queryname'] for the $value parameter, making it to be empy because $niode doesn't have a node_title field as proposed by $field[''queryname']. $node just have title field. So I change that in the field array, not without shame. */
$cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
// dprint_r($field);
$cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
$row[] = $cell;
}
}
$rows[] = $row;
}
return theme('table', $view->table_header, $rows);
}
?>
#1
#2
There was a mistake in the precedant code preventing it to work. It should be correct now.
<?php
function theme_taxonomygroup_group_table($view, $nodes, $type) {
drupal_add_css(drupal_get_path('module', 'views_group_pack').'/taxonomygroup.css');
if (isset($view->sort[0]['field'])) {
$sort_field = $view->sort[0]['field'];
} // if it's got a sort field
if ($sort_field != 'term_data.weight') {
drupal_set_message(t("The view must be sorted first by a 'taxonomy term' field, in order to be Grouped By Taxonomy Term."), 'error');
return theme('views_view_teasers', $view, $nodes, $type);
} // if the sort field isn't set
$by_taxonomy = array();
foreach ($nodes as $proto_node) {
$node = node_load($proto_node->nid);
$val = array_values($node->taxonomy);
$term = $val[0]->name;
$by_taxonomy[$term][] = $node;
} // foreach node
return theme("taxonomygroup_taxonomytable", $view, $by_taxonomy, $type);
}
function theme_taxonomygroup_taxonomytable($view, $by_taxonomy, $type) {
$output = "<div id=\"taxonomygroup\" class=\"taxonomygroup\">";
foreach ($by_taxonomy as $term => $termlist) {
$output .= theme('taxonomygroup_tableterm', $termlist, $term, $view);
} // foreach term
$output .= "</div><!-- taxonomygroup -->";
return $output;
}
function theme_taxonomygroup_tableterm($termtable, $term, $view) {
$output = "";
$output .= "<div id=\"taxonomygroup-{$term}-header\" class=\"taxonomygroup-header taxonomygroup-{$term}-header\">{$term}</div>";
$output .= "<div class=\"taxonomygroup-content taxonomygroup-{$term}-content\">";
$fields = _views_get_fields();
foreach ($termtable as $node) {
$row = array();
foreach ($view->field as $field) {
if ($fields[$field['id']]['visible'] !== FALSE) {
// THE FOLLOWING LINE IS EXTREMELY DIRTY
$field['queryname']='title'; // EXTREMELY DIRTY
// THE PRECEDENT LINE IS EXTREMELY DIRTY
/* THE REASON OF THAT DIRTY TRICK IS THAT views_handler_field_nodelink will be called with $node -> $field['queryname'] for the $value parameter, making it to be empy because $niode doesn't have a node_title field as proposed by $field[''queryname']. $node just have title field. So I change that in the field array, not without shame. */
$cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
// dprint_r($field);
$cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
$row[] = $cell;
}
}
$rows[] = $row;
}
return theme('table', $view->table_header, $rows);
}
?>
#3
I have rewritten all of that - consequently :
1. It is much shorter.
2. More logical
3. Clean
I'll post this there soon.
#4
I too had the same requirement. The coding helped me a lot. But I need to display other cck field types also in the table, only the node title and the taxonomy term are getting displayed in the grouped table view. Need help in displaying other fields also in the table.
Thanks.
#5
In the file views_group_pack.module,modify the the function views_group_pack_views_style_plugins :
<?php
function views_group_pack_views_style_plugins() {
$items = array();
if (module_exists('taxonomy')) {
$items['taxonomygroup'] = array(
'name' => t("Grouped By Taxonomy Term, Full Nodes"),
'theme' => "taxonomygroup_group",
'summary_theme' => "views_summary",
);
$items['taxonomygroup_teaser'] = array(
'name' => t("Grouped By Taxonomy Term, Teasers"),
'theme' => "taxonomygroup_group_teaser",
'summary_theme' => "views_summary",
);
$items['taxonomygroup_list'] = array(
'name' => t("Grouped By Taxonomy Term, List"),
'theme' => "taxonomygroup_group_list",
'summary_theme' => "views_summary",
);
$items['taxonomygroup_table'] = array(
'name' => t("Grouped By Taxonomy Term, Table"),
'theme' => "taxonomygroup_group_table",
//'theme' => "views_view_table",
'validate' => 'views_ui_plugin_validate_table',
'needs_fields' => true,
// 'needs_table_header' => true,
'summary_theme' => "views_summary",
);
}
if (module_exists('date')) {
$items['monthgroup'] = array(
'name' => t("Grouped By Month, Full Nodes"),
'theme' => "monthgroup_group",
'summary_theme' => "views_summary",
);
$items['monthgroup_teaser'] = array(
'name' => t("Grouped By Month, Teasers"),
'theme' => "monthgroup_group_teaser",
'summary_theme' => "views_summary",
);
}
return $items;
}
?>
Then in the file taxonomygroup.inc add the functions :
<?php
function theme_taxonomygroup_group_list($view, $nodes, $type) {
drupal_add_css(drupal_get_path('module', 'views_group_pack').'/taxonomygroup.css');
if (isset($view->sort[0]['field'])) {
$sort_field = $view->sort[0]['field'];
} // if it's got a sort field
if ($sort_field != 'term_data.weight') {
drupal_set_message(t("The view must be sorted first by a 'taxonomy term' field, in order to be Grouped By Taxonomy Term."), 'error');
return theme('views_view_teasers', $view, $nodes, $type);
} // if the sort field isn't set
$by_taxonomy = array();
foreach ($nodes as $proto_node) {
$node = node_load($proto_node->nid);
$val = array_values($node->taxonomy);
$term = $val[0]->name;
$by_taxonomy[$term][] = $node;
} // foreach node
return theme("taxonomygroup_taxonomylist", $view, $by_taxonomy, $type);
}
function theme_taxonomygroup_taxonomylist($view, $by_taxonomy, $type) {
$output = "<div id=\"taxonomygroup\" class=\"taxonomygroup\">";
foreach ($by_taxonomy as $term => $termlist) {
$output .= "<h3>$term</h3>";
$output .= theme('taxonomygroup_listterm', $termlist, $term, $view);
} // foreach term
$output .= "</div><!-- taxonomygroup -->";
return $output;
}
function theme_taxonomygroup_listterm($termlist, $term, $view) {
$output = "";
$output .= "<div id=\"taxonomygroup-{$term}-header\" class=\"taxonomygroup-header taxonomygroup-{$term}-header\">{$term}</div>";
$output .= "<div class=\"taxonomygroup-content taxonomygroup-{$term}-content\">";
$fields = _views_get_fields(); // collect all possible field to be displayed
foreach ($termlist as $node) {
$row = array();
foreach ($view->field as $field) {
if ($fields[$field['id']]['visible'] !== FALSE) {
$field['queryname']='title';
$cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
$cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
$row[] = $cell;
}
}
$rows[] = $row;
}
return theme('table', $view->table_header, $rows);
}
function theme_taxonomygroup_group_table($view, $nodes, $type) {
drupal_add_css(drupal_get_path('module', 'views_group_pack').'/taxonomygroup.css');
if (isset($view->sort[0]['field'])) {
$sort_field = $view->sort[0]['field'];
} // if it's got a sort field
if ($sort_field != 'term_data.weight') {
drupal_set_message(t("The view must be sorted first by a 'taxonomy term' field, in order to be Grouped By Taxonomy Term."), 'error');
return theme('views_view_teasers', $view, $nodes, $type);
} // if the sort field isn't set
$by_taxonomy = array();
foreach ($nodes as $proto_node) {
// we create a table indexed on taxo terms containig group of nodes to be rendered as tables.
$ju_nodes[$proto_node->term_data_name][]=$proto_node;
} // foreach node
$output = '';
foreach($ju_nodes as $nodes_to_render) {
$output .= "<div id=\"taxonomygroup\" class=\"taxonomygroup\">";
$term = $nodes_to_render[0]->term_data_name;
$output .= "<h3><a name=\"{$term}\">{$term}</a></h3>";
$output .= theme("views_view_table", $view, $nodes_to_render, $type);
$output .= "</div><!-- taxonomygroup -->";
}
return $output;
}
function theme_taxonomygroup_taxonomytable($view, $by_taxonomy, $type) {
$output = "<div id=\"taxonomygroup\" class=\"taxonomygroup\">";
foreach ($by_taxonomy as $term => $termlist) {
$output .= "<h3><a name=\"{$term}\">{$term}</a></h3>";
$output .= theme('taxonomygroup_tableterm', $termlist, $term, $view);
} // foreach term
$output .= "</div><!-- taxonomygroup -->";
return $output;
}
function theme_taxonomygroup_tableterm($termtable, $term, $view) {
return theme_views_view_table($view,$termtable,$term);
}
?>
All of that would deserve that the maintainer of the module is releasing a new version but he looks dead for his paerticipations on that.
I've put the archive of all the files as I am using them on prod.