Create function to convert $links to $items (item-list)

Todd Nienkerk - May 8, 2009 - 15:03
Project:Themer
Version:6.x-2.0-3
Component:Code
Category:feature request
Priority:normal
Assigned:Todd Nienkerk
Status:active
Description

Here's something I wanted to put into a new module of theming tools. I ran across this module first, though, so I figured it would be better to combine forces instead of creating a similar module.

This function takes a $links array and returns it as a themed item list. It's handy when you need your $links output to look and behave like an item list -- i.e., those things wrapped in the <div class="item-list"></div> element.

function links_to_item_list($links, $title = NULL, $type = 'ul', $attributes = NULL) {
  $items = array();

  foreach ($links as $link) {
    if (isset($link['href'])) {
      // Pass in $link as $options, they share the same keys.
      $items[] = l($link['title'], $link['href'], $link);
    }
    else if (!empty($link['title'])) {
      // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
      if (empty($link['html'])) {
        $link['title'] = check_plain($link['title']);
      }
      $span_attributes = '';
      if (isset($link['attributes'])) {
        $span_attributes = drupal_attributes($link['attributes']);
      }
      $items[] = '<span'. $span_attributes .'>'. $link['title'] .'</span>';
    }
  }

  return theme('item_list', $items, $title, $type, $attributes);
}

 
 

Drupal is a registered trademark of Dries Buytaert.