From 8880cf89da3f073c482e32efe46a4166202a322d Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Thu, 26 Oct 2023 17:43:29 +0200 Subject: [PATCH] IBX-6504: Gracefully handled URL generation in `RoutingExtension` For more details see https://issues.ibexa.co/browse/IBX-6504 and https://github.com/ezsystems/ezplatform-kernel/pull/389. Key changes: * Handled a case when a translated URL alias taken from `ez_path` used in a view used for preview is not available yet - an empty string is returned. That occurs for previews of translated content item when an item is not always available. * Loaded Location in all Languages in preview context. --- .../Converter/LocationParamConverter.php | 19 ++++++++++++++++--- .../Resources/config/services.yml | 3 ++- .../Converter/LocationParamConverterTest.php | 7 ++++++- .../Twig/Extension/RoutingExtension.php | 14 ++++++++++++-- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/eZ/Bundle/EzPublishCoreBundle/Converter/LocationParamConverter.php b/eZ/Bundle/EzPublishCoreBundle/Converter/LocationParamConverter.php index 65dac4d7db..26524099fd 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Converter/LocationParamConverter.php +++ b/eZ/Bundle/EzPublishCoreBundle/Converter/LocationParamConverter.php @@ -7,15 +7,22 @@ namespace eZ\Bundle\EzPublishCoreBundle\Converter; use eZ\Publish\API\Repository\LocationService; +use eZ\Publish\API\Repository\Values\Content\Language; +use eZ\Publish\API\Repository\Values\Content\Location; +use eZ\Publish\Core\Helper\ContentPreviewHelper; class LocationParamConverter extends RepositoryParamConverter { /** @var \eZ\Publish\API\Repository\LocationService */ private $locationService; - public function __construct(LocationService $locationService) + /** @var \eZ\Publish\Core\Helper\ContentPreviewHelper */ + private $contentPreviewHelper; + + public function __construct(LocationService $locationService, ContentPreviewHelper $contentPreviewHelper) { $this->locationService = $locationService; + $this->contentPreviewHelper = $contentPreviewHelper; } protected function getSupportedClass() @@ -28,8 +35,14 @@ protected function getPropertyName() return 'locationId'; } - protected function loadValueObject($id) + /** + * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException + * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException + */ + protected function loadValueObject($id): Location { - return $this->locationService->loadLocation($id); + $prioritizedLanguages = $this->contentPreviewHelper->isPreviewActive() ? Language::ALL : null; + + return $this->locationService->loadLocation($id, $prioritizedLanguages); } } diff --git a/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml b/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml index a7c34b72c5..967c64fbb7 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml +++ b/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml @@ -191,7 +191,8 @@ services: ezpublish.param_converter.location: class: eZ\Bundle\EzPublishCoreBundle\Converter\LocationParamConverter arguments: - - "@ezpublish.siteaccessaware.service.location" + $locationService: '@ezpublish.siteaccessaware.service.location' + $contentPreviewHelper: '@ezpublish.content_preview_helper' tags: - { name: request.param_converter, priority: "%ezpublish.param_converter.location.priority%", converter: ez_location_converter } diff --git a/eZ/Bundle/EzPublishCoreBundle/Tests/Converter/LocationParamConverterTest.php b/eZ/Bundle/EzPublishCoreBundle/Tests/Converter/LocationParamConverterTest.php index 09e828fa89..58bb42e6e1 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Tests/Converter/LocationParamConverterTest.php +++ b/eZ/Bundle/EzPublishCoreBundle/Tests/Converter/LocationParamConverterTest.php @@ -9,6 +9,7 @@ use eZ\Bundle\EzPublishCoreBundle\Converter\LocationParamConverter; use eZ\Publish\API\Repository\LocationService; use eZ\Publish\API\Repository\Values\Content\Location; +use eZ\Publish\Core\Helper\ContentPreviewHelper; use Symfony\Component\HttpFoundation\Request; class LocationParamConverterTest extends AbstractParamConverterTest @@ -22,11 +23,15 @@ class LocationParamConverterTest extends AbstractParamConverterTest private $locationServiceMock; + /** @var \eZ\Publish\Core\Helper\ContentPreviewHelper&\PHPUnit\Framework\MockObject\MockObject */ + private $contentPreviewHelperMock; + protected function setUp(): void { $this->locationServiceMock = $this->createMock(LocationService::class); + $this->contentPreviewHelperMock = $this->createMock(ContentPreviewHelper::class); - $this->converter = new LocationParamConverter($this->locationServiceMock); + $this->converter = new LocationParamConverter($this->locationServiceMock, $this->contentPreviewHelperMock); } public function testSupports() diff --git a/eZ/Publish/Core/MVC/Symfony/Templating/Twig/Extension/RoutingExtension.php b/eZ/Publish/Core/MVC/Symfony/Templating/Twig/Extension/RoutingExtension.php index 49409f425a..689b9469a4 100644 --- a/eZ/Publish/Core/MVC/Symfony/Templating/Twig/Extension/RoutingExtension.php +++ b/eZ/Publish/Core/MVC/Symfony/Templating/Twig/Extension/RoutingExtension.php @@ -8,6 +8,7 @@ namespace eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension; +use eZ\Publish\API\Repository\Exceptions\NotFoundException; use eZ\Publish\API\Repository\Values\Content\Content; use eZ\Publish\API\Repository\Values\Content\ContentInfo; use eZ\Publish\API\Repository\Values\Content\Location; @@ -78,14 +79,23 @@ public function getPath(object $name, array $parameters = [], bool $relative = f { $referenceType = $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH; - return $this->generateUrlForObject($name, $parameters, $referenceType); + return $this->tryGeneratingUrlForObject($name, $parameters, $referenceType); } 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); + return $this->tryGeneratingUrlForObject($name, $parameters, $referenceType); + } + + private function tryGeneratingUrlForObject(object $object, array $parameters, int $referenceType): string + { + try { + return $this->generateUrlForObject($object, $parameters, $referenceType); + } catch (NotFoundException $e) { + return ''; + } } private function generateUrlForObject(object $object, array $parameters, int $referenceType): string