Return to Snippet

Revision: 4637
at January 14, 2008 09:44 by berkes


Initial Code
/**
 * Return a themed set of links.
 *
 * @param $links
 *   A keyed array of links to be themed.
 * @param $attributes
 *   A keyed array of attributes
 * @return
 *   A string containing an unordered list of links.
 */
function phptemplate_links($links, $attributes = array('class' => 'links')) {
  $output = '';

  if (count($links) > 0) {
    $num_links = count($links);
    $i = 1;

    $items_str = '';
    foreach ($links as $key => $link) {
      $class = '';

      // Automatically add a class to each link and also to each LI
      if (isset($link['attributes']) && isset($link['attributes']['class'])) {
        $link['attributes']['class'] .= ' ' . $key;
        $class = $key;
      }
      else {
        $link['attributes']['class'] = $key;
        $class = $key;
      }

      // Add first and last classes to the list of links to help out themers.
      $extra_class = '';
      if ($i == 1) {
        $extra_class .= 'first ';
      }
      if ($i == $num_links) {
        $extra_class .= 'last ';
      }
      
      // Is the title HTML?
      $html = isset($link['html']) && $link['html'];

      // Initialize fragment and query variables.
      $link['query'] = isset($link['query']) ? $link['query'] : NULL;
      $link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;


      //Check if we have a link, for sometimes we need to output just a splain string.
      //A class can only contain spaces, hyphens or underscores. Translate these to underscores.
      $func_class = strtr($link['attributes']['class'], '- ', '__');
      //dpm($func_class);
      if (isset($link['href'])) {
        //First, see if we have a theme function for this class
        if (!empty($func_class) && ($function = theme_get_function('links_'. $func_class))) { //then,  look for a theme function based on the class.
          dpm($function);
          $items_str .= theme('links_'. $func_class, $link);
        }
        else { //If no specific function was found, fall back to a default.
          $items_str .= '<li class="'. $extra_class . $class .'">';
          $items_str .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);          $items_str .= "</li>\n";  
        }
      }  
      else if ($link['title']) {
        //Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (!$html) {
          $link['title'] = check_plain($link['title']);
        }
        $items_str .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
      }
    
      $i++;
    }
    $output = '<ul'. drupal_attributes($attributes) .'>'. $items_str .'</ul>';
  }

  return $output;
}

/**
 * Theme callbakc from theme_links, renders the comment_add link
 **/
function phptemplate_links_comment_add($ink) {
  $output =  '"<li class="'. $extra_class . $class .'">';
  $output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
  $output .= "</li>\n";  
}

Initial URL
http://api.drupal.org/api/function/theme_links/5

Initial Description
Place the first function, phptemplate_links() in your template.php file. Alternatively (and IMO better) is to name the function your_theme_name_links() where your_theme_name is the name of your theme.

From here on, you can add theme functions like your_theme_name_links_comment_add(), whom will render the specific links with the comment_add class.

Initial Title
Override specific links in theme_links based on their class.

Initial Tags
links, drupal, theme

Initial Language
PHP