Community Documentation

DUPLICATE: How to setup a "Who's bookmarked this" tab

Last updated April 5, 2011. Created by mooffie on January 28, 2008.
Edited by lisarex. Log in to edit this page.

[THIS LOOKS LIKE A DUPLICATE of http://drupal.org/node/296959]

Sometimes it is desired to add a tab to our node which will list all the users who bookmarked it. Here's a recipe explaining how to do this using Views:

[NOTE: You should be using a CVS version recent than 2008-01-26.]

  1. Start a new view.
  2. As usual, pick a name, and possibly a description, for your view.
  3. Tick the Provide Page View checkbox.
  4. Our tab needs a URL, so let's type node/$node-story/who into the URL box. This weird URL is similar to the more conventional node/$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 bookmark.
  5. Tick the Provide Menu checkbox.
  6. Tick the Provide Menu as Tab checkbox.
  7. 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.
  8. Add the Node: ID argument.
  9. Add the Bookmark: User for xyz field. The 'xyz' you'll replace with the name of the mark. Usually it is 'bookmark'.
  10. Optionaly add the Bookmark: xyz Marked Time field, if you want to see the time the users bookmarked the node. The 'xyz' you'll replace as in the previous step.
  11. Add the Bookmark: 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 Marked Time field-- and the filter is neccessary to get rid of the bogus Anonymous user we will see when no user has bookmarked the node. This isn't a bug in ViewsBookmarks but an implication of the way Views works.)
  12. 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.
  13. 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 bookmarked 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:

  1. Tick the 'Provide Block' checkbox.
  2. For the block's 'View Type', choose 'Table View' or 'List View'.
  3. 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 'Date Bookmarked', descending).
  4. 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".
  5. 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.
  6. The block will appear only on nodes that were bookmarked. 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."
  7. We've just said that the block will appear only on nodes that were bookmarked. 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.

Page status

About this page

Drupal version
Drupal 5.x

Archive

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here