0

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

2 Answers 2

0

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.

0

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.

15
  • 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?
    – Yuckle
    Jul 9, 2021 at 20:35
  • Are you sure you've set up the Relationship correctly? With "Any user"?
    – prkos
    Jul 9, 2021 at 23:58
  • 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?
    – Yuckle
    Jul 12, 2021 at 22:15
  • Yes, I have an example that worked, I wrote the answer based on it.
    – prkos
    Jul 12, 2021 at 23:09
  • Can you add the config in the question so I can see maybe I can spot the difference?
    – prkos
    Jul 12, 2021 at 23:10

Your Answer

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

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