Add homepage link to language switcher, when a Node doesn't have a translation
/**
* Add homepage link to language switcher, when a node doesn't have a translation.
*/
function hook_language_switch_links_alter(&$links, $type, $path) {
global $language;
$language_default = language_default('language');
if ($type == LANGUAGE_TYPE_CONTENT && isset($links[$language->language])) {
foreach ($links as &$link) {
// If a certain path doesn't have a link to a translated path, we want to link to the default language path if it exists, or the homepage otherwise.
if (!isset($link['href'])) {
if (isset($links[$language_default]['href'])) {
$link['href'] = $links[$language_default]['href'];
}
else {
$link['href'] = '<front>';
}
// Unset the locale-untranslated class.
$key = array_search('locale-untranslated', $link['attributes']['class']);
if (isset($link['attributes']['class'][$key])) {
unset($link['attributes']['class'][$key]);
}
}
}
}
}