I have a two-language site with the standard language switcher block. I want to remove the link that points to the current page language (I only want the user to see one link to switch to the language they're not seeing).

I've tried this:

function mymodule_block_view_language_block_alter(&$build, $block) {
  $build['links'] = []; //for now for debugging purposes, remove all links

It doesn't work (though it runs the code). The $build array doesn't have a links key, or anything it's supposed to according to the build method in core/modules/language/src/Plugin/Block/LanguageBlock.php. Why might this happen? How can I alter what I'm seeing there?

BTW I also tried the block_build_alter hook. Similar situation.


Those hooks are unfortunately not very useful; I don't really know why they work like that. The actual block view didn't happen yet; that's in a preprocess. You'd need to add another preprocess that runs later.

However, for your specific use case, why not use the hook that's designed exactly for this? hook_language_switch_links_alter()?

| improve this answer | |
  • I'll try that one, looks like exactly what I need. What would be the preprocess hook I'd use if that didn't work? And, do you know the purpose of the block hooks? – cambraca Feb 1 '16 at 23:00
  • BTW the language module hook worked perfectly, it even allowed me to change the label by editing $links[$language_id]['title'] (which I forgot to mention on the original question). – cambraca Feb 1 '16 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.