From c433560a8ccdbcc8542fb194a6384305ff2a6865 Mon Sep 17 00:00:00 2001 From: Kacper Jurak Date: Fri, 29 Jul 2016 10:01:10 +0200 Subject: [PATCH] Generate link from same name based on locale --- Entity/RouteRepository.php | 16 ++++++++++++++-- Routing/Base/DynamicRouter.php | 5 +++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Entity/RouteRepository.php b/Entity/RouteRepository.php index 13296cb..266ac08 100644 --- a/Entity/RouteRepository.php +++ b/Entity/RouteRepository.php @@ -111,9 +111,21 @@ public function findRouteByUrl($url) /** * {@inheritDoc} */ - public function getRouteByName($name, $parameters = array()) + public function getRouteByName($name, $parameters = array(), $config = array()) { - $route = $this->findOneByName($name); + $locale = $config['locale']; + $defaultLocale = $config['defaultLocale']; + + $route = $this->findOneBy(array('name' => $name, 'locale' => $locale)); + + if (!$route) { + $route = $this->findOneBy(array('name' => $name, 'locale' => substr($locale, 0, 2))); + } + + if (!$route) { + $route = $this->findOneBy(array('name' => $name, 'locale' => $defaultLocale)); + } + if (!$route) { throw new RouteNotFoundException("No route found for name '$name'"); } diff --git a/Routing/Base/DynamicRouter.php b/Routing/Base/DynamicRouter.php index 2267da6..8c145c8 100644 --- a/Routing/Base/DynamicRouter.php +++ b/Routing/Base/DynamicRouter.php @@ -121,8 +121,9 @@ public function generate($name, $parameters = array(), $absolute = false) $route = $parameters['route']; unset($parameters['route']); } elseif ($name) { - - $route = $this->routeRepository->getRouteByName($name, $parameters); + $config['locale'] = $this->getLocale($parameters); + $config['defaultLocale'] = $this->container->getParameter('locale'); + $route = $this->routeRepository->getRouteByName($name, $parameters, $config); } else { $route = $this->getRouteFromContent($parameters); unset($parameters['route']); // could be an empty string