class LanguageNegotiationUrlFallback

Same name in other branches
  1. 8.9.x core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationUrlFallback.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrlFallback
  2. 10 core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationUrlFallback.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrlFallback
  3. 11.x core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationUrlFallback.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrlFallback

Determines the language to be assigned to URLs when none is detected.

The language negotiation process has a fallback chain that ends with the default language negotiation method. Each built-in language type has a separate initialization:

  • Interface language, which is the only configurable one, always gets a valid value. If no request-specific language is detected, the default language will be used.
  • Content language merely inherits the interface language by default.
  • URL language is detected from the requested URL and will be used to rewrite URLs appearing in the page being rendered. If no language can be detected, there are two possibilities:

    • If the default language has no configured path prefix or domain, then the default language is used. This guarantees that (missing) URL prefixes are preserved when navigating through the site.
    • If the default language has a configured path prefix or domain, a requested URL having an empty prefix or domain is an anomaly that must be fixed. This is done by introducing a prefix or domain in the rendered page matching the detected interface language.

Plugin annotation


@LanguageNegotiation(
  id = Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrlFallback::METHOD_ID,
  types = {Drupal\Core\Language\LanguageInterface::TYPE_URL},
  weight = 8,
  name = @Translation("URL fallback"),
  description = @Translation("Use an already detected language for URLs if none is found.")
)

Hierarchy

Expanded class hierarchy of LanguageNegotiationUrlFallback

1 file declares its use of LanguageNegotiationUrlFallback
language.module in core/modules/language/language.module
Add language handling functionality to Drupal.

File

core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationUrlFallback.php, line 37

Namespace

Drupal\language\Plugin\LanguageNegotiation
View source
class LanguageNegotiationUrlFallback extends LanguageNegotiationMethodBase {
    
    /**
     * The language negotiation method id.
     */
    const METHOD_ID = 'language-url-fallback';
    
    /**
     * {@inheritdoc}
     */
    public function getLangcode(Request $request = NULL) {
        $langcode = NULL;
        if ($this->languageManager) {
            $default = $this->languageManager
                ->getDefaultLanguage();
            $config = $this->config
                ->get('language.negotiation')
                ->get('url');
            $prefix = $config['source'] == LanguageNegotiationUrl::CONFIG_PATH_PREFIX;
            // If the default language is not configured to convey language
            // information, a missing URL language information indicates that URL
            // language should be the default one, otherwise we fall back to an
            // already detected language.
            if ($prefix && empty($config['prefixes'][$default->getId()]) || !$prefix && empty($config['domains'][$default->getId()])) {
                $langcode = $default->getId();
            }
            else {
                $langcode = $this->languageManager
                    ->getCurrentLanguage()
                    ->getId();
            }
        }
        return $langcode;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
LanguageNegotiationMethodBase::$config protected property The configuration factory.
LanguageNegotiationMethodBase::$currentUser protected property The current active user.
LanguageNegotiationMethodBase::$languageManager protected property The language manager.
LanguageNegotiationMethodBase::persist public function Notifies the plugin that the language code it returned has been accepted. Overrides LanguageNegotiationMethodInterface::persist 1
LanguageNegotiationMethodBase::setConfig public function Injects the configuration factory. Overrides LanguageNegotiationMethodInterface::setConfig
LanguageNegotiationMethodBase::setCurrentUser public function Injects the current user. Overrides LanguageNegotiationMethodInterface::setCurrentUser
LanguageNegotiationMethodBase::setLanguageManager public function Injects the language manager. Overrides LanguageNegotiationMethodInterface::setLanguageManager
LanguageNegotiationUrlFallback::getLangcode public function Performs language negotiation. Overrides LanguageNegotiationMethodInterface::getLangcode
LanguageNegotiationUrlFallback::METHOD_ID constant The language negotiation method id.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.