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?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.
Special shout out to Crell on #drupal-support on freenode for the help.




Comments
Now that is paying it forward!
Happy to help, Eric. Thanks for helping to spread the knowledge even wider!
Simple with templates...
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.
Must of missed that
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.
You are correct sir,
$idis 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
$idvariable, to head off confusion with the foreach key variable of the tempate file example.Attachments are very useful
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.
Is this something you could
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…
There is, but that has
There is, but that has nothing to do with this article.
Using attachment
Hi,
I'm new to Drupal 6 and views.
I'm trying to allow my visitors to click on either full name or username.
I'm struggling with this.
My views list fairly simple user details http://www.rnmedics.com/userlisting?order=value&sort=desc - I want and additional view with more detail to be accessible from the main listing.
Can I do this?
Cheers!
Mike
VERY HELPFUL!
Eric, I was googeling around for a good use case [and explanation] of the implementation of the display type, ATTACHMENT. Yours was the 3rd one I read and the only one I needed! I'm putting my slides together for the CMS Expo 2009 www.cmsassociation.com/35.html and I am covering basic VIEWS but I had never used the ATTACHMENT display before. You saved the show -- Bro! THNX! - Doug Vann - http://www.dougvann.com - http://www.twitter.com/dougvann
Attachments - what they are not
Thanks for the info. To clarify, in views for drupal 6.x, one cannot have completely different views attached to each other (ie: node versus user data), which makes sense because attachments are still getting the data from one big sql query and showing the row data differently in each attachment/display. If what you want is gotten from two different queries, you should use panels and put your views in them. Also, after messing around with attachments, I would argue that ordering them around takes so much effort and frustration that I would simply clone the view, alter it slightly and put each into a pane. It's a better use of time and more understandable for future maintainers, not to say anything about the efficiency benefit attachments may have over panel views.
THANK YOU!
Great, quick little intro into Views2 - Attachments. I struggled and grappled and then sought the source.
For anyone else: - I implemented an image gallery defined by taxonomy - Each term has a Description; this is the Artist's Statement - normal Views2 Page gives me my fields within a tax argument - I couldn't figure out how to get the Term Description to show, or when it did, how to show only one time "as a header" (not once per node whose fields display) - Using Attachment with items count of 1 and Attach to Page, I got it!
Thanks very much for your helpful post, Eric
Post new comment