Voting starts in March for the Drupal Association Board election.
When a user flags an item with the Flag Module, a record --or "information"-- is written somewhere in the database. To be able to do anything related to flags, we first need to "pull in" this information.
The way to pull-in information in Views is by using relationships.
You have to add the flag as you need on "admin/structure/flags/add" which will get listed in the views relationship.
Let's start a new view, of type "Node". Click the "+" in the Relationships box. You'll then notice that the Flag module provides several relationships. The most important ones are:
- Node flag
- User flag
- Comment flag
Each relationship is used to pull-in a flag of the certain type. Technically savvy people may find things easier to grasp if we told them that "a flag" is actually the aforementioned records.
Let's add a "Node flag" relationship. In the next screen we will be asked to choose the exact flag we're interested in (e.g. "bookmarks", "spam", "been here").
Pay attention to the "Include only flagged content" checkbox. Sometimes, and quite frequently, this feature is the only functionality we're after.
Once a flag relationship is added, we'll have access to two new fields. Click the "+" in the Fields box. You'll then find:
- Flag link
- Flagged time
The flag link field is used to display a "flag this!", or "unflag this!" link. Naturally, if you ticked the "Include only flagged content" checkbox mentioned earlier, you'll initially see only "unflag this!" links, because all the items are flagged already.
Whenever you add fields/filters/whatever to the view, make sure to click the "Update" button on the following form, or you'll get some nasty error messages like this one.
This is because of a bug in Views, which was fixed on 2008-09-26; so if your Views is of this date or newer, you can disregard this warning.
Whenever an item is flagged, or unflagged, a counter field is updated in the database. This field records the number of times this item is currently flagged --by all users. It records the "popularity", if you wish, of this item. To access this field, you must first add the corresponding relationship to the view. The process is identical to the described above, but the name of the counter relationships have "counter" appended to them:
- Node flag counter
- User flag counter
- Comment flag counter
Once you add a counter relationship, you'll be able to show the counter field, filter by it, and sort by it.
Tip: Optionally, untick the "Include only flagged content" checkbox to include in your view also nodes that have not been flagged (allowing you to display "Total flagged: 0").
Now, you may ask: Why is this extra relationship necessary? I don't need a special relationship when I want to show the 'flagged time' field!
That's a good question. The only reason for the existence of the counter relationships is technical: This field serves yet another purpose: it's the only means to answer a "show me all flagged items, by any user" query, without causing duplicate rows, and very efficiently. See explanation here.
The flagging user
There are two kinds of users in our system:
- The user who authored the content.
- The user who flagged this content.
Usually, any mention of "User" in Views' User Interface relates to the first kind: the user who authored the content.
But sometimes we want to see, or operate on, the second kind: the user who flagged the content. For example, we may want to filter the content to only that which was flagged by a certain user. Or we may simply want to print the name of this flagging user alongside of the content.
To have access to the user who flagged the content, we must first add the "Flags: User" relationship to the view.
Once we do this we will notice that many fields/filters/arguments now have a "Relationship:" dropdown. This dropdown tells Views to which of the two users we want to relate.