1

I have a user flag that is called "follow" and am trying to setup the following views in Drupal 8.

I want to create 3 views for my website's user profile pages:

  1. A list of users flagged by a user (for example, a "people I'm following" block)
  2. A list of users flagging a user (for example, a "my fans" or "my followers" block)
  3. A list of nodes from all the users that have been flagged by a user (for example, a news feed for nodes created from the users a user flagged)

This has been done before for older versions of Drupal (see View of users flagged by a user, users flagging a user, and nodes from users flagged by a user), but I can't get it to work on Drupal 8 because many of the fields are different.

Even after playing around with different values, I can only get to the point where I display the users the currently logged-in user is following, but that is not what I need.

How do I get a list of users flagged from a user account when I am watching its profile?

  • 3
    I can't get it to work on Drupal 8 is quite broad. You need to describe exactly what you get, and how that is different from what you expect, or what you expect reading the answers for the question you linked. Without that information, users could simply repeat what already said in those answers or say things you already know. – kiamlaluno Sep 4 '18 at 19:39
  • The field names on D8 are not the same as D7. When I tired to add Flag:User it doesn't exist on D8 or there is a different way for it. I did play around with the suggestions for D7 but I could not get those views setup. Thanks for replying. – Progonat Sep 4 '18 at 19:51
4

Whether you're creating content or user Views, you have to add the Flags Relationship (User flag in your case). This is the first step to introducing the flags information into the Views, and in most cases you'll want to set that Relationship to Any user, and use Contextual filters to restrict the results to your needs.

List users flagged by a user

This answers your first question, a page you can add to user profile pages (as a Drupal tab for example).

  1. Create user Views, Page Display
  2. Add the User flag Relationship and configure it:
    Include only flagged content
    Follow
    Any user
  3. Add the Contextual filter User ID from the Flagging category (not the User Category!) and configure it:
    It will use the Flags Relationship by default
    When the filter value is NOT in the URL
    Show "Page not found"
    When the filter value IS in the URL or a default is provided
    Specify validation criteria
    User ID
  4. Change the Path to user/%user/flags
  5. Add a Menu tab if you wish to show on user profiles
  6. Add No results behaviour This user is not following any others.
  7. Add a Field Created from the Flagging Category to show when the flagging occured.

When you visit a person's page you'll see a list of users they follow.

List followers of a user

  1. Duplicate the Page from above
  2. Override the Contextual filters, remove the flagging one, and add User ID Contextual from the User Category and configure like the one above
  3. Change Path to user/%user/followers and Menu tabs
  4. Adjust the Title, No results text etc.
  5. Override Fields, remove the Name (it will always show the user who's page you're viewing so it's redundant)
  6. Add a Field User ID from Flagging Category (Label it Who flagged)

List content from users that are Followed

  1. Duplicate the first Page from above (List users flagged by a user)
  2. Add a new Relationship Content authored from the User Category, require it so you don't see people in the results that haven't authored any content
  3. Edit the Path to user/%user/flags/content or whatever makes sense to you, add the Menu tab
  4. Override the Fields and add the Title Field from Content Category
  5. Remove the Field showing the date when Flag was created
  6. You can Group by user Name under Format, to group Content by authors if you like

You can create Blocks instead of Page using similar logic, just configure the Contextual filters to react to use a default value from context, when it's not in URL.

  • prkos you are a God sent. I can't thank you enough. Everything worked flawlessly. Even added a few cutom fields to fit our needs. I have one question though. How do I exclude certain user roles from views? For example under followers, it shows visitors. I also would like to remove adminstrators from following regular users. – Progonat Sep 5 '18 at 15:33
  • 1
    I'm glad to help :) If it's just about the Views you can add a regular Filter Roles that a user belongs to. But if it's about limiting who can use a Flag then you can adjust that under People > Permissions, there's an entry for each Flag you create. – prkos Sep 5 '18 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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