How to setup a "Who's bookmarked this" tab
Sometimes it is desired to add a tab to our node which will list all the users who bookmarked it (Or "favorited" it, or "hated" it, or whatever your flag stands for). Here's a recipe explaining how to do this using Views:
- Start a new view.
- As usual, pick a name, and possibly a description, for your view.
- Tick the Provide Page View checkbox.
- Our tab needs a URL, so let's type
node/$node-story/whointo the URL box. This weird URL is similar to the more conventionalnode/$arg/who, except that it will appear only on nodes of type story. Change 'story' to match the type of nodes you allow your users to flag. - Tick the Provide Menu checkbox.
- Tick the Provide Menu as Tab checkbox.
- Type a short label to appear on the tab, in the Menu Title box. You may want to type a longer title, to appear on the view page itself, into the Title box.
- Add the Node: ID argument.
- Add the Flag: User for xyz field. The 'xyz' you'll replace with the name of the flag. In our case, for example, it might be 'bookmarks'.
- Optionaly add the Flag: xyz Flagged Time field, if you want to see the time the users flagged the node. The 'xyz' you'll replace as in the previous step.
- Add the Flag: xyz filter. The 'xyz' you'll replace as in the previous step. For the value dropdown select Any User.
(Technical Note: adding this filter is necessary for one reason only: the User field is LEFT JOINED --as is the Flagged Time field-- and the filter is neccessary to get rid of the bogus Anonymous user we will see when no user has flagged the node. This isn't a bug in this module but an implication of the way Views works.) - For the type of the view, select either Table or List. This is important because else the list will show the node itself --instead of only the User field we've just added.
- You may want to type "No user has bookmarked this page yet." into the Empty Text box.
You're done.
Sorting the list of users
There are two possibilities to sorting the list of users: either by their names or by the time they flagged the node. These sorts can be hardcoded, when picked from the 'Sort Criteria' box; or you may give the user viewing the page the freedom to decide the sort, by choosing the Table style and making the fields click-sortables.
And what about a block?
You now know how to setup a "Who's bookmarked ..." tab. But what if you want a block, not a tab?
It's easy to make this view provide a block too:
- Tick the 'Provide Block' checkbox.
- For the block's 'View Type', choose 'Table View' or 'List View'.
- In 'Nodes per Block' type anything to your liking, e.g. '99' (if you're showing a much shorter list of users, it's reasonable to sort the list by the date the users flagged the node, from newest to oldest).
- Add the Node: ID argument.
- Into the 'Argument Handling Code' box type:
if (arg(0) == 'node' && is_numeric(arg(1))) { $args[0] = arg(1); }(It doesn't matter whether or not you're using "pathauto".
- Go to 'Administer >> Site building >> Blocks' and enable this block. If you have troubles finding it in the list, then its name is as the name of the view.
- The block will appear only on nodes that were flagged. You're supposed to like this feature. If you want the block to appear on every node, type some message into the block's 'Empty Text' box; e.g., "No user has bookmarked this page yet."
- We've just said that the block will appear only on nodes that were flagged. However, Views works behind-the-scene nevertheless, and you may wish to prevent this --to ease on the system's resources-- by instructing Drupal to show the block only on certain nodes or pages. For example, if you want the block to show only on 'story' nodes, you may use Drupal's PHP block visibility feature by using this code:
<?php // This expression returns 'true' if we're viewing a 'story' node. return arg(0) == 'node' and $node = node_load(arg(1)) and $node->type == 'story'; ?>
But why is this so complicated?!
If you're asking yourself, "but why do I have to follow these comlicated steps?! can't thing be simpler?!!111", then you have a good point here. Things will be much, much simpler in the Drupal 6 port of this module thanks to Views 2.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion