List view, grouped by taxonomy/month?
| Project: | Views 'Group-By' Pack |
| Version: | 5.x-1.3 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
I would like to be able to get a list view grouped by taxonomy (and/or month), with the option of choosing fields to display, just as in the ordinary list view. I tried writing something to do this myself, but I don't really know PHP, so I was trying to work out how a hybrid of theme_taxonomygroup_group and theme_views_view_list should work, and I don't think I was successful. I mean, I do now have the "option" to select 'Grouped by Taxonomy Term, List' when editing a view, but the resulting view doesn't work. Currently, if I add a field to the view and give it a label, I get a list consisting solely of copies of the label -- as many copies as there would be items in the list normally -- but the field itself is not displayed, there is no grouping of the list of labels, and no taxonomy terms are displayed. If anyone can tell me where the problem lies in my code, I would much appreciate it.
In views.group_pack.module, I added the following to views_group_pack_views_style_plugins:
$items['taxonomygroup_list'] = array(
'name' => t("Grouped By Taxonomy Term, List"),
'theme' => "taxonomygroup_group_list",
'summary_theme' => "views_summary",
);And I added the following definitions to taxonomygroup.inc:
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 .= 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();
foreach ($termlist as $node) {
$item = '';
foreach ($view->field as $field) {
if (!isset($fields[$field['id']]['visible']) && $fields[$field['id']]['visible'] !== FALSE) {
if ($field['label']) {
$item .= "<div class='view-label ". views_css_safe('view-label-'. $field['queryname']) ."'>" . $field['label'] . "</div>";
}
$item .= "<div class='view-field ". views_css_safe('view-data-'. $field['queryname']) ."'>" . views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view) . "</div>";
}
}
$items[] = "<div class='view-item ". views_css_safe('view-item-'. $view->name) ."'>$item</div>\n"; // l($node->title, "node/$node->nid");
}
if ($items) {
return theme('item_list', $items);
}
}
#1
Gah, caught some of my own errors in the code. Changes posted below. Now I've got it to be grouped by taxonomy term, but still I only see the labels for the fields I want to show. Looking at the source HTML for the generated block, I surmise that the
views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view)calls in the function below aren't returning anything.
New version of the last function in the previous post:
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();
foreach ($termlist as $node) {
$item = '';
foreach ($view->field as $field) {
if (!isset($fields[$field['id']]['visible']) && $fields[$field['id']]['visible'] !== FALSE) {
if ($field['label']) {
$item .= "<div class='view-label ". views_css_safe('view-label-'. $field['queryname']) ."'>" . $field['label'] . "</div>";
}
$item .= "<div class='view-field ". views_css_safe('view-data-'. $field['queryname']) ."'>" . views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view) . "</div>";
}
}
$items[] = "<div class='view-item ". views_css_safe('view-item-'. $view->name) ."'>$item</div>\n"; // l($node->title, "node/$node->nid");
}
if ($items) {
$output .= theme('item_list', $items);
}
$output .= "</div>";
return $output;
}
#2
It's still buggy. If I chose to display the niode title as link, I get the link but its syntax is not correct as shown in the following resulting code.
<div class="view-field view-data-node-title"><a href="/node/2"/>
</div>