Mercurial > hg > isophonics-drupal-site
annotate core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php @ 19:fa3358dc1485 tip
Add ndrum files
author | Chris Cannam |
---|---|
date | Wed, 28 Aug 2019 13:14:47 +0100 |
parents | 129ea1e6d783 |
children |
rev | line source |
---|---|
Chris@0 | 1 <?php |
Chris@0 | 2 |
Chris@0 | 3 namespace Drupal\user\Plugin\LanguageNegotiation; |
Chris@0 | 4 |
Chris@0 | 5 use Drupal\language\LanguageNegotiationMethodBase; |
Chris@0 | 6 use Symfony\Component\HttpFoundation\Request; |
Chris@0 | 7 |
Chris@0 | 8 /** |
Chris@0 | 9 * Class for identifying language from the user preferences. |
Chris@0 | 10 * |
Chris@0 | 11 * @LanguageNegotiation( |
Chris@0 | 12 * id = \Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser::METHOD_ID, |
Chris@0 | 13 * weight = -4, |
Chris@0 | 14 * name = @Translation("User"), |
Chris@0 | 15 * description = @Translation("Follow the user's language preference.") |
Chris@0 | 16 * ) |
Chris@0 | 17 */ |
Chris@0 | 18 class LanguageNegotiationUser extends LanguageNegotiationMethodBase { |
Chris@0 | 19 |
Chris@0 | 20 /** |
Chris@0 | 21 * The language negotiation method id. |
Chris@0 | 22 */ |
Chris@0 | 23 const METHOD_ID = 'language-user'; |
Chris@0 | 24 |
Chris@0 | 25 /** |
Chris@0 | 26 * {@inheritdoc} |
Chris@0 | 27 */ |
Chris@0 | 28 public function getLangcode(Request $request = NULL) { |
Chris@0 | 29 $langcode = NULL; |
Chris@0 | 30 |
Chris@0 | 31 // User preference (only for authenticated users). |
Chris@0 | 32 if ($this->languageManager && $this->currentUser->isAuthenticated()) { |
Chris@17 | 33 $preferred_langcode = $this->currentUser->getPreferredLangcode(FALSE); |
Chris@0 | 34 $languages = $this->languageManager->getLanguages(); |
Chris@17 | 35 if (!empty($preferred_langcode) && isset($languages[$preferred_langcode])) { |
Chris@0 | 36 $langcode = $preferred_langcode; |
Chris@0 | 37 } |
Chris@0 | 38 } |
Chris@0 | 39 |
Chris@0 | 40 // No language preference from the user. |
Chris@0 | 41 return $langcode; |
Chris@0 | 42 } |
Chris@0 | 43 |
Chris@0 | 44 } |