The Views2 Attachment Display

I’ve recently started my first Drupal 6 site and with that, my first exposure to Views2. At first glance, all the available template files and the slick new interface got me excited about the second coming of Views, but then I started working on the views for my site and quickly became frustrated with the new mechanics. I could no longer just write what I needed.

So why was it so hard? I simply wanted the first item of data returned to use a larger picture, than the rest of the items. This was my “Featured Item”. Pretty simple right? With the previous Views module, this was easily doable, But now I had no idea how to proceed. My first thought was that I’ll just have to do some reading of the docs and figure it out pretty quickly.

Not so fast.

I dug through the Views2 API and found nothing but frustration. Finally, after two forum posts and patiently awaiting responses on #drupal-support, I found my solution – the Attachment display.

What is this Attachment display? From the API docs, “Attachment displays are secondary displays that are ‘attached’ to a primary display. Effectively they are a simple way to get multiple views within the same view. They can share some information.”

To set this up, just add the Attachment display to your view. For this example, I needed the attachment to display before the page display, and attached to the page display. Also, I set the attachment to 1 for “items to display”. Now, for the page display, set the “items to display” to 9 with an offset of 1. The offset skips the first item, which is rendered with our attachment display, and Viola, we have our “Featured Item” displayed differently. So after adding some div tags to some template files for the view, I was good to go.

What I really like about this display is that its another view, So the fields that are only needed for my “Featured Item”, do not show up for the rest of the items. With the previous Views module, all my items would have the same fields, and I needed conditionals to decide which fields to display.

views-attachment-thumb.jpgviews-page-thumb.jpg

Special shout out to Crell on #drupal-support on freenode for the help, Whoooot Wooooo

Eric

Eric works for Phase2 Technology. He sometimes wants to fight his code and really show it who’s boss.


Is this something you could use to allow others to attach images to your original node? Or is this something whereby only the original author could do so?

This sounds much like something I’m hoping to do with a Drupal app I’m building now, and I haven’t found a good way to let other users attach images to the original node…

I agree that attachments are really useful. They allow set-ups where you’d normally need both views and panels, for instance to display the latest article in full followed by the title of the next 9 articles (the way James Walker does for instance). Also note that the feed display is a specific type of attachment.

I feel that views 2 has an even steeper learning curve than views 1 but that it allows for even more possibilities.

Happy to help, Eric. Thanks for helping to spread the knowledge even wider!

Just check $id which is present in all templates; it tells you how often that particular template has been invoked. If you’ve got a template for a particular view/display you’re guaranteed that when $id = 1 (hm or maybe 0) that’s the first one. You can then style it differently. In the preprocess you could just as easily add a template suggestion using the id so you could give each row its own template file if you so desired.

$id is in fact available to use. Can we update the template example file?

First the comments should include this very useful information about $id . I also couldn’t find this in any of the preprocess funcitons of theme.inc.

And second, might be a good idea to change the name of the preprocess $id variable, to head off confusion with the foreach key variable of the tempate file example.

Did I miss that in the docs? I’m definitely going to check into $id. Don’t remember seeing it documented in the default template files.

I was also just discovered by the power of preprocessing for templates so I’ll dig a little bit on that as well.

Thanks for the comments.