d37e9bcd7049efe1292b0f2c0e8daf137dce3ad1
[yaffs-website] / web / core / modules / language / src / Plugin / LanguageNegotiation / LanguageNegotiationUrlFallback.php
1 <?php
2
3 namespace Drupal\language\Plugin\LanguageNegotiation;
4
5 use Drupal\language\LanguageNegotiationMethodBase;
6 use Symfony\Component\HttpFoundation\Request;
7
8 /**
9  * Class that determines the language to be assigned to URLs when none is
10  * detected.
11  *
12  * The language negotiation process has a fallback chain that ends with the
13  * default language negotiation method. Each built-in language type has a
14  * separate initialization:
15  * - Interface language, which is the only configurable one, always gets a valid
16  *   value. If no request-specific language is detected, the default language
17  *   will be used.
18  * - Content language merely inherits the interface language by default.
19  * - URL language is detected from the requested URL and will be used to rewrite
20  *   URLs appearing in the page being rendered. If no language can be detected,
21  *   there are two possibilities:
22  *   - If the default language has no configured path prefix or domain, then the
23  *     default language is used. This guarantees that (missing) URL prefixes are
24  *     preserved when navigating through the site.
25  *   - If the default language has a configured path prefix or domain, a
26  *     requested URL having an empty prefix or domain is an anomaly that must be
27  *     fixed. This is done by introducing a prefix or domain in the rendered
28  *     page matching the detected interface language.
29  *
30  * @LanguageNegotiation(
31  *   id = Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrlFallback::METHOD_ID,
32  *   types = {Drupal\Core\Language\LanguageInterface::TYPE_URL},
33  *   weight = 8,
34  *   name = @Translation("URL fallback"),
35  *   description = @Translation("Use an already detected language for URLs if none is found.")
36  * )
37  */
38 class LanguageNegotiationUrlFallback extends LanguageNegotiationMethodBase {
39
40   /**
41    * The language negotiation method id.
42    */
43   const METHOD_ID = 'language-url-fallback';
44
45   /**
46    * {@inheritdoc}
47    */
48   public function getLangcode(Request $request = NULL) {
49     $langcode = NULL;
50
51     if ($this->languageManager) {
52       $default = $this->languageManager->getDefaultLanguage();
53       $config = $this->config->get('language.negotiation')->get('url');
54       $prefix = ($config['source'] == LanguageNegotiationUrl::CONFIG_PATH_PREFIX);
55
56       // If the default language is not configured to convey language
57       // information, a missing URL language information indicates that URL
58       // language should be the default one, otherwise we fall back to an
59       // already detected language.
60       if (($prefix && empty($config['prefixes'][$default->getId()])) || (!$prefix && empty($config['domains'][$default->getId()]))) {
61         $langcode = $default->getId();
62       }
63       else {
64         $langcode = $this->languageManager->getCurrentLanguage()->getId();
65       }
66     }
67
68     return $langcode;
69   }
70
71 }