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