Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. LanguageNegotiationSession.php

function LanguageNegotiationSession::getLanguageSwitchLinks

Overrides LanguageSwitcherInterface::getLanguageSwitchLinks

File

core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php, line 159

Class

LanguageNegotiationSession
Identify language from a request/session parameter.

Namespace

Drupal\language\Plugin\LanguageNegotiation

Code

public function getLanguageSwitchLinks(Request $request, $type, Url $url) {
    $links = [];
    $query = [];
    parse_str($request->getQueryString() ?? '', $query);
    $config = $this->config
        ->get('language.negotiation')
        ->get('session');
    $param = $config['parameter'];
    $language_query = $request->getSession()
        ->has($param) ? $request->getSession()
        ->get($param) : $this->languageManager
        ->getCurrentLanguage($type)
        ->getId();
    foreach ($this->languageManager
        ->getNativeLanguages() as $language) {
        $langcode = $language->getId();
        $links[$langcode] = [
            // We need to clone the $url object to avoid using the same one for all
            // links. When the links are rendered, options are set on the $url
            // object, so if we use the same one, they would be set for all links.
'url' => clone $url,
            'title' => $language->getName(),
            'attributes' => [
                'class' => [
                    'language-link',
                ],
            ],
            'query' => $query,
        ];
        if ($language_query != $langcode) {
            $links[$langcode]['query'][$param] = $langcode;
        }
        else {
            $links[$langcode]['attributes']['class'][] = 'session-active';
        }
    }
    return $links;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal