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