Drupal: Adding a Unique ID to a Themed List Wrapper

Or, how to style lists like a boss.

Well, well, well. We don't usually post much geeky tech stuff here. But rest assured, there's plenty of geekery going on behind the scenes here at The Vacuum. Let's crack open the door for a minute here & post something techy. Who knows, it might be just the thing you're looking for some day. Stranger things have happened.

The other day I needed to output a couple of lists from a custom Drupal 7 module using theme_item_list(), and I realized styling them properly would be much easier if they each had a unique ID on their wrapper div element instead of just the generic class="item-list".

Here's how I added an ID by overriding the function in my theme's template.php.

One gotcha: I was originally calling theme_item_list() directly, which meant my overridden function wasn't being called. As indicated here, I switched to calling theme('item_list') instead, and voila, custom IDs for my list wrappers. Also, don't forget to clear your cache!

function MYTHEME_item_list($variables) {
  $items = $variables['items'];
  $title = $variables['title'];
  $type = $variables['type'];
  $attributes = $variables['attributes'];
  // KM: Check for an item in the $variables array called 'container_id'
  // KM: If not found, default to a blank string.
  $container_id = isset($variables['container_id']) ? ' id="' . $variables['container_id'] . '"' : '';

  // Only output the list container and title, if there are any list items.
  // Check to see whether the block title exists before adding a header.
  // Empty headers are not semantic and present accessibility challenges.
  // KM: Add the container_id attribute to the list element container
  $output = '<div class="item-list"' . $container_id . '>';
  if (isset($title) && $title !== '') {
    $output .= '<h3>' . $title . '</h3>';
  }

  if (!empty($items)) {
    $output .= "<$type" . drupal_attributes($attributes) . '>';
    $num_items = count($items);
    foreach ($items as $i => $item) {
      $attributes = array();
      $children = array();
      $data = '';
      if (is_array($item)) {
        foreach ($item as $key => $value) {
          if ($key == 'data') {
            $data = $value;
          }
          elseif ($key == 'children') {
            $children = $value;
          }
          else {
            $attributes[$key] = $value;
          }
        }
      }
      else {
        $data = $item;
      }
      if (count($children) > 0) {
        // Render nested list.
        $data .= theme_item_list(array('items' => $children, 'title' => NULL, 'type' => $type, 'attributes' => $attributes));
      }
      if ($i == 0) {
        $attributes['class'][] = 'first';
      }
      if ($i == $num_items - 1) {
        $attributes['class'][] = 'last';
      }
      $output .= '<li' . drupal_attributes($attributes) . '>' . $data . "</li>\n";
    }
    $output .= "</$type>";
  }
  $output .= '</div>';
  return $output;
}

I use it like this:

$output .= theme('item_list', array(
    'items' => $typeList,
    'title' => $typeName,
    'type' => 'ul',
    'attributes' => array('id' => 'scope-list'),
    'container_id' => 'type-list-wrapper',
    )
  );

And here's the output:

<div class="item-list" id="type-list-wrapper">
  <h3>Type:</h3>
  <ul id="type-list">
    <li class="first"><a href="/projects">All</a></li>
    <li><a href="/projects/residential/">Residential</a></li>
    <li class="last"><a href="/projects/commercial/" class="active">Commercial</a></li>
  </ul>
</div>

There you have it, a nice unique ID on our list wrapper, just waiting for some CSS to rain down upon it. Neat!

Posted by kris
August 28, 2012 - 4:31pm

TAGS: Dev / Drupal / Tech



Permalink

Spearhead Sneak-Peek

Logo, Identity, Print, Web, Motion & more...

We've been working with Spearhead for many months now, and it's about time we leaked some of the results. We began with a 2-day intensive brand workshop with key Spearhead stakeholders, that resulted in a merger of two company divisions, 'Spearhead Timberworks' and 'Clearform Design', to simply 'Spearhead'. Integration is good, Batman. We then redesigned their logo, letterhead, business cards, brand guidelines, a little half page print ad and we're currently redoing their website. This will undoubtedly become a case study, but we just couldn't sit on all this any longer. Here's a sneak peek, much more to come.

You can see the interim version of their site here.

Posted by nate
May 29, 2012 - 10:16am

TAGS: brand / identity / logo / print / spearhead



Permalink

Five Full Runs for Retallack:The Movie has been nominated for a Vimeo Festival Award in the Action Sports category. Feels pretty cool... the video has over 70,000 views to date. This was more or less a pet project, first shown at KMC Magazine's Backyard Booty film festival and released that same night online.

We had shot this footy for the film, passed our files off and that was just kinda gonna be it. But we knew we had something interesting that we wanted to show - full runs from start to finish, we basically didn't want to edit them at all. Most action sports has aggro music with quick edits - not that there's anything wrong with that, but we wanted to present the inherent beauty of what actually happened, from beginning to end.

Posted by nate
May 28, 2012 - 2:22pm

TAGS: Cinematography / edit / ski / skiing



Permalink

Well wouldn't you know it - the first project we did with the National Film Board won a Gemini award! The Test Tube with David Suzuki snagged the award for Best Original Program or Series produced for Digital Media - Non Fiction. That's not only a mouthful, but a huge honour for us!

We thought it was pretty cool to be on the list of nominees. Turns out we were right, it was pretty cool. What was even cooler and more surprising though, was winning. Thanks to everyone who chipped in to make the project a success!

Posted by kris
November 1, 2011 - 2:15pm

TAGS: Awards / Flash / Motion / News / NFB



Permalink

Betcha Can't Give Away Just One!

50,000 Likes in a week for Ruffles Canada

After seeing our success with Juniper Park for Ruffles USA (100,000 Likes in 48 Hours), Frito-Lay Canada saw some potential. So they called up their agency (BBDO Toronto), who then called us with the concept of a "Mancave".

We then developed the creative, designed and built the coupon giveaway app. We also wrote the copy inhouse, which agencies usually do themselves when they work with us, so that was a fun bonus. Best of all, since we had the backend for a Ruffles coupon giveaway on Facebook already developed, we were able to repurpose some code, and pass savings along to BBDO, who passed them along to Frito-Lay. Everyone wins!

After escaping from a scarily bland suburban living room, the user descends level by level as they go through the steps of entering the contest - winding up in the very deepest of Mancaves. The campaign generated over 50,000 likes in its first week, and at the time of this post Ruffles Canada boasts 80,000 likes. We're really eating up Facebook campaigns these days. Get it? Eating up?

Posted by nate
October 26, 2011 - 4:33pm

TAGS: bbdo / facebook / giveaway / Ruffles



Permalink