Skip to content

Commit

Permalink
IBX-6504: Used forcedLanguage parameter in RoutingExtension durin…
Browse files Browse the repository at this point in the history
…g preview
  • Loading branch information
barw4 committed Oct 19, 2023
1 parent 336f0a6 commit 87489f2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ services:

ezpublish.templating.extension.routing:
class: eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\RoutingExtension
arguments: ["@ezpublish.route_reference.generator", "@router"]
arguments:
$routeReferenceGenerator: "@ezpublish.route_reference.generator"
$urlGenerator: "@router"
$contentPreviewHelper: "@ezpublish.content_preview_helper"
$locationService: "@ezpublish.api.service.location"
tags:
- {name: twig.extension}

Expand Down
5 changes: 4 additions & 1 deletion eZ/Publish/Core/MVC/Symfony/Routing/UrlAliasRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ public function generate(string $name, array $parameters = [], int $referenceTyp
);
}

$location = isset($parameters['location']) ? $parameters['location'] : $this->locationService->loadLocation($parameters['locationId']);
$location = $parameters['location'] ?? $this->locationService->loadLocation(
$parameters['locationId'],
isset($parameters['forcedLanguage']) ? [$parameters['forcedLanguage']] : null
);
unset($parameters['location'], $parameters['locationId'], $parameters['viewType'], $parameters['layout']);

return $this->generator->generate($location, $parameters, $referenceType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

namespace eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension;

use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\Core\Helper\ContentPreviewHelper;
use eZ\Publish\Core\MVC\Symfony\Routing\Generator\RouteReferenceGeneratorInterface;
use eZ\Publish\Core\MVC\Symfony\Routing\RouteReference;
use eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter;
Expand All @@ -30,12 +33,22 @@ class RoutingExtension extends AbstractExtension
/** @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface */
private $urlGenerator;

/** @var \eZ\Publish\Core\Helper\ContentPreviewHelper */
private $contentPreviewHelper;

/** @var \eZ\Publish\API\Repository\LocationService */
private $locationService;

public function __construct(
RouteReferenceGeneratorInterface $routeReferenceGenerator,
UrlGeneratorInterface $urlGenerator
UrlGeneratorInterface $urlGenerator,
ContentPreviewHelper $contentPreviewHelper,
LocationService $locationService
) {
$this->routeReferenceGenerator = $routeReferenceGenerator;
$this->urlGenerator = $urlGenerator;
$this->contentPreviewHelper = $contentPreviewHelper;
$this->locationService = $locationService;
}

public function getFunctions(): array
Expand Down Expand Up @@ -74,31 +87,42 @@ public function getRouteReference($resource = null, $params = []): RouteReferenc
return $this->routeReferenceGenerator->generate($resource, $params);
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function getPath(object $name, array $parameters = [], bool $relative = false): string
{
$referenceType = $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH;

return $this->generateUrlForObject($name, $parameters, $referenceType);
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function getUrl(object $name, array $parameters = [], bool $schemeRelative = false): string
{
$referenceType = $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL;

return $this->generateUrlForObject($name, $parameters, $referenceType);
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
private function generateUrlForObject(object $object, array $parameters, int $referenceType): string
{
if ($object instanceof Location) {
$routeName = UrlAliasRouter::URL_ALIAS_ROUTE_NAME;
$parameters += [
'locationId' => $object->id,
'forcedLanguage' => $this->getForcedLanguageCodeBasedOnPreview(),
];
} elseif ($object instanceof Content || $object instanceof ContentInfo) {
$routeName = UrlAliasRouter::URL_ALIAS_ROUTE_NAME;
$parameters += [
'contentId' => $object->id,
'forcedLanguage' => $this->getForcedLanguageCodeBasedOnPreview(),
];
} elseif ($object instanceof RouteReference) {
$routeName = $object->getRoute();
Expand All @@ -113,6 +137,38 @@ private function generateUrlForObject(object $object, array $parameters, int $re
return $this->urlGenerator->generate($routeName, $parameters, $referenceType);
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
private function getForcedLanguageCodeBasedOnPreview(): ?string
{
if ($this->contentPreviewHelper->isPreviewActive() === false) {
return null;
}

$previewedContent = $this->contentPreviewHelper->getPreviewedContent();
$versionInfo = $previewedContent->getVersionInfo();
$contentInfo = $versionInfo->getContentInfo();
$alwaysAvailable = $versionInfo->getContentInfo()->alwaysAvailable;
if ($alwaysAvailable) {
return null;
}

$previewedLocation = $this->contentPreviewHelper->getPreviewedLocation();
try {
$this->locationService->loadLocation(
$previewedLocation->id,
[$versionInfo->initialLanguageCode],
true
);

return null;
} catch (NotFoundException $e) {
// Use initial language as a forced language
return $contentInfo->getMainLanguageCode();
}
}

/**
* Determines at compile time whether the generated URL will be safe and thus
* saving the unneeded automatic escaping for performance reasons.
Expand Down

0 comments on commit 87489f2

Please sign in to comment.