Drupal Recipe: Create an Image Who’s Online Block


whos_online_finalRecently in Drupal 6, I needed to create a way to display images showing the currently logged in users for a project we are working on. Essentially, this is a Who’s Online block but rather than displaying usernames, we want to show user profile pictures. Additionally, we extended this with the Gravatar module. Adding this module allowed us to have user profile pictures for users even if they did not upload one if their email address had a gravatar associated with it.

This is an easy Drupal recipe and only takes a few minutes to create.

Ingredients

  • Drupal 6
  • Views 2
  • Gravatar Module (optional)

Directions

whos_online_user_settingsTake your Drupal 6 site and ensure that Views 2 is installed. Next, go to Administer –> User Management –> User Settings. Scroll down to the “Pictures” section and be sure that user pictures are enabled. We also set the size to 48×48 as it allowed us to use a standard size and get a 4 across grid for our new block.

Once you have made it possible for users to have profile pictures, now we need to build our view. So, without further ado, please proceed to Administer –> Site Building –>Views. Click on “Add” to add your new view. Name it “Online Users” and change the type from “Node” to “User“. This is important since the source of our view is the user object, not nodes. Click next.

Now we are at the view display definition screen. Since we want to know who is currently online, we need to decide what exactly constituents a user “being online”. In our case, we chose to show any users that have accessed the site in the last 20 minutes. Thanks to Drupal and Views 2 we have access to this information.

whos_online_user_filterFirst, let’s add our filter. Click the plus sign next to the Filter area and choose “User: Last Access” as your filter. After adding this filter we need to set the filter settings.

Choose the “is greater than or equal to” operator. For the Value type, choose the “an offset form the current time” option. Finally, in the value textbox enter “-20 minutes“. What this does is lists all users where the date and time of their last access is greater than or equal to 20 minutes ago.Now update this display and save your view.

Now, we need a field to display in our block. So, go ahead and add the User: Picture field to the view. This will display the users profile picture, or default picture, in our block.

whos_online_style_optionsLastly, we want to set the display style to a grid. Under the Basic Settings section click the style link. Change the display style to grid from unformatted. In the grid options chose a horizontal alignment with 4 columns.

whos_online_add_blockAgain update and save your view. Lastly, we have been working on the default display, but we really need a block. So add a block by choosing “Block” from the display dropdown and click the “Add Display” button. When you have completed all these steps, you will see this block in your Administer –> Site Building –> Blocks. Add it to a content zone and you are all set. Try it out.

Now, as described at the beginning of this article, we also installed the gravatar module. Nothing had to be changed with the view we created here since the gravatar module returns its image as the User: Picture as well.

Popularity: 15% [?]

About Tom

Christ follower, husband, father, technology and photography enthusiast. Attempting to live life out as a light in this world and stumbling at times in this fallen world. Got a topic you want to have me look into? Did I miss something in a post? Let me know. Just add a comment below.