This is a very simple tutorial on how to render a list of users who have flagged a page using the Flag module and Views.
This tutorial will show you how to add a "Like this page" button to a page and display which users had previously like the page.
The Flag module is an extremely flexible module which allows site administrators to place flags on any content in the form of a link. Flags are split in to two categories: Per-user and Global.
- Per-user: Each user can flag content individually.
- Global: The flag is shared across all users; if one user flags the content, it's flagged for everyone.
The module has a number of useful featured that really make it a must have module for any social website. For now we're going to focus on it's integration with Views.
Let's get into it:
First of all, download and install the Flag module.
To create a flag go to admin/structure/flags and click the + Add a new flag link. Choose a name, select Nodes as the Flag type and click Submit.
Finish creating the flag by by configuring a few extra options; the majority are self explanatory, a few notes:
- Make sure that the “Global flag” field is un-checked.
- Select the content types you wish to flag in the Flaggable content field.
- Make sure Display link on node page is checked.
It's that easy to create a flag and add it to your content. Make sure you flag some content with a few users so that the effects of the next part are visible.
Head over to views (admin/structure/views) and add a new view that has the type User rather than content (we are going to be displaying which users have flagged this content after all) and create a display block so that we can easily place it on the page with the content.
First of all add a relationship to the View - Flags: User's flagged content and choose your flag on the configuration page. Doing this will show all users who have used this flag on any page. Obviously, this is not ideal.
To Further filter the users add a contextual filter – Flags: Content ID. Select the relationship that we just set up. In the “When the filter value is not available” section, select Provide default value and chose Content ID from URL from the type.
These settings mean that the users are being filtered so that it displays all users who have flagged the content on the current page. Save the view.
Now simply add the block the relevant pages.
photo credit: ciro@tokyo via photopin cc