Current Language Not Shown

Created on 27 September 2023, over 1 year ago
Updated 9 October 2023, over 1 year ago

Problem/Motivation

At some point during the development of my site, this module stopped showing the current language at the top of its dropdown list. So if I have 6 languages, it would only show the 5 languages that are not the current language.

Steps to reproduce

I am using PHP 8.1 and Drupal 10.1.4.

Proposed resolution

Investigating the problem, I found it to be lines 134-146 of src/Block/DropdownLanguage.php

      $derivative_id = $this->getDerivativeId();
      $current_language = $this->languageManager->getCurrentLanguage($derivative_id)->getId();
      $languageSwitchLinksObject = $this->languageManager->getLanguageSwitchLinks($derivative_id, Url::fromRouteMatch($this->routeMatch));
      $links = ($languageSwitchLinksObject !== NULL) ? $languageSwitchLinksObject->links : [];

      // Place active language ontop of list.
      if (isset($links[$current_language])) {
        $links = [$current_language => $links[$current_language]] + $links;
        // Set an active class for styling.
        $links[$current_language]['attributes']['class'][] = 'active-language';
        // Remove self-referencing link.
        $links[$current_language]['url'] = Url::fromRoute('<nolink>');
      }

Upon investigation, it seems that getLanguageSwitchLinks does not return a link to the current language. As a result, everything within the if() loop is unreachable. At least on my site, this logic doesn't work. I have not changed core drupal functionality to my knowledge, but I'm not sure.

I was able to solve the problem on my site by fixing the logic. The following code assumes that $links will not contain the link and constructs it manually. I don't think this is a solution that would work for all versions of drupal, but it is what I found to solve the issue on my site:

      $derivative_id = $this->getDerivativeId();
      $current_language = $this->languageManager->getCurrentLanguage($derivative_id)->getId();
      $languageSwitchLinksObject = $this->languageManager->getLanguageSwitchLinks($derivative_id, Url::fromRouteMatch($this->routeMatch));
      $links = ($languageSwitchLinksObject !== NULL) ? $languageSwitchLinksObject->links : [];

      if (!isset($links[$current_language])) {
        $links[$current_language] = [
          'url' => Url::fromRoute('<nolink>'),
          'title' => $this->languageManager->getLanguage($current_language)->getName(),
          'language' => $this->languageManager->getLanguage($current_language),
          'attributes' => ['class' => ['active-language']],
          'query' => [],
        ];
        // Ensure the current language is on top of the list.
        $links = [$current_language => $links[$current_language]] + $links;
      }

I'd love to get to the bottom of this. Do you think this is a problem I have somehow accidentally introduced myself, or does this seem to be an actual problem with your module?

🐛 Bug report
Status

Active

Version

4.0

Component

Code

Created by

🇯🇵Japan neptuneDG

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024