Score:0

How to create a view showing Users who have flagged AND are flagged by the current user

nl flag

I'm trying to display a list of users who have flagged the current user, and are flagged by the current user. So a two-way flagging.

I've tried using different relationships and arguments in views, but can't seem to get the combination right. Any tips? Thanks!

Here's my views config from @pkros's instructions as requested.

View Overview

View Relationship

View Contextual Filter

Also, here is a quick screencast showing the issue in action... https://drive.google.com/file/d/1MOXKZSq_cjPRv6moim1a5ikKeeD8mjY9/view?usp=sharing

Score:0
mx flag

Here's how you can do this with Views:

  1. Create User Views, the username field is usually already there, you can keep it for now

  2. Add the Flag Relationship and set it to Include only flagged content, your particular Flag name, and Any user

  3. Add the Field User ID from the Flagging Category (not the User Category)

    Now you should see the list of all Flagging "connections". The username field is showing which user has been flagged, and the Flagging user field is showing who flagged them.

  4. Now add the User ID Contextual filter from the User Category, and configure it:
    When the filter value is NOT available
    Provide default value
    User ID from logged in user
    When the filter value IS available or a default is provided
    Specify validation criteria
    User ID
    Single ID
    Adjust all other options to your needs.

    This shows only the "connections" that are made towards the current user.

  5. Remove the username field since you don't need it anymore since it's always showing the current user.

What is left is the user field from Flagging context showing who flagged the current user, while at the same time that user has been flagged by the current user.

nl flag
I walked through your steps, but the list output is showing only Users who have flagged the Current User (Even if the current user didn't flag them). Wondering if there isn't another step we are missing?
mx flag
Are you sure you've set up the Relationship correctly? With "Any user"?
nl flag
Yeah, it's setup that way. If I have the Current User unflag a User who has already flagged the Current user... that User still shows in the view output above. You tested it?
mx flag
Yes, I have an example that worked, I wrote the answer based on it.
mx flag
Can you add the config in the question so I can see maybe I can spot the difference?
nl flag
Hey @pkros, I've included my views config in the question. I even tried a completely new Drupal install with your instructions. Still running into the issue mentioned above. Only showing users who have flagged the current user. Doesn't matter who the current user has flagged. Any further tips? Thanks!
mx flag
Can you include screenshots of your Views config? Raw export isn't very readable and I can't import it since it depends on your particular structure outside of Views. Reduce window and increase font if necessary when taking screenshots to make them easier to read and avoid a lot of blank space.
nl flag
Okay, screenshots added. Also included a brief video to show the current user unflagging the related user, and the output not changing. Thanks!
nl flag
@pkros Any further feedback? Appreciate your help.
mx flag
Ah it fell through the cracks, I'll try to squeeze it in this week
nl flag
@pkros, just wanted to followup, if you had any more advice. Such a frustrating issue on my end :). Thanks!
mx flag
I have been doing upgrades to D9 and still have some way to go, then I need to dig out the example so I can compare with yours. I can't find it ATM, it must be in a backup. I'll look for it tomorrow.
nl flag
Good luck on the D9 updates. Thanks again!
mx flag
I found my example, and it seems that it's not working as I expected. Maybe I have a combination of mutual following through flags that made me think it was showing correctly. Now when I think of it, it is strange that I thought it was working, there is no place where two flagging directions are compared/matched. I think it might be solved with the help of the Views Field View, because this module allows nesting queries, but I haven't tried it yet, I blew up the site trying to upgrade composer...
mx flag
You use the existing view as a base. You create a new display to be used as a Views field, build it the same as the existing views, only contextual filter may need adjustments. I think you might control the row appearing with contextual filter within the embedded field, to exclude people who don't have the flag going in the opposite direction. Or you might need to Rewrite a field in the base view to exclude them if the embedded field is empty, something of that kind. If I get to fix my site soon I'll try it out and post another answer.
Score:0
cn flag

Flags are one way, so you can't have a "two-way flagging".

In Drupal 8/9, a flag always has a target entity.

So, if user A flags user B and user B flags user A, you have two flags:

  • User A's flag of User B
  • User B's flag of User A

I can't think of a good way to do this in Views without custom code.

An alternative would be something like the Friend Flag module, which implements "friending" functionality (or Tinder mutual-swipe-to-match functionality).

Having implemented functionality like this before in Drupal 7, in Drupal 8/9, I would consider writing code in the _flagging_insert() hook that checks when a user flags another user and determines whether the flagged user has flagged the flagging user. If so, create an entity (could be a node with entity references to both users or a custom entity if you want to get fancy) of type Friend or Match or whatever, and then sort the matches that way in Views. Of course, if users can unflag each other and break the match, you have to add code to support that case, too.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.