At the end of a node I have the "Read more", "Comments" and "Add new comment" links...

<ul class="links inline">
  <li class="node-readmore first>...</li>
  <li class="comment-comments>...</li>
  <li class="comment-add last>...</li>
</ul>

Now, I can change/add class(es) to the ul using a function in my template.php as follows:

function MYTHEME_preprocess_comment(&$variables) {
  $variables['attributes']['class'][]='new-class";
}

I.e, the classes (links, inline) for the ul are stored in the following array:

$var['content']['links']['comment']['#attributes']['class']

But how can I add a class to the li elements? I've tried so many ways now but I cannot figure it out?!

To be clear, I want to end up with

<code>
<ul class="links inline">
  <li class="node-readmore NEW-CLASS first">...</li>
  <li class="comment-comments NEW-CLASS">...</li>
  <li class="comment-add last NEW-CLASS">...</li>
</ul>

Regards B

Comments

milodesc’s picture

norendahlb’s picture

Yeah, maybe. I'm looking into it now. It's a little closer, but still far away.
I don't really get it. For starters I don't know where this code should be written.

I'll keep trying to add to the attributes in the item_list (in my template.php), see if I can figure it out.

Jaypan’s picture

The array that represents a list looks something like this:

$list = array
(
  '#theme' => 'item_list',
  '#items' => array
  (
    'item 1',
    'item 2',
    'item 3',
  ),
);

When you add your class 'new-class', it looks like this:

$list = array
(
  '#theme' => 'item_list',
  '#attributes' => array
  (
    'class' => array('new-class'),
  ),
  '#items' => array
  (
    'item 1',
    'item 2',
    'item 3',
  ),
);

The part you have to change is the #items. Each item is a string, you need to turn it into an array. So this:

  '#items' => array
  (
    'item 1',
    'item 2',
    'item 3',
  ),

Needs to become this:

  '#items' => array
  (
    array('data' => 'item 1', 'class' => 'list-item-class-1'),
    array('data' => 'item 2', 'class' => 'list-item-class-2'),
    array('data' => 'item 3', 'class' => 'list-item-class-3'),
  ),

So you'll need to alter $var['content']['links']['comment']['#items'] until it looks like the above.

norendahlb’s picture

Really appriciate the help. However, as frustrating as it is, i still can't get it right. Let me recap, if you bare with me.

Currently I have:

$var['content']['system_main']['nodes'][5]['links']['comment'] (Array, 3 elements)

#theme	(String)	links__node__comment
#links	(Array, 2 elements)	
	comment-comments 	(Array, 5 elements)
	comment-add 		(Array, 4 elements)
#attributes	(array, 1 element)
	class (Array, 2 elements)
		0 (string) links
		1 (string) inline

Which renders:

<ul class="links inline">
    <li class="comment-comments">...</li>
    <li class="comment-add">...</li>
</ul>

I do not want to alter anything inside the comment-comments and comment-add arrays.
I just want to change their names, like
comment-comments > "comment-comments my_class"
comment-add > "comment-add my_class"

So it is infact the #links-array I want to change.

From
[comment-comments, comment-add] to
['comment-comments my_class', 'comment-add my_class']

So that it renders

<ul class="links inline">
    <li class="comment-comments my_class">...</li>
    <li class="comment-add my_class">...</li>
</ul>

I only manage to make NEW list items (adding more elements to the #Links array)...and they are empty (nothing between the li-tags). The original ones gets rendered automatically, with number of comments and links to correct places etc.

Again, thanks for your time and effort trying to explain to me how it's done.

Jaypan’s picture

I don't know what I'm looking at here:

$var['content']['system_main']['nodes'][5]['links']['comment'] (Array, 3 elements)
#theme (String) links__node__comment
#links (Array, 2 elements)
comment-comments (Array, 5 elements)
comment-add (Array, 4 elements)
#attributes (array, 1 element)
class (Array, 2 elements)
0 (string) links
1 (string) inline

Can you show the output of this:

die('<pre>' . print_r($var['content']['system_main']['nodes'][5]['links']['comment'], TRUE) . '</pre>');
norendahlb’s picture

That outputs what I was trying to describe in previous post :P

Array
(
    [#theme] => links__node__comment
    [#links] => Array
        (
            [comment-comments] => Array
                (
                    [title] => 1 kommentar
                    [href] => node/5
                    [attributes] => Array
                        (
                            [title] => Hoppa till detta inläggs första kommentar.
                        )

                    [fragment] => comments
                    [html] => 1
                )

            [comment-add] => Array
                (
                    [title] => Lägg till ny kommentar
                    [href] => comment/reply/5
                    [attributes] => Array
                        (
                            [title] => Lägg till en ny kommentar till denna sida.
                        )

                    [fragment] => comment-form
                )

        )

    [#attributes] => Array
        (
            [class] => Array
                (
                    [0] => links
                    [1] => inline
                )

        )

)
Jaypan’s picture

Ok, you should be able to do this:

$var['content']['system_main']['nodes'][5]['links']['comment']['#links']['comment-add']['attributes']['class'][] = 'some_class';
norendahlb’s picture

That adds 'some_class' to the content (the link) between the li-tags.

<li class="comment-add">
  <a href="..." title=".." class="some_class"> Add comment</a>
</li>

Thoose li-tags sure are hard to alter...

Jaypan’s picture

In that case, you probably will have to override theme_links(). I don't think you'll be able to do it in a preprocess function.