Using views to show users who flagged nodes... by node.

Flags is a very useful module that allows users to "flag" nodes that meet some criteria (eg type etc). Administratively, it might be of interest to show the users who have flagged a specific node. Unfortunately, that's both tricky and slightly non-intuitive.

 I assume you have set a flag that allows users to flag nodes, along with options (such as which roles, what types, etc) and that your users can now flag the nodes you want. A simple example I recently had was for "premium" users to opt in some closed beta of a site feature. I obviously would like to know the people who showed interest, and the first step would be to get them on a list.

Start by creating a new Node view (not user, apparently flags doesn't play nice with that in our case). Set the simple stuff such as pages, number of items  etc, and let's get to the interesting part.

  1. In relationships, add from the flag group "Flags: User's flagged content"
    • Check "Include only users who have flagged content."
    • Select the flag which is of interest
  2. In sorting, you can add "Flags: Flagged time" (desc) to get a chronological list
  3. Most importantly, in the arguments section, add "Flags: Content ID"
    • You can use the Title field for specific title when viewing a node, in my case "Beta interest for %1". Note that this will be replaced by the actual title, not the nid.
    • Choose "Summary, sorted descending"
  4. Choose any fields that you want, and the layout you'd like ( I prefer a table with uid, name, e-mail ).
  5. Create a Page display, and set a path.

As it is at the moment, when you go at the path defined, you'll see a list of node ids (with the number of users that flagged it in parentheses). That's not exactly user friendly, but I couldn't find a simple straight-forward solution, so some small hacking was required. That is, use one of the least documented features of views, and that's summary templating.

Copy over the file sites/all/modules/views/theme/views-view-summary.tpl.php to your template folder, and edit it. The only thing you need is to add the following line after line 11 ( after the foreach loop  ) :

<?php $row->link = dbquery(db_result("SELECT title FROM {node} 
  WHERE nid=%d",$row->link)); ?>

And you're all set!

Image from here