/** * {@inheritdoc} */ public function resolve(RequestAnalyzerInterface $requestAnalyzer) { // determine default locale (if one exists) $defaultLocalization = $requestAnalyzer->getPortal()->getDefaultLocalization(); $defaultLocale = $defaultLocalization ? $defaultLocalization->getLocalization() : null; return ['request' => ['webspaceKey' => $requestAnalyzer->getWebspace()->getKey(), 'defaultLocale' => $defaultLocale, 'locale' => $requestAnalyzer->getCurrentLocalization()->getLocalization(), 'portalUrl' => $requestAnalyzer->getPortalUrl(), 'resourceLocatorPrefix' => $requestAnalyzer->getResourceLocatorPrefix(), 'resourceLocator' => $requestAnalyzer->getResourceLocator(), 'get' => $requestAnalyzer->getGetParameters(), 'post' => $requestAnalyzer->getPostParameters(), 'analyticsKey' => $requestAnalyzer->getAnalyticsKey()]]; }
/** * Returns alternate link HTML tags with href-lang attributes. * * @param array $urls * * @return string * * @deprecated since 1.1 use SeoTwigExtension::renderSeoTags - sulu_seo */ public function getAlternateLinks($urls) { // determine default and current values $webspaceKey = $this->requestAnalyzer->getWebspace()->getKey(); $currentPortal = $this->requestAnalyzer->getPortal(); $defaultLocale = null; if ($currentPortal !== null && ($defaultLocale = $currentPortal->getXDefaultLocalization()) !== null) { $defaultLocale = $defaultLocale->getLocalization(); } $result = []; foreach ($urls as $locale => $url) { // url = '/' means that there is no translation for this page // the only exception is the homepage where the requested resource-locator is '/' if ($url !== '/' || $this->requestAnalyzer->getResourceLocator() === '/') { if ($locale === $defaultLocale) { $result[] = $this->getAlternate($url, $webspaceKey, $locale, true); } $result[] = $this->getAlternate($url, $webspaceKey, $locale); } } return implode(PHP_EOL, $result); }
/** * @dataProvider provideSeoData */ public function testRenderSeoTags($seoExtension, $content, $urls, $defaultLocale, $shadowBaseLocale, $xDefaultLocale, $expectedResults, $unexpectedResults = [], $resourceLocator = '/test') { /** @var Localization $localization */ $localization = $this->prophesize(Localization::class); $localization->getLocalization()->willReturn($xDefaultLocale ?: $defaultLocale); /** @var Portal $portal */ $portal = $this->prophesize(Portal::class); $portal->getXDefaultLocalization()->willReturn($localization->reveal()); $this->requestAnalyzer->getPortal()->willReturn($portal->reveal()); $this->requestAnalyzer->getResourceLocator()->willReturn($resourceLocator); $webspace = $this->prophesize(Webspace::class); $this->requestAnalyzer->getWebspace()->willReturn($webspace); $this->contentPath->getContentPath(Argument::cetera())->will(function ($arguments) { return '/' . str_replace('_', '-', $arguments[2]) . $arguments[0]; }); $result = $this->seoTwigExtension->renderSeoTags($seoExtension, $content, $urls, $shadowBaseLocale); foreach ($expectedResults as $expectedResult) { $this->assertContains($expectedResult, $result); } foreach ($unexpectedResults as $unexpectedResult) { $this->assertNotContains($unexpectedResult, $result); } }
/** * Renders the alternate links for this page, this means all the localizations in which this page is available. In * addition the default localization is also rendered. * * @param string[] $urls All the localized URLs for the current page * @param string $webspaceKey The key of the current webspace * * @return string The rendered HTML tags */ private function renderAlternateLinks(array $urls, $webspaceKey) { $html = ''; $defaultLocale = null; $portal = $this->requestAnalyzer->getPortal(); if ($portal) { $defaultLocale = $portal->getXDefaultLocalization()->getLocalization(); } foreach ($urls as $locale => $url) { // url = '/' means that there is no translation for this page // the only exception is the homepage where the requested resource-locator is false if ($url !== '/' || $this->requestAnalyzer->getResourceLocator() === false) { if ($defaultLocale === $locale) { $html .= $this->renderAlternateLink($url, $webspaceKey, $locale, true); } $html .= $this->renderAlternateLink($url, $webspaceKey, $locale); } } return $html; }
/** * Checks if the resource locator is valid. * A resource locator with a slash only is not allowed, the only exception is when it is a single language * website, where the browser automatically adds the slash. * * @return bool */ private function checkResourceLocator() { return !($this->requestAnalyzer->getResourceLocator() === '/' && $this->requestAnalyzer->getResourceLocatorPrefix()); }