Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. LocalizedRouteTrait.php

trait LocalizedRouteTrait

@internal

@author Nicolas Grekas <p@tchwork.com> @author Jules Pietri <jules@heahprod.com>

Hierarchy

  • trait \Symfony\Component\Routing\Loader\Configurator\Traits\LocalizedRouteTrait
2 files declare their use of LocalizedRouteTrait
XmlFileLoader.php in vendor/symfony/routing/Loader/XmlFileLoader.php
YamlFileLoader.php in vendor/symfony/routing/Loader/YamlFileLoader.php

File

vendor/symfony/routing/Loader/Configurator/Traits/LocalizedRouteTrait.php, line 23

Namespace

Symfony\Component\Routing\Loader\Configurator\Traits
View source
trait LocalizedRouteTrait {
    
    /**
     * Creates one or many routes.
     *
     * @param string|array $path the path, or the localized paths of the route
     */
    protected final function createLocalizedRoute(RouteCollection $collection, string $name, string|array $path, string $namePrefix = '', ?array $prefixes = null) : RouteCollection {
        $paths = [];
        $routes = new RouteCollection();
        if (\is_array($path)) {
            if (null === $prefixes) {
                $paths = $path;
            }
            elseif ($missing = array_diff_key($prefixes, $path)) {
                throw new \LogicException(\sprintf('Route "%s" is missing routes for locale(s) "%s".', $name, implode('", "', array_keys($missing))));
            }
            else {
                foreach ($path as $locale => $localePath) {
                    if (!isset($prefixes[$locale])) {
                        throw new \LogicException(\sprintf('Route "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $name, $locale));
                    }
                    $paths[$locale] = $prefixes[$locale] . $localePath;
                }
            }
        }
        elseif (null !== $prefixes) {
            foreach ($prefixes as $locale => $prefix) {
                $paths[$locale] = $prefix . $path;
            }
        }
        else {
            $routes->add($namePrefix . $name, $route = $this->createRoute($path));
            $collection->add($namePrefix . $name, $route);
            return $routes;
        }
        foreach ($paths as $locale => $path) {
            $routes->add($name . '.' . $locale, $route = $this->createRoute($path));
            $collection->add($namePrefix . $name . '.' . $locale, $route);
            $route->setDefault('_locale', $locale);
            $route->setRequirement('_locale', preg_quote($locale));
            $route->setDefault('_canonical_route', $namePrefix . $name);
        }
        return $routes;
    }
    private function createRoute(string $path) : Route {
        return new Route($path);
    }

}

Members

Title Sort descending Modifiers Object type Summary
LocalizedRouteTrait::createLocalizedRoute final protected function Creates one or many routes.
LocalizedRouteTrait::createRoute private function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal