I try to create a view that shows all nodes sorted by a field.

The field is configured to display all values in the same row (see screenshot 1). So it makes no sense to show a nodes more than once. But every node is show as often as it has values of my sorting field (see screenshot 2).

The "distinct" option in the query settings doesn't change this.

CommentFileSizeAuthor
screenshot2.png15.4 KBdrcho
screenshot1.png24.36 KBdrcho
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Status: Active » Closed (works as designed)

Sure, there is no reason it should work.

It's just not possible to sort by a fieldapi field without joining it.
If you join it, you can't get rid of the duplicates. That's IMPOSSIBLE. Either choose to use sort or not.

drcho’s picture

Thank you for your explications that helps me to understand, I'm no database expert.

However, is it possible to remove the duplicates by the node ids, maybe after the views request. I tried to manipulate the $views->result it in the views-views-list.tpl.php but without any success.

A more realistic use case for my example: show event nodes with dates, but each event only once, and sorted by upcoming dates, like that:

Event 1
Dates: 2011-07-20, 2011-07-29

Event 2
Date: 2011-07-21

Event 3
Dates: 2011-07-24, 2011-08-05

I think this functionality would be very usefull. Any suggestions how I could result this?

drcho’s picture

Component: Miscellaneous » Documentation
Status: Closed (works as designed) » Active
dawehner’s picture

You could for example use hook_views_pre_render and alter $view->result.

In general at least from my perspective an event has just a single date, no multiple, but i might be wrong.

drcho’s picture

Thank you, I'll try it out.

An event could have multiple dates in multiple locations. My client doesn't want to create multiple nodes with the same description, categories, images and other infos just to change the date an location. I don't see another possibility for it.

drcho’s picture

Here is my solution if anybody else needs the solution:

(1) Add a function to your template functions in mytheme/template.php:

/*
* implements hook_view_pre_render()
*/
function mytheme_views_pre_render(&$view)
{
  switch($view->name)
  {
    case 'no_double_nodes_view':
      $nids = array(); // array of nodes to prevent duplicates
      $resultNoDoubleNodes = array(); // manipulated results
      foreach($view->result as $key => $result) // check each node of the result array on it's nid
      {
        $nid = $result->nid;
        if (!in_array($nid, $nids)) // if this node isn't a duplicate
        {
          $resultNoDoubleNodes[] = $view->result[$key]; // add it to the manipulated results
          $nids[$key] = $nid; // mark this nid as in results to prevent duplicates from now on
        };
      }
      $view->result = $resultNoDoubleNodes; // replace the old results with the results without duplicates
      break;
    default:
      break;
  }
}

(Sorry for the non drupal conform php formatting.)

(2) After that clear all caches:

drush cc
1

and it should display each node only once.

However, that would be a nice feature in the views ui :-)

mpotter’s picture

drcho: Thanks *very* much for posting that solution. It's clean, it works, and it doesn't mess with the SQL. It's a great way to say "I don't want any node displayed more than once in this view". Honestly, I rarely want a node duplicated in a view so I agree that this would be a good addition to views as an option. DISTINCT is just too flaky and most users just want the dups removed.

ursula’s picture

@drcho: Thanks a lot, exactly what I was looking for.
@dereine: I am listing opera productions, with identical performances on different days (identical for the purpose of the web-site). So in this case, the same general event has several dates.

dawehner’s picture

Status: Active » Fixed

So this issue seems to be fixed.

hansrossel’s picture

The code in #6 works but unfortunately does not recounts the pager.

drcho’s picture

My solution was provisorily for my view without pager.
I hoped this would be integrated into a new views feature but it dosn't seem so. It's a pity.

dawehner’s picture

@drcho
Don't complain... make a proper issue, with a good patch etc.
If you complain that we aren't doing enough, feel free to help on other issues as well.

drcho’s picture

@dereine
you are right, but i'm not a programmer and at the moment i don't know how to create a good patch for views. for my actual project my solution is working. maybe one day when i need it and when i have more experience with developing on drupal.

Status: Fixed » Closed (fixed)

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

jackalope’s picture

Component: Documentation » Code
Status: Closed (fixed) » Active

Don't want to be disrespectful, but I'd like to open this issue again in the hopes that someone might be able to figure out a fix. I think this problem might come up frequently on arts or performance related websites; I'm currently working on a choreographer's site and most of her performances have multiple dates and times, similarly to what ursula mentions in #8.

I've not been able to figure out my own fix yet, but I will try to come up with a patch for this if no one else figures it out first!

dawehner’s picture

Well read http://drupal.org/node/1223448#comment-4755566 first, i think you are kind of lost here.

fleepp’s picture

works ok but it shows some nodes in each page. Once in page 1, once in page 2,...
Any ideas?

eiland’s picture

I was just checking, but cant we put the code in #6 in the view header? I tried but it seems I can't alter the $results...

leanderl’s picture

I would like to give 1 million thank yous to @drcho for this solution. I've been struggling with this for a few months! You are a hero!

couturier’s picture

Status: Active » Closed (fixed)

A much more current issue related to this is here: http://drupal.org/node/1765730

DrupalNovice’s picture

Issue summary: View changes

#6 worked for me.

Hopefully this will be fixed soon.

DrupalNovice’s picture

.

sbreese’s picture

I tweaked #6 to keep the internal index in sync with the new array. Otherwise, the last row was not displaying its content for me.

/*
* implements hook_view_pre_render()
*/
function mytheme_views_pre_render(&$view)
{
  switch($view->name)
  {
    case 'no_double_nodes_view':
      $nids = array(); // array of nodes to prevent duplicates
      $resultNoDoubleNodes = array(); // manipulated results
      $skipped = 0;
      foreach($view->result as $key => $result) // check each node of the result array on it's nid
      {
        $nid = $result->nid;
        if (!in_array($nid, $nids)) // if this node isn't a duplicate
        {
          $view->result[$key]->index = ($key - $skipped);
          $resultNoDoubleNodes[] = $view->result[$key]; // add it to the manipulated results
          $nids[$key] = $nid; // mark this nid as in results to prevent duplicates from now on
        } else {
          $skipped++;
        }
      }
      $view->result = $resultNoDoubleNodes; // replace the old results with the results without duplicates
      break;
    default:
      break;
  }
}
wooody’s picture

Thanks @sbreese ,, #23 works fine :)

s13’s picture

@drcho

Your solution worked for me. But it didn't work on xls export view.

Many Thanks anyway
:)

cirrus3d’s picture

Hi,
has anybody tried to turn on aggregation for the view? Distinct then works for me.

wjhessels’s picture

For date field with multiple values This worked for me:

In Filter Criteria, I added "Content: Date:delta (= 0)"

Edit: Sorry. It did work not for this reason:
In particular, if you are filtering by upcoming dates and the field's first date (delta 0) is in the past but subsequent dates are in the future, no row will be returned.