LanguageNegotiationUser.php

Same filename and directory in other branches
  1. 9 core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php
  2. 8.9.x core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php
  3. 10 core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php

Namespace

Drupal\user\Plugin\LanguageNegotiation

File

core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php

View source
<?php

namespace Drupal\user\Plugin\LanguageNegotiation;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\language\Attribute\LanguageNegotiation;
use Drupal\language\LanguageNegotiationMethodBase;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class for identifying language from the user preferences.
 */
class LanguageNegotiationUser extends LanguageNegotiationMethodBase {
    
    /**
     * The language negotiation method id.
     */
    const METHOD_ID = 'language-user';
    
    /**
     * {@inheritdoc}
     */
    public function getLangcode(?Request $request = NULL) {
        $langcode = NULL;
        // User preference (only for authenticated users).
        if ($this->languageManager && $this->currentUser
            ->isAuthenticated()) {
            $preferred_langcode = $this->currentUser
                ->getPreferredLangcode(FALSE);
            $languages = $this->languageManager
                ->getLanguages();
            if (!empty($preferred_langcode) && isset($languages[$preferred_langcode])) {
                $langcode = $preferred_langcode;
            }
        }
        // No language preference from the user.
        return $langcode;
    }

}

Classes

Title Deprecated Summary
LanguageNegotiationUser Class for identifying language from the user preferences.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.