Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
screenshot2.png | 15.4 KB | drcho | |
screenshot1.png | 24.36 KB | drcho |
Comments
Comment #1
dawehnerSure, 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.
Comment #2
drcho CreditAttribution: drcho commentedThank 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?
Comment #3
drcho CreditAttribution: drcho commentedComment #4
dawehnerYou 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.
Comment #5
drcho CreditAttribution: drcho commentedThank 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.
Comment #6
drcho CreditAttribution: drcho commentedHere is my solution if anybody else needs the solution:
(1) Add a function to your template functions in mytheme/template.php:
(Sorry for the non drupal conform php formatting.)
(2) After that clear all caches:
and it should display each node only once.
However, that would be a nice feature in the views ui :-)
Comment #7
mpotter CreditAttribution: mpotter commenteddrcho: 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.
Comment #8
ursula CreditAttribution: ursula commented@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.
Comment #9
dawehnerSo this issue seems to be fixed.
Comment #10
hansrossel CreditAttribution: hansrossel commentedThe code in #6 works but unfortunately does not recounts the pager.
Comment #11
drcho CreditAttribution: drcho commentedMy 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.
Comment #12
dawehner@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.
Comment #13
drcho CreditAttribution: drcho commented@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.
Comment #15
jackalope CreditAttribution: jackalope commentedDon'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!
Comment #16
dawehnerWell read http://drupal.org/node/1223448#comment-4755566 first, i think you are kind of lost here.
Comment #17
fleepp CreditAttribution: fleepp commentedworks ok but it shows some nodes in each page. Once in page 1, once in page 2,...
Any ideas?
Comment #18
eiland CreditAttribution: eiland commentedI 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...
Comment #19
leanderl CreditAttribution: leanderl commentedI 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!
Comment #20
couturier CreditAttribution: couturier commentedA much more current issue related to this is here: http://drupal.org/node/1765730
Comment #21
DrupalNovice CreditAttribution: DrupalNovice commented#6 worked for me.
Hopefully this will be fixed soon.
Comment #22
DrupalNovice CreditAttribution: DrupalNovice commented.
Comment #23
sbreese CreditAttribution: sbreese as a volunteer commentedI tweaked #6 to keep the internal index in sync with the new array. Otherwise, the last row was not displaying its content for me.
Comment #24
wooody CreditAttribution: wooody commentedThanks @sbreese ,, #23 works fine :)
Comment #25
s13 CreditAttribution: s13 as a volunteer and commented@drcho
Your solution worked for me. But it didn't work on xls export view.
Many Thanks anyway
:)
Comment #26
cirrus3d CreditAttribution: cirrus3d commentedHi,
has anybody tried to turn on aggregation for the view? Distinct then works for me.
Comment #27
wjhessels CreditAttribution: wjhessels commentedFor date field with multiple values This worked for me:
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.