/** * Return available page translation information. * * Be careful, for static routes Roadiz will generate a localized * route identifier suffixed with "Locale" text. In case of "force_locale" * setting to true, Roadiz will always use suffixed route. * * ## example return value * * array (size=3) * 'en' => * array (size=4) * 'name' => string 'newsPage' * 'url' => string 'http://localhost/news/test' * 'locale' => string 'en' * 'active' => boolean false * 'translation' => string 'English' * 'fr' => * array (size=4) * 'name' => string 'newsPageLocale' * 'url' => string 'http://localhost/fr/news/test' * 'locale' => string 'fr' * 'active' => boolean true * 'translation' => string 'French' * 'es' => * array (size=4) * 'name' => string 'newsPageLocale' * 'url' => string 'http://localhost/es/news/test' * 'locale' => string 'es' * 'active' => boolean false * 'translation' => string 'Spanish' * * @param Request $request * @param boolean $absolute Generate absolute url or relative paths * * @return $this */ public function getTranslationMenuAssignation(Request $request, $absolute = false) { $attr = $request->attributes->all(); $query = $request->query->all(); $name = ""; $forceLocale = (bool) SettingsBag::get('force_locale'); if (in_array("node", array_keys($attr), true)) { $node = $attr["node"]; } else { $node = null; } if ($node === null && !empty($attr["_route"])) { $translations = Kernel::getService('em')->getRepository("RZ\\Roadiz\\Core\\Entities\\Translation")->findAllAvailable(); $attr["_route"] = RouteHandler::getBaseRoute($attr["_route"]); } elseif (null !== $node) { $translations = $node->getHandler()->getAvailableTranslations(); $translations = array_filter($translations, function (Translation $trans) { if ($trans->isAvailable()) { return true; } return false; }); $name = "node"; } else { return []; } $return = []; foreach ($translations as $translation) { $url = null; if ($node) { $urlGenerator = new NodesSourcesUrlGenerator($request, $node->getHandler()->getNodeSourceByTranslation($translation), $forceLocale); $url = $urlGenerator->getUrl($absolute); if (!empty($query)) { $url .= "?" . http_build_query($query); } } elseif (!empty($attr["_route"])) { /* * Use suffixed route if locales are forced or * if it’s not default translation. */ if (true === $forceLocale || !$translation->isDefaultTranslation()) { $name = $attr["_route"] . "Locale"; $attr["_route_params"]["_locale"] = $translation->getLocale(); } else { $name = $attr["_route"]; if (in_array("_locale", array_keys($attr["_route_params"]), true)) { unset($attr["_route_params"]["_locale"]); } } $url = Kernel::getService("urlGenerator")->generate($name, array_merge($attr["_route_params"], $query), $absolute); } if (null !== $url) { $return[$translation->getLocale()] = ['name' => $name, 'url' => $url, 'locale' => $translation->getLocale(), 'active' => $this->translation == $translation ? true : false, 'translation' => $translation->getName()]; } } return $return; }
/** * Get nodeSource url using cache. * * @param NodesSources $ns * @param array $criteria * @return string */ public function getNodesSourceUrl(NodesSources $ns, array $criteria = []) { $absolute = false; if (isset($criteria['absolute'])) { $absolute = (bool) $criteria['absolute']; } $cacheKey = $this->getCacheKey($ns, $absolute); if ($this->cacheProvider->contains($cacheKey)) { return $this->cacheProvider->fetch($cacheKey); } else { $urlGenerator = new NodesSourcesUrlGenerator($this->request, $ns, $this->forceLocale); $url = $urlGenerator->getUrl($absolute); $this->cacheProvider->save($cacheKey, $url); return $url; } }
/** * @dataProvider getUrlProvider */ public function testGetUrl($nodeSource, $expectedUrl) { $generator = new NodesSourcesUrlGenerator(Kernel::getInstance()->getRequest(), $nodeSource); $this->assertEquals($generator->getUrl(), $expectedUrl); }