1

I don't think this is hard at all but beeing a beginner at PHP and Drupal I need some help to do this.

I want to be able to add classes to my navigation elements that are outputted with this code:

function mytheme_menu_link__menu_block__1($variables) { 

$element = $variables['element'];

$sub_menu = '';

if ($element['#localized_options']['attributes']['title'] != 'misc' && $element['#localized_options']['attributes']['title'] != 'something') {

    if ($element['#below']) {
        $sub_menu = drupal_render($element['#below']);
    }

    if ($element['#original_link']['in_active_trail'] == 0) {
        $output = ''.$element['#title'].'';
    }
    else {
        $output = ''.$element['#title'].'';
    }

    $link = l($output, $element['#href'], array('html' => TRUE));

    return $link;
}
else {
    return '';
}
}

If the menu item is active, it should have class X, otherwise class Y. Could someone explain how I add this?

------------------------- Correct Answer -------------------------------

Here is the correct code that longboardnode helped me come up with.

function mytheme_menu_link__menu_block__1($variables) { 

$element = $variables['element'];
$sub_menu = '';

if ($element['#localized_options']['attributes']['title'] != 'misc' && $element['#localized_options']['attributes']['title'] != 'something') {

    if ($element['#below']) {
        $sub_menu = drupal_render($element['#below']);
    }

    if ($element['#original_link']['in_active_trail'] == 0) {
        $output = $element['#title'];
        $class = 'activeClass';

    }
    else {
        $output = $element['#title'];
        $class = 'otherClass';

    }

    $link = l($output, $element['#href'], array('attributes' => array('class' => array($class)), 'html' => TRUE));

    return $link;
}
else {
    return '';
}

}

2

I hope I understand your question. If you want to add class to l(), then it must be an array, so your "$link=..." line would read

$link = l($output, $element['#href'], array('attributes' => array('class' => array('YOUR_CLASS_NAME')), 'html' => TRUE));

You can also read more and see other examples in Drupal's l() documentation

Hope this helps.

  • I understand what I want, I don't understand the syntax well enough. That line still gives me syntax error though. Parse error: syntax error, unexpected ';'. Thank's for helping. – Johan Dahl Feb 21 '13 at 8:26
  • I fixed it. You have to put class names as an array itself. I updated my original question with the answer. – Johan Dahl Feb 21 '13 at 8:47
  • Glad it helped! I did have a missing ) at the end to close the l() statement, revised in my answer. It should work now without the need to make HTML its own array. It's not a big deal but you might want to check it over. The third ) after your ($class))) effectively closes the third array in the l() statement and the l() documentation distinctly says that HTML should be an element inside l()'s third array ($options). – longboardnode Feb 22 '13 at 6:36
  • 1
    Johan, did you try my revised answer with the extra ) at end of line? As far as I can see you really shouldn't make HTML->TRUE a separate element in l() but should be part of the third element array. – longboardnode Feb 25 '13 at 6:42
  • Hi, I just tried it. It seems to work! Although making HTML -> TRUE into a seperate element seems to work just as well. At least I can't see any errors and I get my classes like I wanted. Anyway I'm going with your method since I trust you more than myself! :) – Johan Dahl Feb 28 '13 at 8:17
1

You have to insert your desired class as attribute in your l() like:

if ($element['#original_link']['in_active_trail'] == 0) {
    $output = ''.$element['#title'].'';
    $class = 'activeClass';
}
else {
    $output = ''.$element['#title'].'';
    $class = 'otherClass';
}


l( $output, $element['#href'], array(
  'attributes' => array(
    'id' => '',
    'class' => $class,
  ),'html' => TRUE))
);
  • Thank's. Although I'm getting a syntax error: "Parse error: syntax error, unexpected T_DOUBLE_ARROW". I can't see what's wrong, can you? – Johan Dahl Feb 20 '13 at 15:27
  • hmm sorry about that try array('html' => TRUE))) – Pan Chrono Feb 20 '13 at 16:02
  • Can't get it to work. How do you type out the entire l(); part? – Johan Dahl Feb 20 '13 at 16:10

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.