Skip to content

Instantly share code, notes, and snippets.

@NikLP
Last active June 14, 2022 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikLP/d97072946171578637815bedf9517233 to your computer and use it in GitHub Desktop.
Save NikLP/d97072946171578637815bedf9517233 to your computer and use it in GitHub Desktop.
MYTHEME.theme snippet for language switcher block in Drupal 8+
<?php
// Changes the language switcher block to use country codes instead of long format country names
// So if the site was in English/Italian, the original switcher would show (at D9 iirc):
// English - Italian, and this code makes it into EN - IT (lack of correct formatting notwithstanding)
// This could easily be adapted to show flags etc instead I imagine.
/**
* Implements hook_preprocess_block().
*/
function mytheme_preprocess_block(&$variables) {
if ($variables['elements']['#id'] == 'languageswitcher') {
$links = &$variables['content']['#links'];
foreach ($links as $lang => $data) {
$variables['content']['#links'][$lang]['title'] = $lang;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment