Back to top

User Flags (Follow) in Drupal 8

Follow a user Flag in action—Drupal 8

Flag module can be used to mark users in a Drupal project. Flags can be activated globally (where the flag status is the same for everyone) or personally where each user can control their own flag preference for another user.

The most common use of user Flags is to create lists of Followers. Users can add others to their Interests list, and have Followers who are interested in what they create on the site.

This article shows how to configure a Follow flag and create Views lists of Interests and Followers to be added to user Profile pages.

Configure Follow user Flag link text

Flags can be created through the Structure > Flags page. Before you can configure anything else you first select which entity the Flag will be applied to. In this particular case we have to choose User.

How to configure User Flags (Follow) and list Followers with Views in #Drupal8 @prkosTweet this

The module allows for a lot of control over user interaction wording so you can adjust it to your special case. Only the link text is required, but you can also control the mouse tooltip and the confirmation message that is briefly animated after clicking the link.

Follow Flag link text and other wording—Drupal 8

Configure Flag Display options

A number of options is also available when choosing where to display the link and which method to use to make it work. Most users will prefer the AJAX link option that will seamlessly flag or unflag without reloading the page.

Flag link Display options—Drupal 8

Configure Flag permissions

Before your users can start using the newly created Flag you have to adjust which user Roles are allowed to use that particular Flag. For each new Flag you create on the system two new Permissions entries are created: Flag and Unflag:

Flag module Permissions per Roles—Drupal 8

This configuration shows a simple link on a user Profile page. The following screenshot is illustrating what the tooltip might look like and the animated message that is briefly displayed next to the link seconds after it has been clicked:

Follow a user Flag in action—Drupal 8

Many projects that use Flags require lists of flagged and entities and lists of users who flagged them. When it comes to the Follow Flag we can call those lists Interests (to list users we follow and their content) and Followers (to list other users who follow us).

Views module can be used to create those lists, and make them display on user Profile pages.

List Followers of a user

The easiest Flag-related View Display to create is a list of users who follow a user, and show that list in a separate Drupal tab on that user's profile page.

Since we want to create a list of users we have to create the User type of View. We create the Page Display, set the Path to user/%user/followers, and Menu to Menu tab.

Flag Followers Views configuration—Drupal 8

The User ID Contextual filter from the User Category (not the Flagging Category) should be configured with these options:

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
Override title
{{ arguments.uid }}'s Followers (check the Replacement patterns Fieldset a bit further down the settings)
Specify validation criteria
User ID
Validate user has access to the User

You can leave the rest as default or adjust to your specific needs.

We didn't need any Flags related information so far, because we've only oriented the Views to be sensitive to the user who's page we're viewing. We now want to show who that user's Followers are, that is users who used the Follow link on this user's page.

To do that we add the Relationship User flag from the Flag Category and configure it:

Include only flagged content
Follow
Any user

Now that we have Follow information in the Views we can use it to show some relevant Fields in the Views Results. The User: Name Field is already there but we don't need it since we know what user's page we're viewing, so you can remove it.

Then add the Fields Created and User ID from the Flagging Category. They will both use the Relationship we added previously. You can change the Labels to something like Date added and Follower so you know they're related to Follow Flag.

Don't forget to set the No results behavior to something like This user has no followers..

And here is what the result might look like:

Followers Flag information on user Profile page—Drupal 8

List Interests of a user

To create the reverse where we list who the user follows we Duplicate the above Page and make some small adjustments.

Flag Interests Views configuration—Drupal 8

The obvious changes need to be made to the Path and Menu options.

The Relationship stays the same because it's the one that introduces Flag information into Views and we already have it configured to the correct Flag and to include all users.

It is important to completely change the Contextual Filter. We have to remove the existing User: User ID one, and add the User ID Contextual filter from the Flagging Category. It will use the Flags Relationship by default, and we can configure it similarly to the one above.

To clean up the Fields we can remove the Follower field because it will only show the same person, the owner of the Profile page we're viewing. We can keep the Created Field as it gives useful information about when an Interest was added.

But to show who the use id following we need to bring back the User: Name Field.

Adjust the No results behavior to something like This user is not following any others..

Interests Flag information on user Profile page—Drupal 8

List content from users that are Followed

To add a table listing latest content from people someone follows is just a matter of Duplicating the Interests Page as an Attachment, adding a new Relationship just to that Display (This attachment (override)) Content authored. The Relationship can be required since we only want users in this table that have created some content.

We also want a new Field Title from the Content Category, also overriden for this Attachment only. It will automatically use the previously created nodes Relationship.

When we add the Attachment to display on the Interest page we see something like this:

Interests Flag information with Content on user Profile page—Drupal 8

How to configure User Flags (Follow) and list Followers with Views in #Drupal8 @prkosTweet this

There are other adjustments that can improve the UX of these Views, but they fall under the usual Views features and best practices. This article should be enough to guide you through building the majority of user Flags features on your Drupal project.

Would you like me to help you with setting this up? Or with any other Drupal problem? Contact me and I'll summon all my skills and Drupal might to solve it for you.