20 Steps to Views Happiness

First things first. A View is a LIST! List, list, list. A List of stuff on your website. You want Views because we're not all happy with the way Drupal builds lists out-of-the-box, and we want to customize our lists.

Install the module from http://drupal.org/project/views. It has an install file, so it works really nicely in 4.7. Upload it to your modules directory, then browse to the modules section in admin and enable the Views module and the views_ui module. That's it.

Enable the module for the users you want to be able to create views using the admin -> access control menu.

Go to the Views menu item under Settings. Looks intimidating, right? Not really.

There are many things you can do with Views that go far beyond this configuration interface, but we'll leave that for now. If you are ambitious later, go look at the snippets section mentioned above. You (yes, YOU) can import some of these canned Views using the import tab.

So, a simple exercise. 20 steps to views happiness. Let's create a view (list, remember?) that is displayed in a block. To the right you can see a block that says "New Forum Topics" and lists titles. Let's build a View that creates a list sort of like that.

  1. Click on the "Add" link.
  2. Give it a name "LatestFrontPageItems" [spaces not allowed]
  3. Don't check anything in the access section because we want everyone to be able to see our cool block.
  4. Give it a description for the Blocks admin page: "Latest Front Page Items"
  5. Next is the Page section. We're not making a page (but Views pages are cool too!) we're making a block, so just leave this section alone.
  6. Now go to the Block section and check the "Provide Block" box because we want Views to provide us with a Block for our View.
  7. Give it a title: "Cool Block, eh?" (I'm a Canuck)
  8. Let's show five items in our Block, so input "5" in the "nodes per block" section. A node is an item like a story or a blog entry. We want a list of item titles (not a table or a list of teasers or a big list of full items) so leave the default "List Items" the way it is. You can play with this later to see what it does.
  9. If you wanted your block to be a preview of a page of the same View, you would check the "More" link. We chose not to make a Page for this view, so we won't do that. You can have both Page and Block Views of the same View.
  10. OK, let's take a little break here. What's a View again?

    Here's another take on it: a View is just a way of looking at all your stuff on your website. With real world objects you can have several views. You can look from above, from below, from the side, etc. You can look at it through filters that show only certain spectrums of light. You can look really close, or from far away.

    Viewing the content of your website using Views Module is the same thing. How do you want to see your content? In a list that has node titles, then dates, then times published? Do you want to see it as a table or a standard list of items? Do you want to sort alphabetically, from the latest, or from the earliest? All these ways of viewing your stuff can be a single "View". So we're building a block that will give us a particular view of our content on our website.

  11. So, we're in the block section of the Views administration, and we just chose not to provide a "More" link because we're not generating a page for this View. Now we click on "Header". Do you want to put some stuff at the top of the block? Use the Header area. Check "Use Page Header" and Type in the box "This is is my really brilliant introductory paragraph. Hope you like it." The text box can take any kind of input, including HTML and probably images, though I haven't tried that yet. Now THAT is amazing.
  12. Close the Header section and click on Footer. Same story here. Let's put in a footer. Check the "Use Footer" box and type in the text box "Thanks for looking at my Cool Block. It's my first one."
  13. Close Footer and click on "Empty Text". This is here in case the View doesn't generate any results. It could say something like "This View sucks" or "Come back later for a better View" but we're going to make sure there is some content in our View by picking a big section of content, so don't bother with that for now. Just remember it's there in case you need it.
  14. Close Empty Text and now we're at "Fields". We need some fields here or our block will be empty and we'll wish we had put something in empty text. (Oh, by the way, you can save your View and go back in to edit it some more now if you are worried about your six month old daughter hitting the keyboard). Fields are just the parts of your content you want to display in your View. So the default one that is staring you in the face is a good place to start. "Node: Title" is just the Title Field of your content items. The title. Just the title. Node is a fancy word for a piece of content. So we want to add this to our View because want our list to contain the titles of our content pieces. Click "Add Field". Now you have a field for your view. Look at all the extra stuff you can configure to the right of the field name. When you get better you can play around with the options on these, but for now, let's keep it simple and go with one field with default configuration.
  15. Close Fields and skip Arguments (try this later) and go to "Filter". So far we have told Views we want a list of Nodes with Titles in it, but we don't want a list of ALL the nodes on our site. We want something more useful. Let's make a list of all nodes that have been published to the front page. That way, no matter where people are on our site, they can see the list of front page nodes. So, click on the list of Filters and choose "Node: Front Page" and "Add Filter".
  16. Skip "Exposed Filters" and click on "Sort Criteria". We want our nodes to be listed in our block with the most recent node first. So, select "Node: Created Time". Now, when you add it, it comes back with an option to sort by ascending or descending. We want latest items at the top, so descending.
  17. Save your View!
  18. Now, go to Blocks and you'll see your Views Block listed in the inactive blocks. Enable it and put it where you want. In Drupal 4.7 you can put it in lots of places, but for this excersise, put it in the right column.
  19. Save your blocks configuration and you new Views Block will show up on the right. Notice the list has titles of items published to front page, listed with the newest on top. Amazing, eh?
  20. Now, go back to Views and start playing around with all the different setting for your block you just created. Change the filters, add arguments, add Fields, take them away again. Each time you save your changes, the block you created on the right hand side will refect the changes. This is a great way to get started with Views and learn the beginnings of what is possible. Think about what you can do with pages! Oh, and in pages you can set the path, so http://example.com/my/cool/views/page and you can even put a link to your cool views page in the menu, right from the views configuration interface. That is so amazing. That's Drupal 4.7.
  21. Go have a beer, because you are now well on your way to understanding Views and you need a bit of time to reflect on all the possibilites. It is a lot more powerful than this simple excercise, but its now up to you to read the documentation at http://drupal.org/node/42592 and learn more.

Have fun. You now have much more power at your fingertips.

Cross-posted at http://blurt.info

 
 

Drupal is a registered trademark of Dries Buytaert.