0

In Drupal 6, I have a view for certain objects with Location:Country and Location:Province fields, both set to Exclude From Display; sort criteria to sort first by country then by province; and Style setting Grouping Field to be Location:Province. That displays all the objects in the order I want, everything with the same country grouped together, then within each country objects are grouped by Province/State, each state's group of objects labelled with the name of the state.

However, I would like to have a group title displayed for Country, not just Province, so it would look like

Canada
  Ontario
    object 1
    object 2
  Quebec
    object 3
United States
  Arizona
    object 4
  California
    object 5
    object 6

I'm a newbie to Drupal. Is there a standard way to do this? I've tried search terms such as nested grouping and multiple groups but haven't found mention of this use case.

0

1 Answer 1

1

If you are comfortable with theme templates, you could do this by modifying the Style output, eg:

enter image description here

for your View. Here I created a quick View called for-da-grouping like this:

enter image description here

which has the node title and then two fields, country and province which are excluded from display. I am sorting first on Country and then within that Province, so, given the same information in your example, this produces:

enter image description here

But, if you add some logic to your new Views template:

<?php
/**
 * @file views-view-unformatted.tpl.php
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 */

$country='';
$province='';

/*
  drupal_set_message('<pre>' . print_r($view->result[0], TRUE) . '</pre>');

    [node_title] => object 1
    [nid] => 660
    [node_language] => 
    [node_data_field_da_country_field_da_country_value] => Canada
    [node_type] => page
    [node_vid] => 660
    [node_data_field_da_country_field_da_province_value] => Ontario

*/

?>
<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <?php if ($view->result[$id]->node_data_field_da_country_field_da_country_value!=$country): ?>
    <div class="country-group-title">
      <?php print check_plain($view->result[$id]->node_data_field_da_country_field_da_country_value); ?>
      <?php $country=$view->result[$id]->node_data_field_da_country_field_da_country_value; ?>
    </div>
  <?php endif; ?>
  <?php if ($view->result[$id]->node_data_field_da_country_field_da_province_value!=$province): ?>
    <div class="province-group-title">
      <?php print check_plain($view->result[$id]->node_data_field_da_country_field_da_province_value); ?>
      <?php $province=$view->result[$id]->node_data_field_da_country_field_da_province_value; ?>
    </div>
  <?php endif; ?>
  <div class="<?php print $classes[$id]; ?>">
    <?php print $row; ?>
  </div>
<?php endforeach; ?>

You can get:

enter image description here

Now, things to look at in specific:

  1. You need to sort your View first by country and then by province so the data is in the right order to begin with.

  2. You need to know your Views results, they are stored in $view->result and you can get what you need out of looking at $view->result[0] and in my view, my two fields I want to group on are called node_data_field_da_country_field_da_country_value and node_data_field_da_country_field_da_province_value but yours are most probably going to be different but the logic is still the same: Only display the country and/or province if it has changed since the last time it was displayed.

  3. Add some css to your new group headings and you should be all set.

If not, you can investigate the Views Field Module and so forth mentioned in https://stackoverflow.com/questions/9015500/how-to-group-by-more-than-one-field-in-drupal-view for alternative methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.