Only show links to existing translations in language switcher block - when using Entity Translation.

Public

</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_language_switch_links_alter
  3.  *
  4.  * Unset lang-switcher links for non-existent translations.
  5.  */
  6. function MY_MODULE_language_switch_links_alter(array &$links, $type, $path) {
  7. $node = menu_get_object();
  8. if ($node) {
  9. // Unset lang-switcher links for non-existent translations
  10. foreach ($links as $langcode => &$link) {
  11. if (!isset($node->translations->data[$langcode])) {
  12. unset($links[$langcode]);
  13. }
  14. $link['attributes']['hreflang'] = $langcode;
  15. }
  16. }
  17. }

Comments

typotraum's picture

If you don't want to hide the language-links, maybe you add a class:

  1. // unset($links[$langcode]);
  2. $link['attributes']['class'][] = 'language-link-disabled';

Thanks valderama!

Tachion's picture

If you don't want to hide the language-links and you don't want the language-links to point to anything do everything above and add
unset($link['href']);