Replacing the Wordpress Blog Archives with Views in Drupal 6

The Views module in Drupal is both useful and confusing. The basic idea is that Views will allow you to provide a styled list of some content. This content can be pages, stories, blog posts, comments, users, RSS feeds items, nodes and even some other kinds of data. You can think of it kind of like those generic examples of PHP and MySQL that pull out a bunch of records from a table and just give you a list. Except that it doesn't rely on just MySQL. And it doesn't just give you a list. And it does validation. And you don't really have to write any code.

The Views interface in Drupal 6 (under Administer >> Site Building >> Views) is pretty complicated. I don't understand all of it yet, so I decided to work through an example of something that I needed here on Late Night PC. There's a view included called "archive" which shows up at example.com/archive/yyyymm. This is pretty close to the way I was doing my archives under Wordpress. My archives page URLs had the form latenightpc.com/blog/archives/yyyy/mm. So I basically wanted the archive view with these changes:

  1. Move the URL from archive to blog/archives
  2. Change the parameter from yyyymm to yyyy/mm
  3. Filter the view to show only blog posts instead of all content

I wanted copy of the archive view to work on so I went to Administer >> Site Building >> Views and enabled the archive view then I clicked the Clone link for it. As it turns out I couldn't modify the original archive view itself since it's the type of view that's called a Default. Default views can be enabled, disabled, overridden or cloned but they can't be edited or deleted. The reason is that they exist in code not in the database, you can read more about that here.

After clicking the Clone link for the archive view a form came up to fill in the new view's name and details, I called it "blogsarchive" and clicked Next to save it. The Edit page for my new blogsarchive view came up with a big form and lots of options. The starting values for everything in this view are the same as the Default archive view since that's the one I cloned. Along the left side there's a list of the Displays for this view. The Displays are called Default, Page and Block. I clicked on Page and all the changes I'm talking about today apply to that display. I'll detail the changes I had to make one by one.

Changing the path for the archive view
This part isn't too hard. The Page settings are near the bottom of the form. The path defaults to "archive." This would make the view control things at the URL latenightpc.com/archive. So I clicked on the word archive and changed the value to "blogs/archive" to match the URL I want my view to show up at. Then I clicked Update to save this change.

Changing the parameter from combined year and month to two paramaters
The argument for the archive view comes from a URL parameter that looks like yyyymm, where yyyy is a four digit year and mm is a two digit month. The URL parameter is embedded in the URL if clean URLs are turned on (as mine are). So a URL for the Default archive view would look like example.com/200710. The archive URLs on my old Wordpress software were configured to look like latenightpc.com/blog/2007/10. I need to replace the single argument with two arguments and split out the year and month. One thing to be aware of in general here is that View arguments don't always come from URL parameters but they often do (as in this case).

I added the two new arguments and configured them then removed the old argument. The workflow is a little long but it makes sense when if you pay close attention as you're doing it. Before I walk through each of the bits I clicked on, I'll just say that each of these arguments needs 6 fields filled in:

  • Title
  • Action to take if argument not present
  • Wildcard
  • Wildcard title
  • Validator
  • Action to take if argument does not validate

I copied the values for these fields almost directly from the default archive view.

Back to the workflow for a bit now. To actually add the new arguments (year and month), I found that next to the heading Argument (still on the same Edit tab for the blogsarchive view), there's a + icon. I clicked the + and a form showed up with the heading Page: Add arguments and a list of possible arguments. I checked the boxes next to Node: Created year and Node: Created month then clicked the Add button.

After clicking Add, the views module wanted me to configure the new arguments in a subsequent form, starting with Page: Configure Argument: "Node: Created Month". Mimicking the default archive view, I filled in %1 for the Title and checked "Summary, sort ascending" for Action to take if argument not present. The %1 in the title stands in for the value of the first argument, like "2007". The summary option (for Action to take if argument not present) means that the view will attempt to create a summary of possible values for the argument. In this case it means that blogs/archive/2007 should give a list of the months linked to the pages which list posts for each month.

The rest of the argument configuration I left as the defaults. The Wildcard and Wildcard Title were blank since I didn't want to use them. The validator was set to default and the Action to take if argument does not validate I left as a 404.

After all this I clicked the "Update default display" button to save these changes and was greeted with another form, still dealing with the "Node: Created Month" argument, this one titled Change summary style for Argument "Node: Created Month". The options are List or Unformatted, I chose List and clicked "Update" to save this change. This brought up a third form, still dealing with the "Node: Created Month" argument. The title was Page: Configure summary style for Argument "Node: Created Month". I could check "Display record count with link" to control whether the summary by month would have a count of posts after it or not. I left this as the default (checked). I could also override the number of items to show in the summary list with the other checkbox "Override number of items to display". I also left this one as the default (unchecked). I clicked the Update button to save this last setting.

Whew. Lots of clicking. But it's not over yet because I had next to do the same thing for the "Node: Created Year" argument. I'll spare you the details because it was basically all the same (and if that's not quite right then I don't know which detail I got wrong anyways).

Okay, so now the two new arguments are there but the original argument is also there. To remove an argument there's a little button next to the + that looks like a couple arrows pointing up and down. The title for it is Rearrange. So click the Rearrange button next to the Arguments heading and you get this nifty little form with a list of the current arguments. The title is Page: Rearrange Arguments, To the left of each argument is a NSWE arrow (the one with the compass points on it) that you can drag to change their order. To the far right of each argument is a button with the title "Remove this Argument". I clicked the remove button for the "Node: Created year + month" argument and poof, it was gone. Then I clicked Update to save this change.

That's it for the arguments. Lots to click and lots of AJAX. It's a very nice interface but I think I'd like it if some things were flattened out so they weren't as wizard-y.

Filtering by node type

So far the blogsarchive view does almost everything I need. The last thing is to just restrict it to blog posts instead of showing all content on the site. This part is easy. On the right side of the view editing interface (the same page I've been at all along) there's a section titled Filters. The only thing under there is "Node: Published True" for the default archive view. I had to add one for the node type. So I clicked the Add button to the right of Filters. A form showed up with the title Page: Add filters and a list of a bunch of filter types. I checked the filter called "Node: Type" then clicked Add. After clicking Add I got a form called Page: Configure filter "Node: Type" where I had to set what type of node I wanted to filter on. There are two parts to this, the Operator and the Node Type. I set the Operator field to "Is one of" and the Node type field to "Blog entry". Then I clicked the "Update default display" button to save the changes.

That's it. All the changes to make my blogsarchive view work the way I wanted were in so I clicked the Save button and that saved my new view. Now my archives show up the way they used to so some of the links to my site that were longer broken when I moved from Wordpress to Drupal will work again.

5
Your rating: None Average: 5 (1 vote)