Community Documentation

Hiding active language from language switcher block

Last updated November 5, 2012. Created by Botto on September 12, 2010.
Edited by frederickjh, sylvain_a. Log in to edit this page.

The language switcher block does not support the ability to hide the active language, therefore this has to be done in template.php
This will also disable any active language links that use the theme_links

Edit your template.php file, this will usually be located in the folder for the theme you are using. If it does not exist, you can make it.

We will now want to copy the original theme_links code in to an override function called phptemplate_links
Located here http://api.drupal.org/api/function/theme_links/6

The modification that needs to be done is to unset the array element that contains the currently active language.

<?php
unset($links[$language->language])
?>

The final function should look like this:

<?php
function phptemplate_links($links, $attributes = array('class' => 'links')) {
  global
$language;
 
$output = '';
  if (
count($links) > 0) {
   
$output = '<ul'. drupal_attributes($attributes) .'>';
   
   
//Removes the current active language from the language switcher
   
unset($links[$language->language]);

   
$num_links = count($links);
   
$i = 1;
   
    foreach (
$links as $key => $link) {
     
$class = $key;

     
// Add first, last and active classes to the list of links to help out themers.
     
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->language)) {
       
$class .= ' active';
      }
     
$output .= '<li'. drupal_attributes(array('class' => $class)) .'>';

      if (isset(
$link['href'])) {
       
// Pass in $link as $options, they share the same keys.
       
$output .= l($link['title'], $link['href'], $link);
      }
      else if (!empty(
$link['title'])) {
       
// Some links are actually not links, but we wrap these in <span> for adding title and class attributes
       
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;
}
?>

This function can be placed anywhere in the template.php file

Comments

Solution for D7

In D7 it gets much easier: http://groups.drupal.org/node/139329

nobody click here