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 created a view with flag as relationship. This causes nodes to appear more than once. I added distinct and also the only fields I added were Flag link and Flag count. Even though I removed the two fields with out removing the relationship, the rows are repeating how many time that particular node is flagged. If I flagged a node 3 time the node is appearing thrice.
Also "Node flag counter" relationship mentioned in the below link is not available in Drupal 8
https://www.drupal.org/node/922986
Comment | File | Size | Author |
---|---|---|---|
#13 | flag-Duplicate-Flagged-Nodes-in-View-2922066-13.patch | 1.51 KB | Mike Lewis |
Comments
Comment #2
joachim CreditAttribution: joachim as a volunteer commentedHow many flags do you have? Did you filter by them?
(BTW: please don't use random tags!)
Comment #3
sumithb CreditAttribution: sumithb commentedHi joachim,
Thanks for looking into this.
I am using only one flag: Like. Added relationship with this Like flag selected.
Comment #4
joachim CreditAttribution: joachim as a volunteer commentedCan you post the config output of your view please?
Comment #5
kpv CreditAttribution: kpv at DrupalBASE commentedThe duplicates appear when "Any user" is chosen in flag relationship settings for "By" option. If "Current user" chosen, duplicates don't appear though you can't get total flags count then.
Comment #6
joachim CreditAttribution: joachim as a volunteer commentedThanks for the explanation!
In which case, this is as designed. If you join nodes to flaggings, and the flag is non-global, then you will get repeated rows because there will be more than one flagging for each node.
Comment #7
kpv CreditAttribution: kpv at DrupalBASE commentedThen we should allow to join on
flag_counts
table.Now user can use
flagging
relationship and it would work withDISTINCT
if the view wouldn't add{flagging}.id
field (which is not used) to the query.Shortly, the following case should be available: output a list of entities with total number of flags (of one type) on it. E.g. you want to make a list of popular nodes and show how many users flagged it.
Comment #8
alanburke CreditAttribution: alanburke at Annertech commented> E.g. you want to make a list of popular nodes and show how many users flagged it.
Yep - exactly the use case I'm aiming for.
Happy to try write a patch, if some advice can be given as to where to start
Comment #9
joachim CreditAttribution: joachim as a volunteer commented> Happy to try write a patch, if some advice can be given as to where to start
> Then we should allow to join on flag_counts table.
That sounds like the right approach. Though I've a feeling there is already an issue for this -- please check before uploading a patch to this one!
The steps would be:
- find where we declared Views data. Might be the hook, or might be an entity views data handler for the flagging entity
- declare flag_counts table if not already done
- iterate over all flags
- for each different entity type that has a flag on it, add a relationship to that entity type's base table that joins to flag_counts on the entity ID and entity type (the entity type needs to be in a join extra property in the views data)
- add a views relationship handler plugin, which has:
-- setting for to let you pick the flag
-- query() method which restricts the join by the flag
- add a field to the flag_counts table for the count
Comment #10
attheshow CreditAttribution: attheshow at Richland Library commentedHey, I just wanted to share a workaround I did for our site. Inside a custom module I added an implementation of hook_views_query_alter() with a group by like this:
Comment #11
droprocker CreditAttribution: droprocker commentedAny news about this issue?
Comment #12
kpv CreditAttribution: kpv at DrupalBASE commented@attheshow, thanks for sharing
Comment #13
Mike Lewis CreditAttribution: Mike Lewis commentedI took attheshow's idea and created a hook_views_query_alter implementation in the flag.module file itself that applies that group by to the base_table.base_field for the view whenever a flag relationship is present and the count is being output.
It's possible this could use some refinement, but here's the patch:
Comment #14
Mike Lewis CreditAttribution: Mike Lewis commentedComment #15
AvO WebWorks CreditAttribution: AvO WebWorks commentedPatch #13 works and fixes the issue. Thank you
Comment #16
KlemenDEV CreditAttribution: KlemenDEV as a volunteer and at Pylo commentedIs there anyone who could get this patch in next release?
Comment #17
joachim CreditAttribution: joachim as a volunteer commentedWe're testing for a relationship handler that we provide.
So we should be able to make this query alteration in the handler itself, rather than use a hook.
Comment #18
KlemenDEV CreditAttribution: KlemenDEV as a volunteer and at Pylo commentedThank you for the info. I hope this gets forward soon :)
Comment #19
ydahi+1 rtbc for patch #13 on 8.x-4.0-alpha3
Comment #20
joachim CreditAttribution: joachim as a volunteer commentedPlease see my comment in #17 -- the patch needs work.
Comment #21
ccshannon CreditAttribution: ccshannon commentedI have the same "duplicates" issue using a flag called "Follow" on a single node type, to sort by "most followed" descending. I tried the patch in #17, but it did not result in the desired outcome. The solution in #10 did, however, provide the desired outcome. Thanks, @attheshow!
Comment #22
MykolaVeryha CreditAttribution: MykolaVeryha commentedWe can use unique index. It will resolve the bug
Comment #23
cryptoline CreditAttribution: cryptoline commentedComment #24
cryptoline CreditAttribution: cryptoline commentedGreetings, the patch given above is not working. How can I solve this problem?
Comment #25
chucksimply CreditAttribution: chucksimply commentedPatch isn't working on my end anymore. Any updates?
Comment #26
chucksimply CreditAttribution: chucksimply commented@attheshow's solution on #10 does work for me as a custom module. Can we not get this as a workable patch? Seems as though #13 isn't cutting it?
Comment #27
Anonymous (not verified) CreditAttribution: Anonymous commentedAny news on this?
Comment #28
holybiberMaybe I'm missing something obvious, but I solved this problem by just enabling "Use aggregation" in the advanced settings of the view. That way I only get one row per node and still can query the flag count.
Comment #29
KlemenDEV CreditAttribution: KlemenDEV as a volunteer and at Pylo commentedThis does not work with content types with images due to a bug in views, and could also be considered a workaround, not a solution.
Comment #30
tunaman CreditAttribution: tunaman commentedI used https://www.drupal.org/project/issues/views_distinct to solve that problem (filtering by NID). You'll need to patch it to get a D9/10 version though.