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