4

I'd like to add some classes to local task items, now there is only "active" class available which is pretty not flexible, I'd like to have "first" on first item and "last" on last item.

Tried theme_menu_local_tasks() and theme_menu_local_task(), neither of them work. And the post about "Add first and last classes to secondary local tasks" is probably for Drupal 6 so it's not working either.

1

You can use jQuery :first and :last selectors, example:

$('ul li:first').addClass(‘first’);
$('ul li:last’).addClass(‘last’);

Or you can use CSS :first-child and :last-child selectors, example:

li:first-child { add your css here }
li:last-child { add your css here }

The CSS way will give better performance.

0

You could use hook_menu_local_tasks_alter(). You can access all local tasks through the first parameter$data and you can add more class names to the option array localized_options of each tab.

function YOURMODULE_menu_local_tasks_alter(&$data, $router_item, $root_path){
    if(isset($data['tabs'][0])){
        $tabs =  $data['tabs'][0];  
        $i = 1; 
        foreach($tabs['output'] as &$t){
            if($i == 1) $class = 'first';
            elseif($i = $tabs['count']) $class = 'last';
            if(isset($t['#link']['localized_options']['attributes'])){
                $attributes = $t['#link']['localized_options']['attributes'];
                $attributes['class'][] = $class;
            }else{
                $attributes = array(
                    'attributes' => array(
                        'class' => array($class)
                    )
                );
            }
            $t['#link']['localized_options'] = $attributes; 
            $i++;
        }
        $data['tabs'][0] = $tabs;
    }
}

If you don't want to loop through all local tasks for performance sake, you can directly update$data['tabs'][0]['output'][0] and $data['tabs'][0]['output'][$data['tabs'][0]['count']-1]. But you should check if the indexes exist, otherwise you will get Warning: Undefined index...

0

You may use the nth-child CSS 3 property if .active is not convinent:

p:nth-child(2) {
  background:#ff0000;
} 

Source is MDN.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.