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.
Start by adding a new view, giving it a suitable name such as “randoms”. Then:
- Give your block a Title
- Change the “Items to display” setting to 5.
- 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.
- On the dialog for “Node: Published”, choose the option “Yes”.
- On the dialog for “Node: Type”, check the box for the “Story” type.
- 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.
- Add a field for “Node: Title”.
- Clear the Label box as you won’t want a label to be shown.
- Check the option that says “Link this field to its node”.
- Now you have defined what each row consists of, the next thing to do is to output the result as a list.
- 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”.
- 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”.
- 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”.
- 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.