Drupal: How to Print a Pager for a View in a Block

Aaaarrgghh ... the Drupal mantra - "there's a module for that" - didn't hold true! There was no module. I had to come up with a solution; a solution to a problem that should have been simple (it was in the end).

I had a spec for a job to create image galleries of "our work", one node per image and 5 categories with about 20 samples in each category - so about 100 nodes. Each category was part of a dropdown menu (created using DHTML Menu module), whereby if you click on a category, it would show the first "sample work" image and a pager in the sidebar to scroll to the next item. Sounds simple? it did to me.

So, off I went looking for a "Drupal views pager in block module" using my favourite search engine! The closest I could find was the FreePager module - a great module - but it didn't suit my specific need, in that it could scroll through nodes, but I needed it to scroll through items in a view.

The solution? Here:

  1. Create the view with the five different categories.
  2. Add a "block" display to the view.
  3. Click on "Theme: Information" to see the list of possible views tpl files available for that block.
  4. Choose the most suitable one - in my case: views-view-work-gallery-block.tpl.php.
  5. Create a file called the above and paste in the given code. 
  6. Remove the unneccessary code, in my case everything except the print pager section.
  7. Place this file in your theme directory.
  8. Place the block in your sidebar and hey, presto!

Filed Under:

  1. Drupal
  2. Drupal Views
  3. Drupal Theming
  4. Drupal Planet