comparison core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\user\Plugin\LanguageNegotiation;
4
5 use Drupal\language\LanguageNegotiationMethodBase;
6 use Symfony\Component\HttpFoundation\Request;
7
8 /**
9 * Class for identifying language from the user preferences.
10 *
11 * @LanguageNegotiation(
12 * id = \Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser::METHOD_ID,
13 * weight = -4,
14 * name = @Translation("User"),
15 * description = @Translation("Follow the user's language preference.")
16 * )
17 */
18 class LanguageNegotiationUser extends LanguageNegotiationMethodBase {
19
20 /**
21 * The language negotiation method id.
22 */
23 const METHOD_ID = 'language-user';
24
25 /**
26 * {@inheritdoc}
27 */
28 public function getLangcode(Request $request = NULL) {
29 $langcode = NULL;
30
31 // User preference (only for authenticated users).
32 if ($this->languageManager && $this->currentUser->isAuthenticated()) {
33 $preferred_langcode = $this->currentUser->getPreferredLangcode();
34 $default_langcode = $this->languageManager->getDefaultLanguage()->getId();
35 $languages = $this->languageManager->getLanguages();
36 if (!empty($preferred_langcode) && $preferred_langcode != $default_langcode && isset($languages[$preferred_langcode])) {
37 $langcode = $preferred_langcode;
38 }
39 }
40
41 // No language preference from the user.
42 return $langcode;
43 }
44
45 }