class LanguageNegotiationUser

Same name and namespace in other branches
  1. 8.9.x core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php \Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser
  2. 10 core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php \Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser
  3. 11.x core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php \Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser

Class for identifying language from the user preferences.

Plugin annotation


@LanguageNegotiation(
  id = \Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser::METHOD_ID,
  weight = -4,
  name = @Translation("User"),
  description = @Translation("Follow the user's language preference.")
)

Hierarchy

Expanded class hierarchy of LanguageNegotiationUser

3 files declare their use of LanguageNegotiationUser
AccountForm.php in core/modules/user/src/AccountForm.php
BookMultilingualTest.php in core/modules/book/tests/src/Kernel/BookMultilingualTest.php
LanguageUILanguageNegotiationTest.php in core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php

File

core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php, line 18

Namespace

Drupal\user\Plugin\LanguageNegotiation
View source
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;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
LanguageNegotiationMethodBase::$config protected property The configuration factory.
LanguageNegotiationMethodBase::$currentUser protected property The current active user.
LanguageNegotiationMethodBase::$languageManager protected property The language manager.
LanguageNegotiationMethodBase::persist public function Notifies the plugin that the language code it returned has been accepted. Overrides LanguageNegotiationMethodInterface::persist 1
LanguageNegotiationMethodBase::setConfig public function Injects the configuration factory. Overrides LanguageNegotiationMethodInterface::setConfig
LanguageNegotiationMethodBase::setCurrentUser public function Injects the current user. Overrides LanguageNegotiationMethodInterface::setCurrentUser
LanguageNegotiationMethodBase::setLanguageManager public function Injects the language manager. Overrides LanguageNegotiationMethodInterface::setLanguageManager
LanguageNegotiationUser::getLangcode public function Performs language negotiation. Overrides LanguageNegotiationMethodInterface::getLangcode
LanguageNegotiationUser::METHOD_ID constant The language negotiation method id.

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