In Drupal 8, i am trying to create a custom language switcher block. So, I created a custom module, where I created a new class that extends the "LanguageBlock" class of the core language module. I utilize the build() method, that builds alright my custom block. The problem though, is that the resulting links of my custom block, return a pattern like this in the url:

and the language does not change, whereas the default language switcher block works fine. This problem occurs even if the build() method is copy/paste from the parent class, and/or even if I just return parent::build(), with no editing whatsoever. Does anybody have a clue why this is happening, and maybe a possible solution? Thank you.

PS: The code of the build() method.

public function build() {
    $build = array();
    $route_name = $this->pathMatcher->isFrontPage() ? '<front>' : '<current>';
    $type = $this->getDerivativeId();
    $links = $this->languageManager->getLanguageSwitchLinks($type, Url::fromRoute($route_name));

    if (isset($links->links)) {
      $build = array(
        '#theme' => 'links__language_block',
        '#links' => $links->links,
        '#attributes' => array(
          'class' => array(
        '#set_active_class' => TRUE,
    return $build;

1 Answer 1


I finally found what the problem was. This code "$type = $this->getDerivativeId();" was null, and, as a result, this code "$links = $this->languageManager->getLanguageSwitchLinks($type, Url::fromRoute($route_name));" could not build the following part of the $links array:

"[language] => Drupal\language\Entity\ConfigurableLanguage Object
                        [id:protected] => en
                        [label:protected] => English
                        [direction:protected] => ltr
                        [weight:protected] => -10
                        [locked:protected] => 
                        [preSaveMultilingual:protected] => 
                        [originalId:protected] => en
                        [status:protected] => 1
                        [uuid:protected] => 6c14f789-dcf3-4149-883f-63c29df3e544
                        [isSyncing:Drupal\Core\Config\Entity\ConfigEntityBase:private] => 
                        [isUninstalling:Drupal\Core\Config\Entity\ConfigEntityBase:private] => 
                        [langcode:protected] => en
                        [third_party_settings:protected] => Array

                        [_core:protected] => Array
                                [default_config_hash] => HolYKYAaiuzaHd7bV6FOM0HVbmmEMd3Zq4JAcgW55_4

                        [trustedData:protected] => 
                        [entityTypeId:protected] => configurable_language
                        [enforceIsNew:protected] => 
                        [typedData:protected] => 
                        [cacheContexts:protected] => Array
                                [0] => languages:language_interface
                                [1] => url.site

                        [cacheTags:protected] => Array

                        [cacheMaxAge:protected] => -1
                        [_serviceIds:protected] => Array

                        [dependencies:protected] => Array


All the above was missing, so the url could not be properly build. The solution was to add "Derivative" php file for my class, just like the core language module includes both the "core/modules/language/src/Plugin/Block/LanguageBlock.php" and the "core/modules/language/src/Plugin/Derivative/LanguageBlock.php". Of course, you have to fix the machine names and namespaces if you do something similar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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