You are here

class LanguageNegotiationUser in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php \Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser
  2. 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

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