You are here

class LanguageNegotiationUser in Drupal 8

Same name and namespace in other branches
  1. 9 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

2 files declare their use of LanguageNegotiationUser
AccountForm.php in core/modules/user/src/AccountForm.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

Namesort descending Modifiers Type Description 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.