Drupal Question: How to theme a block created with views?

Drupal LogoTonight sees me writing a very frustrated Drupal post, but I’ve googled until my eyes went googly and I can’t see a solution.

So I turn to you, lovely people of Drupal Planet. I need your expertise and advice. And possibly a sample .tpl.php file…

Here’s my problem – I’m using views to create a block for the sidebar of my site. What I want to do is display a list of latest content showing the title of the post and an ImageField thumbnail. Examples of what I’m trying to achieve are on Daily Mail and Holy Moly – both on their right-hand sidebars.

Where I’m struggling is that I want granular control over the display output. I want to build the <img> tag manually, pulling the node title in as the ALT and TITLE tag for each listing.

I’m using an unformatted view type and the fields display. I can’t select the whole image field because that’s a pre-configured HTML string. And the ‘meta’ version is seemingly useless. I’ve also played around with block-specific template files, but nothing has worked.

So, over to you Drupal geniuses! Hit me with your best solution, because I’m fresh out of ideas!

15 thoughts on “Drupal Question: How to theme a block created with views?

  1. Abhijit

    You dont need a tpl.php to do this. In views add the node:title as a field. Add the image (this should be from an image-field), and for the “Format” in the image, select “URL to file” and check “Exclude from display” to hide this image field. Add a “Global:Custom text” field. In the “Text” section of this field write – <img src=”[field_image_fid]” alt=”[title]” title=”[title]” />, where [field_image_fid] is the image field token and [title] is the title field token. These tokens are available in the “Replacement patterns” fieldset below the field textarea. That should get you all set! Enjoy!!

    1. gerard

      Awesome solution, Abhijit – I didn’t know this functionality even existed in Views. You’ve taken my knowledge of Views theming to a whole new level. My block is now available on Unreality Shout’s right sidebar – and I’ll do a write-up of this technique when I get a few moments!Genuine thanks – I’m sure everybody reading this understands exactly how much time you’ve saved me!

  2. dixhuit

    Abhijit’s right. Just bring in the fields you want to reference but exclude them from the display. Then either add a custom text field or override the output of another field and put your custom code there. You can then include tokens that refrence the fields that you excluded from display.

  3. Matt

    This just blew my mind.  I’ve built countless views using complicated template overrides which would be greatly simplified using this technique.  Amazing.Matt

  4. tzoscott

    Saw this on the Drupal aggregator, and came here to provide the answer (Abhijit beat me to it, well done).As you see now – get your view to provide the raw material first, and then you can assemble it together. Most of the time, this works and you can ignore the Views defaults and “roll your own” markup element using the raw bits.The times where Views output becomes a bit more complicated are when you want one field to have access to another field’s data, or you want to combine data from different $rows. Only then do you start needing to mess around with PHP regular expressions (and you end up either 1 theme layer up or down from where you thought you should be working). In these cases I always spend a few minutes wondering if there’s any other way (and there probably is, if I write a views handler), and then I dive in and start rewriting the output in PHP. But most of the time, the answer is right there in Views tokebs. 🙂

    1. gerard

      Thanks for stopping by and filling in a bit of extra detail though! What happens if you want to do something conditional with the output – like handling rows that don’t have imagecache fields differently to those that do?

  5. Felipe Fidelix

    You gotta be kidding man… Asking for help on Drupal Planet? WTF are you thinking?The Forums, the Mailing list and the IRC channels are for that!

    1. gerard

      Oh, the audacity! Sorry Felipe, I figured I’ve shared a few Drupal howto posts on Drupal Planet, people probably wouldn’t mind answering a question. And at least two Drupallers have gained new insight from this post, so mission accomplished 🙂

  6. wordjuggler

    Great post; as a beginner building my own site I have been trying to work this out for days.I like Drupal (have worked with a preconfigured Drupal site at work for a while now) but the learning curve is steep, even for a semi-geeky person.Yes, there are resources on the net, but a lot assume knowledge of CSS or PHP which many people don’t have, so it’s refreshing to see something explained so clearly.

  7. Anonymous

    Could anyone please post an advance newsletter module(other than simplenews) I have an urgent requirement. Thanks in advance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s