Using Views in Drupal 6
Drupal 6 has been out for a while now and modules are getting ported to the new version. For those of us that are relatively new to Drupal it has been a long slow wait – but the benefit is that we have a bit of extra motivation to learn and do “stuff” for ourselves.
Interesting Modules for Working with Views in Drupal 6
If you search for cool modules and functions on the Drupal site one thing you begin to see is the importance of modules that might not necessarily supply a prebuilt function, like a calendar for example, but provide the ability to define new types, views, or displays of data from either your Drupal site or a different source. The modules I’ve been exploring are:
- Feed Aggregator
Feed Aggreagtor is supplied in D6 and is pretty self explanatory. The remaining 3 are very powerful. I checked out the really cool myplay.com site Sony-BMG put together. If you haven’t seen it you should check it out. They made extensive use of Views and Panels to build this site. Views is in beta now for D6 and since I also wanted a calendar, I decided to start experimenting with Views. Ok – to explain that comment: the best Calendar function for D6 uses Views for the rendering of the event data. Because Views are still in beta I had to do some tweaking.
Drupal 6 Views Explained
There are a bunch of overviews and tutorials for the original Views modules on drupal.org – so start there if you want an overview. What you’ll find however is that once you get past the “what is a view?” type stuff they aren’t much help. In short a View is a list of “stuff”. The list is defined in the Views administration area through a GUI interface. The list is generated by defining a query against your database – you add attributes to extract; arguments to select what to return; sorting criteria, etc. You can then select how to display the list: a node, a block, as a feed, etc. Pretty cool I think. How do you create one? That’s what I’ll cover.
To get started I’m working with the very latest Drupal code (6.2 at time of writing). I have a pretty basic config – but its completely to do date as of 3-May-2008. I’ve installed Views (6.x-2.0-beta2) and I’ve also installed the Advanced Help module (advanced_help-6.x-1.0-beta2).
To start with Views make sure you have the very latest by checking your adminstration area. Once you’ve done that then go into the page for administrating the Views module. You’ll see a few tabs for Adding and Importing views. Click on the Add choice and you should get a screen like this:
This is the main spot where you create your view. Go ahead and bang in some values and make sure you pick off the “node” choice. What we’re going to do is create a simple “node” type view that will look like a page. We can create a link in the Navigation menu for this afterwards.
Start with defining some fields to retrieve – just click on the plus sign. You’ll get an Ajax refresh that listed off the different groupings of fields. You should scroll around in here and see what’s available. Pick a few easy ones from the Node group like Author and Post date. As you pick fields you’ll see that Views is building the query for you and showing you example output:
As you move through the various items in the menu you can easily build some really cool output. The relationships block is used to define keys between fields. Arguments can be used to filter data based on a parameter, for example s returning records based on a date limiter.
After you get done playing around with the data you need to select a display view for your query results. There are several options, but for this example I’ve selected the page display mode. What this will be is create a view of the output in a “page” – gee imagine that. The output will display in the main content area.
The display view can either inherit from the base display or you can pick a new display mode and over ride the base settings with new fields or different sorting options or whatever. Just add a new display and then pick a setting – be sure to save things with the button down the page:
When doing this make sure to watch the button carefully as they are AJAX and change quickly. If you click something and you think nothing has happened, be sure to scroll down and look at the output.
Once you get done screwing around with your view you can then add it to a menu – for the page type just use the page settings. I added mine to my main menu. Experiment with the other display types.
That’s pretty much it. If you start to experiment with Views you can really learn to do some very interesting things. This is especially true for very active group/team sites where a lot of people are contributing content. One interesting things I’ve done with Views is assemble sorted “update” pages that show the latest contributions sorted by dates, or by users. This is a great way to keep up to date on what your teamates are doing.