1

One item in the menu has sub-items that point to the same page (just with different arguments) ex:

Products - List item 1 \products?pid=1 - List item 2 \products?pid=2 - List item 3 \products?pid=3

Drupal assigns the "active" class in the

  • and tag of the menu items since all the items have the same page url (\products)

    I'm overwriting the menu_link function in template.php to remove the active class but the class remains in the tag

    function mytheme_menu_link(array $variables) {
      $element = $variables['element'];
      $sub_menu = '';
    
      if ($element['#below']) {
    
        if (strtolower($element['#title']) == 'Products') {
    
          if (isset($_GET['pid'])) {
            $currID = $_GET['pid'];
    
            foreach ($element['#below'] as $Taxonomy) {
    
              if ($currTaxonomyID != $Taxonomy['#localized_options']['query']['pid']) {
                $mlid = $Taxonomy['#original_link']['mlid'];
                $key = array_search('active', $Taxonomy['#attributes']['class'], TRUE);            
                if ($key) {
                  unset($element['#below'][$mlid]['#attributes']['class'][$key]);
                  $element['#below'][$mlid]['#localized_options']['attributes']['class'][$key] = '';
                }
                // remove active-trail class
                $key = array_search('active-trail', $Taxonomy['#attributes']['class'], TRUE);            
                if ($key) {
                  $element['#below'][$mlid]['#attributes']['class'][$key] = '';              
                }
              }          
            }
          }      
        }
        $sub_menu = drupal_render($element['#below']);         
      }
      $output = l($element['#title'], $element['#href'], $element['#localized_options']);  
      return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
    }
    

    Final Result:

    <ul class="menu nav">
       <li class="first leaf menu-mlid-44253">
          <a href="/products?pid=1" title="Best Practices" class="active">Best Practices</a>
        </li>
        <li class="leaf menu-mlid-44254">
          <a href="/products?pid=2" title="Checklists" class=" active">Checklists</a>
        </li>
    ....
    

    How can I remove the class "active" from the tag?

    My menu Structure:

    -Clients
    -Products
      -prod1 (url:\\products?pid=1)
      -prod2 (url:\\products?pid=2)
      -prod3 (url:\\products?pid=3)
      -prod4 (url:\\products?pid=4)
    -About
    
  • 3
    • Can u send me your menu structure.., It will helps in solving the program fast. Commented Jan 8, 2016 at 8:12
    • -Clients -Products -prod1 (url:\\products?pid=1) -prod2 (url:\\products?pid=2) -prod3 (url:\\products?pid=3) -prod4 (url:\\products?pid=4) -About
      – Viacheslav
      Commented Jan 8, 2016 at 14:34
    • This will NEVER evaluate if (strtolower($element['#title']) == 'Products') { Fix it and it may work.
      – iLLin
      Commented Feb 22, 2017 at 16:04

    1 Answer 1

    0

    I had the same problem: the l() function automatically assigns an active class when a link is pointing to the current page.

    I got around this using an (ugly) hack:

    As noted in the documentation, $options['language'] is additionally compared with $language_url->language before assigning the active class.

    So, the hack is to supply a deliberately incorrect language when calling l().

    You can find out what the current language of your site is using dpm():

    global $language_url;
    dpm($language_url->language);
    

    In my case, I know that the language of the site is definitely not und, so I appended array('language' => 'und') to my call to l():

    l('link text', 'path/to/page', array('language' => 'und'))

    After this, the a element no longer had the active class.

    1
    • He just has a bug in his code if (strtolower($element['#title']) == 'Products'). I think when/if he fixes(d) that it should be fine.
      – iLLin
      Commented Feb 22, 2017 at 16:06

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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