I want to add a class of 'clearfix' to the links that appear at the bottom of a node in teaser view (i.e. 'read more' and 'add new comment'.) The class needs to be added to the <ul> tag.

This is my code so far:


function bsc_theme_preprocess_node(&$variables) {
		$attributes = $variables['content']['links']['#attributes'];
		$attributes['class'][] = 'clearfix';	
}

This code successfully adds a class of 'clearfix' to the $attributes['class'] array. However, the class does not appear within the html markup.

What more do I need to do to get the clearfix class to be output?

Thanks.

Comments

dreamleaf’s picture

Olivaw’s picture

If anyone cares there are two ways of doing this:

(1) put the following function into your template.php:

function your_themename_preprocess_node(&$variables) {


if($variables['node']->type == 'blog_post' && $variables['node']->view_mode == 'teaser')
{
$variables['content']['links']['#attributes']['class'][] = 'clearfix';
}

	
}

(2) Overriding theme_links(). I happened to come across the following post:

theme_links() would make changes to all implementations

theme_links__node() would only make changes to the links inside nodes

theme_links__comment() would only makes changes to links inside comments

theme_links__contextual() for contextual links, etc...

And indeed if you use the following code in your template.php it will add a class of clearfix to the <ul> but only for theme_links that appear in the node part of the page. I've commented where I made a change:

function your_themename_links__node($variables) {
	
  $links = $variables['links'];
  $attributes = $variables['attributes'];
  $heading = $variables['heading'];
  global $language_url;
  $output = '';

  if (count($links) > 0) {
    $output = '';

    if (!empty($heading)) {
      if (is_string($heading)) {

        $heading = array(
          'text' => $heading,

          'level' => 'h2',
        );
      }
      $output .= '<' . $heading['level'];
      if (!empty($heading['class'])) {
        $output .= drupal_attributes(array('class' => $heading['class']));
      }
      $output .= '>' . check_plain($heading['text']) . '</' . $heading['level'] . '>';
    }
	

   // adds clearfix class to <ul>
   $attributes['class'][] = 'clearfix';
	
	
	
	
    $output .= '<ul' . drupal_attributes($attributes) . '>';

    $num_links = count($links);
    $i = 1;

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

      if ($i == 1) {
        $class[] = 'first';
      }
      if ($i == $num_links) {
        $class[] = 'last';
      }
      if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
           && (empty($link['language']) || $link['language']->language == $language_url->language)) {
        $class[] = 'active';
      }


      $output .= '<li' . drupal_attributes(array('class' => $class)) . '>';
		
		

      if (isset($link['href'])) {

        $output .= l($link['title'], $link['href'], $link);
      }
      elseif (!empty($link['title'])) {

        if (empty($link['html'])) {
          $link['title'] = check_plain($link['title']);
        }
        $span_attributes = '';
        if (isset($link['attributes'])) {
          $span_attributes = drupal_attributes($link['attributes']);
        }
        $output .= '<span' . $span_attributes . '>' . $link['title'] . '</span>';
      }

      $i++;
      $output .= "</li>\n";
    }

    $output .= '</ul>';
  }

  return $output;
}