Nesting Views like a Mecha-Eagle

Posted by Chuck Vose Wed, 25 Aug 2010 17:47:00 GMT

As you have probably figured out, I love views. I love teasing out little features that are completely buried and obscure. Nesting Views is one of my favorite new travesties; not sure if it’s ready for Drupal for Evil, but it’s certainly not kosher.

If birds can nest, why can’t I?

extreme nesting
image by Benjamin Verdonck, reported by apartmenttherapy.com

Ah nesting season, time to buckle down, collect bits of straw, and raise little Views babies that will eventually grow up and save the world. An anecdote from several weeks ago: I was creating a fairly epic view, if I do say so myself, and it just wasn’t panning out. Somewhere between the 4 relationships and the three sorts my taxonomies were getting mismatched and my images were coming back in the wrong order. Now the taxonomies were a pretty easy fix which I’ll talk about in a later snippet post, but the images were killing me.

The situation was thus: I needed to find the most recent image uploaded for a node, but the images were stored in a related child node rather than in a cck field or something. So I had this awkward situation where I’m sorting on the child node by date, but I want the child node’s image sorted by date the other direction. This just doesn’t work in views, no amount of hook_views_query_alter or sort ordering was working out for me.

But I had a similar view in another area, actually on the child node’s page I was displaying those images in the correct order, was it possible that I could use the child nid and nest a view inside my view? Is this anathema? Surely it must be.

Turns out, this is completely painless and extremely performant! How is this possible Chuck? You’re issuing an extra Views display for every single row in a View! Ah, yes, this is true, but Views and MySQL have excellent caching for simple things and complicated Views that have 4 relationships end up not really being cacheable at all. So comparatively, a nested view is much, much faster.

This is in my views-view-field.tpl.php in my themes folder. Make sure when you add it the first time to rescan your views template files. There’s a button in Views under Theme -> Information. After that if you don’t have caching on you shouldn’t need to rescan unless you add/delete more template files.

$views = array('homepage_recently_updated',
  'node_index',
  'node_index_list');
$vname = $view->name;
if (array_search($view->name, $views) !== FALSE) {
  // Print out the correct image for a node. 
  // Finding the field_alias for the correct row is just homework, try running a
  // dpm on $field->field_alias when you save the view and you'll see a list of 
  // possibilities. Mine was absurdly named because of the many relationships,
  // I'm sure that yours will be much nicer because you're smarter than I am.
  if ($field->field_alias == "node_node_data_field_journal_node_data_field_scan_field_scan_fid") {
    // My view relies on the nid of the parent node, but this is your standard
    // list of args that you would expect to have in the query string.
    $view_args = array($row->nid);

    // If you know which display_id you're going to be using drop it here and
    // skip the switch later on. 
    $display_id = '';

    // Pull down the inner view for display later
    $view_inner = views_get_view('journal_node_primary_image');
    if (!empty($view)) {
      // Select the correct display based on which view called us
      switch ($vname) {
        case 'node_index':
          $display_id = 'block_2';
          break;
        case 'node_index_list':
        case 'homepage_recently_updated':
          $display_id = 'block_4';
          break;
      }

      // Don't use display or execute if you're just wanting to output the View. 
      // Those both have extra stuff strapped onto them that may cause problems
      // later on in the Views lifecycle according to the comments in the code.
      print $view_inner->preview($display_id, $view_args);
      return;
    }
  }

So really, all it comes down to is making sure that you’re in the right field_alias, pulling down the view with views_get_view(), figuring out your display id and any arguments, then doing a $view->preview($display_id, $view_args). Not too shabby!

Posted in ,  | Tags , , , , , ,  | 5 comments

Drupal snippets #1 (Views exposed filters, F5 tweaks)

Posted by Chuck Vose Fri, 09 Jul 2010 23:50:00 GMT

Cross-posted at the Metaltoad Blog

While I’m certain that I could never work up to the standards of Peter Cooper his “Interesting Ruby Tidbits That Don’t Need Separate Posts” series was a great help to me when I was a ruby developer. You can see the original here: IRTTDNSP #1. I’ve not found similar for the Drupal community but I believe a lot of the things I do during the week are interesting but not at all worthy of a complete post of their own. So this will be my attempt to categorize and commit those very thoughts to memory. I hope that even one of these things sparks something for you.

How to get Views’ exposed filters to display in a block

If you create a block display and have some exposed filters you may have trouble getting them to actually show up. The problem is that blocks don’t seem to like to display their exposed filters unless two prerequisites are met: there must be a full page display somewhere and it must have a path, the block must be set to use ajax. I would have never guessed this either but if you look at the requests that are going across the wire they’re going to that page display and the results are coming back over ajax.

How to reorder taxonomy terms in Views’ exposed filters

On that same page we realized that we needed to order the dropdown in a reasonable way in part just to keep people from using it on the awards day to stave off some of the traffic. Analysis of traffic suggests we actually got the ordering wrong but whatever, you just want to know how to reorder taxonomy terms in an exposed filter.

Emmys reordered taxonomies

Despite the ordering that Views seems to be suggesting below, the actual display order of terms is determined by the taxonomy weights page at /admin/content/taxonomy/. So you can feel confident that even though views doesn’t have a means to order your taxonomy selections, they will be ordered for you. Now the real question is though, is it possible to reorder those terms on just one page and not on another? I don’t know, maybe I’ll get to post about that later.

Views exposed filter settings for taxonomy

How to get an F5 Load Balancer to stop putting the Vary headers on your responses

When you’re using a CDN like Akamai having a Varies header in your response absolutely kills your caching despite all the hard work drupal puts into getting things right. We were seeing about 50-75% cachability through Akamai because of it. Mostly this was because Akamai couldn’t cache the JS/CSS which was clearly incorrect. After we figured out that it was the Varies header (props to Grendzy as usual :P), it was a hop skip and a jump to figure out which settings could be causing the actual problem.

Turns out the Varies header setting isn’t the culprit though it does in fact fix the problem we were having it also introduces some others. The culprit is in fact having Browser Workarounds on which fixes IE6 and gzip and not having HTTP/1.0 Requests on. Of course the manual doesn’t really describe what’s going on so massive props out to spark on the F5 forums for answering this question before I had the chance to ask it.

F5 settings for fixing the Varies header

Conclusion

I’ve run out of time but I’d be grateful for any feedback. If people don’t find this useful I’ll not post it on planet but if you do please keep reading and encouraging me with your great comments.

Posted in ,  | Tags , , , , , , ,  | 2 comments