I'd like to have the language switcher block in core Drupal 7 hide the active language. (ie: If you're reading the English page, don't display a link to the English page.)
I've come across this solution for Drupal 6, which seems hefty for a seemingly trivial task (and doesn't work in D7):
http://drupal.org/node/909418
I'm new to Drupal, and I am having difficulties knowing where to start.
What's interesting, is that the language links that appear on the bottom of the content that is translated don't include the language of the current page. Perhaps what I need to do is somehow override the functionality and have it output in the theme region of my choosing?
Your comments would REALLY be helpful!
Comments
If you are looking to hide
If you are looking to hide it, why not just assign the .active for the element to display: none; in your theme's CSS?
yeah. that will just work
yeah. that will just work perfectly!
Other way
If, like me, you don't like to hide elements with CSS, you can put this code in your template.php file :
<?php
function mythemename_links__locale_block($variables) {
global $language;
unset($variables['links'][$language->language]);
return theme('links', $variables);
}
?>
This is better than CSS
This approach is better than hiding the active language using CSS.
1) It will not generate the HTML at all
2) You will not have to tackle annoying UL LI's borders.
Bhavik Shah
Drupal and MongoDB enthusiast, Sr. Software Engineer at Tatvasoft
bhavikshah835@gmail.com || (+91) 901 682 0729
Hide untranslated languages
And for those who only want to hide the untranslated languages elements use this:
<?php
function iic_main_links__locale_block($variables) {
foreach($variables['links'] as $key => $value) {
if ($value['attributes']['class']=='locale-untranslated') {
unset($variables['links'][$key]);
}
}
return theme('links', $variables);
}
?>
Hide active language and format to copy Primary Links
I know it's been a while, but I thought I'd post my solution, for the sake of those like me: who never got to work this out other ways.
For drupal 7
I copied this in my css theme style:
.language-switcher-locale-url li.active a{
display: none;
}
.language-switcher-locale-url li,
.language-switcher-locale-url ul,
.language-switcher-locale-url ol,
.language-switcher-locale-url a,
.language-switcher-locale-url a:link,
.language-switcher-locale-url a.link,
.language-switcher-locale-url a:visited,
.language-switcher-locale-url a.visited,
.language-switcher-locale-url a:hover,
.language-switcher-locale-url a.hover,
.language-switcher-locale-url a.hovered
{
font-size: 15px;
font-family: Arial, Helvetica, Sans-Serif;
display: inline;
color: white;
text-decoration: none;
}
After, I got my switcher block in a free region and aligned it with my "Primary links" menu with css (Extra2 in my case)
Up to you to change fonts settings and more. I covered pretty much all angles of the link (hovered, visited). I'm relatively new to css and drupal so please feel free to correct this. This hides my active language and formats the unactive language with my Primary links menu.
** sorry for my bad english, I'm french.
Keep it theme agnostic
To keep this theme agnostic (independent of the theme you're currently using), I'd recommend doing this in a hook_language_switch_links_alter implementation.
For details, see How can I hide the active language in the language switcher block?.
This did not work for me. Am
This did not work for me.
Am I correct that I just have to add this code to the language.api.php file in /public_html/modules/system?
Not at all
No, that won't work. You need to add it to a custom module you've created. You should never add custom changes to modules from drupal.org.
Normally, I have one custom module per project named after the project that handles little tweaks like this. Let's say the site is called "Lemonade Stand". So you'd build a module with the machine name "lemonade", which would have a lemonade.module file.
In there, you would implement the hook. The "hook" is replaced by the module name:
function lemonade_language_switch_links_alter(array &$links, $type, $path) {
unset($links[$GLOBALS['language']->language]);
}
After you enable your module, your hook implementation will fire.
Ok Thank you, but I still
Ok Thank you, but I still need a little more help.
I created a folder as follows: /public_html/sites/all/modules/TasteDelft
In there I have a TasteDelft.info and a TasteDelft.module file.
the TasteDelft.info looks like this:
name = TasteDelft Custom Module
description = A Module with all specific settings for tastedelft.nl
version = "7.x-1"
core = "7.x"
project = "TasteDelftCustom"
The TasteDelft.module looks like this:
<?php
function TasteDelft_language_switch_links_alter(array &$links, $type, $path) {
unset($links[$GLOBALS['language']->language]);
}
The module is visible in my modules list but when I enable it, it leads me to a blank page: http://www.tastedelft.nl/admin/modules/list/confirm
If I go back, the module is not enabled.
Do I forget something?
Solved
Never mind, I solved it by using this code in the module:
<?php
/**
* Implements hook_language_switch_links_alter().
*/
function tastedelft_language_switch_links_alter(array &$links, $type, $path) {
global $language;
unset($links[$language->language]);
}
Below work for me... Add this
Below work for me...
Add this in your theme template.php file.
<?php
function mythemename_links__locale_block($variables) {
global $language;
unset($variables['links'][$language->language]);
return theme('links', $variables);
}
?>
No
You're putting functionality in your theme. I don't recommend that. It'll stop working when you switch themes, and is counter to best practices. See my above comment for a better way to do this.
Thanks Colan
Thanks for sharing that simple and useful piece of code. Works beautifully.
Popolo - en webbyrå i Malmö som älskar Drupal
What if I don't want to
What if I don't want to completely remove the current language item,
but, for SEO considerations, only the link on the current language item,
like this :
<ul class="language-switcher-locale-url">
<li class="fr first active francais">Français</li>
<li class="en last english">
<a href="/lilylatifi/en/custom-curtain" class="language-link english active" xml:lang="en" hreflang="en" title="Custom curtains">English</a>
</li>
</ul>
Hide the current language
Well, you could hide it, like I do.
It's a cheap solution, but it works.
This is not an option as SEO
This is not an option as SEO tools don't care about CSS and will still see the link.
The reason of not displaying language switcher link to active page is to avoid over-pushing the url with non relevant content.
Indeed, the content of the language switcher
<a>
tag is the tag "title" i.e. : "french" or "english"... that are not content related terms.Thanks anyway for your answer.