Last updated October 15, 2010. Created by mooffie on September 18, 2008.
Edited by NonProfit. Log in to edit this page.

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.

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.

Counters

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:

  1. The user who authored the content.
  2. 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.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

I am trying to do as in this example, "filter the content to only that which was flagged by a certain user". If I just add a "Flags: Node flag" relationship, I can filter to only list nodes flagged by Current user. However, that is not what I want as I want to list nodes flagged by a specific user, user "Adam".

So, I thought what is explained above matched what I wanted and was happy to find an explanation. However, when trying to follow this instruction, I can not manage to add a "Flags: User" relationship, since there is no such option in the list of potential relationships. The only available options are "Flags: Node flag", "Flags: Node flag counter", "Flags: User flag", "Flags: User flag counter", and "Flags: User's flagged content". Both the above instruction and some other threads I have found talks about a "Flags: User" relationship, but I can not find it.

Any advice?

Updated by myself: One first has to create a relationship to the specific flag (Flag: Node), then one can create a relationship to Flag: User using the flag relationship.

I don´t get how the global flag works here.

There are two kinds of users in our system:
1. The user who authored the content.
2. The user who flagged this content.

I have a user flag that´s global. So when the admin flag a certain user, that person stays flagged.
I need a tab in each profile that should show all users flagged with the same flag that the user have been flagged.

So in the list:
1. The user who authored the content.
2. The user who flagged this content.
Shouldn´t we have also:
3. the user who has been flagged.

I don´t understand how to create a view like that.

Rosamunda
Buenos Aires | Argentina

I love the way this documentation reads. It's neither too technical for the non-technical nor condescending to the experienced developer. Serves to address multiple audiences and uses good grammar to boot! Kudos.

--
..happiness is point and click..
http://www.bronius.com

Can different flags, flag the same node?

Yes.

When I have a flag relationship that is configured to "By: Any user", the flagged time field is - the time of the most recent flag by any user on a node - right?