How to make language switcher links link to frontpage in Drupal 7

Kristian Polso • July 29, 2014

Drupal 7 Drupal Planet I18N

Drupal has a block called "Language Switcher", which displays links to different language versions of the current page/node. If the node does not have translated version on the specified language, the block will not display a link for it. This can cause some confusion, since the user always expects to see links to all of the site's languages.

This can be fixed by modifying the block so that the all of the links link to the corresponding language's frontpage. It is easy to do by editing the site's theme.

Edit your theme's template.php-file (or create one if it does not exist) and add the following lines:

 /**  * Implements hook_language_switch_links_alter().  */ function MYTHEME_language_switch_links_alter(&$links, $type, $path) {   global $language;   if ($type == LANGUAGE_TYPE_INTERFACE && isset($links[$language->language])) {     foreach ($links as $langcode => &$link) {       $link['href'] = '<front>';     }   } }

Change "MYTHEME" to your theme's name.

Share This Post