Override specific links in theme_links based on their class.


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. /**
  2.  * Return a themed set of links.
  3.  *
  4.  * @param $links
  5.  * A keyed array of links to be themed.
  6.  * @param $attributes
  7.  * A keyed array of attributes
  8.  * @return
  9.  * A string containing an unordered list of links.
  10.  */
  11. function phptemplate_links($links, $attributes = array('class' => 'links')) {
  12. $output = '';
  13.  
  14. if (count($links) > 0) {
  15. $num_links = count($links);
  16. $i = 1;
  17.  
  18. $items_str = '';
  19. foreach ($links as $key => $link) {
  20. $class = '';
  21.  
  22. // Automatically add a class to each link and also to each LI
  23. if (isset($link['attributes']) && isset($link['attributes']['class'])) {
  24. $link['attributes']['class'] .= ' ' . $key;
  25. $class = $key;
  26. }
  27. else {
  28. $link['attributes']['class'] = $key;
  29. $class = $key;
  30. }
  31.  
  32. // Add first and last classes to the list of links to help out themers.
  33. $extra_class = '';
  34. if ($i == 1) {
  35. $extra_class .= 'first ';
  36. }
  37. if ($i == $num_links) {
  38. $extra_class .= 'last ';
  39. }
  40.  
  41. // Is the title HTML?
  42. $html = isset($link['html']) && $link['html'];
  43.  
  44. // Initialize fragment and query variables.
  45. $link['query'] = isset($link['query']) ? $link['query'] : NULL;
  46. $link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;
  47.  
  48.  
  49. //Check if we have a link, for sometimes we need to output just a splain string.
  50. //A class can only contain spaces, hyphens or underscores. Translate these to underscores.
  51. $func_class = strtr($link['attributes']['class'], '- ', '__');
  52. //dpm($func_class);
  53. if (isset($link['href'])) {
  54. //First, see if we have a theme function for this class
  55. if (!empty($func_class) && ($function = theme_get_function('links_'. $func_class))) { //then, look for a theme function based on the class.
  56. dpm($function);
  57. $items_str .= theme('links_'. $func_class, $link);
  58. }
  59. else { //If no specific function was found, fall back to a default.
  60. $items_str .= '<li class="'. $extra_class . $class .'">';
  61. $items_str .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html); $items_str .= "</li>\n";
  62. }
  63. }
  64. else if ($link['title']) {
  65. //Some links are actually not links, but we wrap these in <span> for adding title and class attributes
  66. if (!$html) {
  67. $link['title'] = check_plain($link['title']);
  68. }
  69. $items_str .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
  70. }
  71.  
  72. $i++;
  73. }
  74. $output = '<ul'. drupal_attributes($attributes) .'>'. $items_str .'</ul>';
  75. }
  76.  
  77. return $output;
  78. }
  79.  
  80. /**
  81.  * Theme callbakc from theme_links, renders the comment_add link
  82.  **/
  83. function phptemplate_links_comment_add($ink) {
  84. $output = '"<li class="'. $extra_class . $class .'">';
  85. $output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
  86. $output .= "</li>\n";
  87. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.