There are more than one ways available to alter the language item link in the language switcher block. We will see what are the ways in the below,
- Using hook_preprocess_links__language_block() in the theme file
- Using hook_language_switch_links_alter() in the module file
- Using contrib module “Disable Language.
hook_preprocess_links__language_block()
Suppose if we want to remove the english language from the drop down, then here is the code, we can alter this code as per our needs.
function cherry_preprocess_links__language_block(&$variables) {
foreach ($variables[‘links’] as $i => $link) {
$linkLanguage = $link[‘link’][‘#options’][‘language’];
if($linkLanguage->get(‘id’) == ‘en’) {
unset($variables[‘links’][$i]);
}
}
}
Using hook_language_switch_links_alter()
Suppose if we want to unset the current language code, then here is the code. We can alter this hook as per our needs.
function my_module_language_switch_links_alter(&$variables) {
foreach ($variables as $i => $link) {
$linkLanguage = $link[‘language’];
if ($currentLanguageCode == $linkLanguage->get(‘id’)) {
unset($variables[$i]);
}
}
}
Using these hooks, we can set active class to the current language item link or to add any extra attributes to pass to twig file.