Things to do with Drupal Views #1: a Random Stories block

Technology Blog

The Drupal Views module is a terrific tool, if somewhat challenging to get to grips with. In this series of posts I’ll be providing instructions with accompanying video footage, showing how to accomplish some useful tasks with this module.

In this first post, I show how to make a block with a list of 5 stories chosen at random.

Video Demo

CLICK HERE TO START VIDEO

Detailed instructions

Start by adding a new view, giving it a suitable name such as “randoms”. Then:

  1. Give your block a Title
  2. Change the “Items to display” setting to 5.
  3. Add two filtersĀ  for “Node: Published” and “Node: Type” and press “Add”. These filters will let you restrict the nodes shown to stories that are published.
  4. On the dialog for “Node: Published”, choose the option “Yes”.
  5. On the dialog for “Node: Type”, check the box for the “Story” type.
  6. Now in the Live Preview, you’ll see a message like this: ‘Display “Defaults” uses fields but there are none defined for it or all are excluded.’. This is because you haven’t yet said how you want your view to display the results, so that’s what we’ll deal with next.
  7. Add a field for “Node: Title”.
  8. Clear the Label box as you won’t want a label to be shown.
  9. Check the option that says “Link this field to its node”.
  10. Now you have defined what each row consists of, the next thing to do is to output the result as a list.
  11. Next to “Style” under “Basic Settings”, you’ll see the word “Unformatted”. Click on that and a dialog will appear with a number of choices of output styles. Select “HTML List”, then on the next step choose “Unordered List”.
  12. Next you need to order the result randomly. Click on the plus sign next to “Sort criteria” and from the list that appears, choose “Global: Random”.
  13. Finally, you need to create a block. Under the word “Defaults” on the left you’ll see a pulldown selector. Choose “Block” and click on “Add Display”.
  14. Save the result and you now have a block which shows the titles of 5 stories entirely at random.

Coming up …

In future postings I’ll be showing how to customise your site’s RSS feed, how to add lists of user contributions to their profiles and more.

Figure W do all kinds of Drupal development and consultancy for clients large and small. Call us on +447783 386951 or use our contact form for a chat about your requirements.

Share this post:
  • Digg
  • Technorati
  • del.icio.us
  • StumbleUpon
  • Facebook
  • Sphinn
  • TwitThis

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>