howdy.

i'm trying to make a node view that has both a node and its comments, with one row per node.

if i add both node and comment fields to the view, i get one row per node, per comment - so the nodes are duplicated.

if i use nid as a grouping field in the unformatted style settings, i don't see any grouping. i still get one row per node per comment

if i use distinct, i get one row per node and only the first comment

i don't have theme developer enabled, so this is not a theme developer issue, as seems to be the case often with grouping issues.

can views2 do this kind of grouping? i haven't been able to find any good documentation (in advanced_help or otherwise) on the grouping field in the style options. if someone can explain, or point me to documentation on what this setting can and can't do, i'll try to add help text...

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

firebus’s picture

here's an export of the view in question

$view = new view;
$view->name = 'test';
$view->description = 'test';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('fields', array(
  'nid' => array(
    'label' => '',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'absolute' => 0,
      'link_class' => '',
      'alt' => '',
      'rel' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
      'strip_tags' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'link_to_node' => 0,
    'exclude' => 0,
    'id' => 'nid',
    'table' => 'node',
    'field' => 'nid',
    'relationship' => 'none',
  ),
  'title' => array(
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
  ),
  'subject' => array(
    'label' => '',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
      'strip_tags' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'link_to_comment' => 1,
    'exclude' => 0,
    'id' => 'subject',
    'table' => 'comments',
    'field' => 'subject',
    'relationship' => 'none',
  ),
));
$handler->override_option('filters', array(
  'status' => array(
    'operator' => '=',
    'value' => '1',
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'status',
    'table' => 'node',
    'field' => 'status',
    'relationship' => 'none',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('cache', array(
  'type' => 'none',
));
$handler->override_option('title', 'test');
$handler->override_option('use_pager', '0');
$handler->override_option('distinct', 0);
$handler->override_option('style_options', array(
  'grouping' => 'nid',
));
$handler->override_option('row_options', array(
  'inline' => array(
    'nid' => 'nid',
    'title' => 'title',
    'subject' => 'subject',
  ),
  'separator' => '',
  'hide_empty' => 0,
));
$handler = $view->new_display('page', 'Page', 'page_1');
$handler->override_option('path', 'views/test');
$handler->override_option('menu', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));
$handler->override_option('tab_options', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));
merlinofchaos’s picture

Status: Active » Fixed

Views' idea of grouping is incredibly naive.

Probably the best way to accomplish something is to set up your rows as if you're viewing the comment, and make all of the node fields excluded. Then, in a preprocess for the row, check to see if the node has changed from the previous node (and for the first record, the previous node will be NULL. You can store the previews node on the $view object itself). If it has changed, set a template suggestion to use a different row template, and have that template place your fields more or less manually.

I realize it's not ideal, but that's the best i can offer.

merlinofchaos’s picture

You could probably actually do something similar attempting to use the 'grouping' mechanism, but the grouping only groups by one field and shows up as $title in the actual style template. However, you could probably figure out a row index that will contain the data you need and render that data long with the $title looking into $view->style_plugin->rendered_fields[$row_index].

firebus’s picture

thanks for your quick response, and no problem. i wish i had a handle on what the grouping field is attempting to do - eventually it will bug me enough and i'll dig into the code :)

i was also considering:

-making a separate comment view with an nid argument, and then embedding that view at the end of each node row

-displaying the rows node style with comments showing and themeing. this is probably the easiest, but requires putting some logic into the template that i would rather have in a preprocess function. although i guess i could have a preprocess function even if i was using a node display...

dawehner’s picture

Perhaps the module views_field_view helps you a bit.

merlinofchaos’s picture

The grouping field will re-organize your rows into a single level hierarchy. Let's say you're listing comments and grouping upon nid. You'll get:

$title = nid #1:
  comment 1
  comment 2
  comment 3
  comment ...n

$title = nid #2:
  comment 1
  comment 2
  comment 3
  comment ...n

...and so on.

Each 'group' will be a single instance of the style template. So if you've got a table, for example, then each 'group' will be a table.

Now, it's only single level, and only on a single field. Right now the giant weakness is that you can't put more into the $title to have a better, more interesting headers and you have to work pretty hard to get that. That's something that's been coming up in my mind, looking for ways to improve.

firebus’s picture

Component: Views Data » Documentation
Assigned: Unassigned » firebus

aha!

so, without a grouping field, we always get a single instance of the style template, and all rows in the result set are in that single template.

but with a grouping field, we get multiple instances of the style template, with each row pertaining to the grouping field in the individual style template.

and since we can only group on the single field - nid in this case - it makes sense that node title appears in each row, resulting in the perception of duplicate nodes. if we could group on every node field together, or on the node object as a whole, i could get what i wanted with the grouping field.

if that's correct then i think i can write some better help for this setting.

it seems like the existing help for grouping is in the documentation for "How should this view be styled" (help/views/style) instead of in "Style options" where the grouping field setting exists (e.g. help/views/style-unformatted).

since each style has its own help entry for "Style options" it would be annoying to repeat the same information in each one.

maybe it would make sense to add more grouping documentation to help/views/style but add a note in each individual style's help to go up one level to learn more about the grouping field?

firebus’s picture

Status: Fixed » Needs review
FileSize
3.98 KB

here's a patch that creates a new help file - style-grouping.html - with more verbose information on the grouping field as i understand it. style-grouping is a child of style.html, which also links explicitly to the new file.

it still bother's me that the closest help_icon to the grouping field (e.g. style-unformatted.html) doesn't mention the grouping field, and you kind of have to get lucky to find the grouping documentation in style.html.

however, i'd like to get some feedback from someone before i make links to style-grouping.html in all the other style-*.html help documents.

also, this is my first git patch, sorry in advance if it's borken in some way.

esmerel’s picture

Status: Needs review » Needs work

This looks pretty good, but the patch itself has some problems. It's removed quotes from around some of the titles; not sure if that's something that happened by accident with your editor or what.

Also, it looks like you're adding the same information to style.html and your new style-grouping page. I think you'd really only want one or the other. If you want a new page to handle grouping styles, that's fine; make a link to it from the original style page, but let's try to avoid duplicating the info across multiple pages if we can.

If you want to reroll the patch, that'd be fine, and you can put it back into Needs Review. If you don't want to reroll it, I can likely fix it myself when I get a chance!

firebus’s picture

hmm. i intended to just have a link to the style-grouping page from the style page, maybe i messed up my commit?

not sure what's up with the quotes.

let me take another look and give it another try, i'll do some more testing this time on my end...

firebus’s picture

Status: Needs work » Needs review
FileSize
2.74 KB

here's a new patch the fixes the issue with quotes in views.help.ini, i'm not sure what happened there - i must have done something stupid in my editor.

i don't think i see what you mean about adding the same information to both style.html and style-grouping.html. as i read both this patch, and the previous one, the discussion of grouping in style.html is shortened, and a link to style-grouping.html is added.

style-grouping.html contains a longer discussion of grouping.

if you are getting a different result in style.html when you apply the patch, something weird is going on! could you provide your before and after style.html if that's the case?

i followed the instructions here to create my patch

esmerel’s picture

Status: Needs review » Fixed

committed to 3.x

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.