diff --git a/src/bundle/Core/Converter/LocationParamConverter.php b/src/bundle/Core/Converter/LocationParamConverter.php index de30aeefa0..8bcbc9e053 100644 --- a/src/bundle/Core/Converter/LocationParamConverter.php +++ b/src/bundle/Core/Converter/LocationParamConverter.php @@ -7,16 +7,23 @@ namespace Ibexa\Bundle\Core\Converter; use Ibexa\Contracts\Core\Repository\LocationService; +use Ibexa\Contracts\Core\Repository\Values\Content\Language; use Ibexa\Contracts\Core\Repository\Values\Content\Location; +use Ibexa\Core\Helper\ContentPreviewHelper; class LocationParamConverter extends RepositoryParamConverter { /** @var \Ibexa\Contracts\Core\Repository\LocationService */ private $locationService; - public function __construct(LocationService $locationService) - { + private ContentPreviewHelper $contentPreviewHelper; + + public function __construct( + LocationService $locationService, + ContentPreviewHelper $contentPreviewHelper + ) { $this->locationService = $locationService; + $this->contentPreviewHelper = $contentPreviewHelper; } protected function getSupportedClass() @@ -29,9 +36,15 @@ protected function getPropertyName() return 'locationId'; } - protected function loadValueObject($id) + /** + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + * @throws \Ibexa\Contracts\Core\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/src/bundle/Core/Resources/config/services.yml b/src/bundle/Core/Resources/config/services.yml index c3c6cf156c..fdf4895cfd 100644 --- a/src/bundle/Core/Resources/config/services.yml +++ b/src/bundle/Core/Resources/config/services.yml @@ -191,7 +191,8 @@ services: Ibexa\Bundle\Core\Converter\LocationParamConverter: class: Ibexa\Bundle\Core\Converter\LocationParamConverter arguments: - - '@ibexa.siteaccessaware.service.location' + $locationService: '@ibexa.siteaccessaware.service.location' + $contentPreviewHelper: '@Ibexa\Core\Helper\ContentPreviewHelper' tags: - { name: request.param_converter, priority: '%ibexa.param_converter.location.priority%', converter: ez_location_converter } diff --git a/src/lib/MVC/Symfony/Templating/Twig/Extension/RoutingExtension.php b/src/lib/MVC/Symfony/Templating/Twig/Extension/RoutingExtension.php index 165b89f2f5..d8529bb500 100644 --- a/src/lib/MVC/Symfony/Templating/Twig/Extension/RoutingExtension.php +++ b/src/lib/MVC/Symfony/Templating/Twig/Extension/RoutingExtension.php @@ -8,6 +8,7 @@ namespace Ibexa\Core\MVC\Symfony\Templating\Twig\Extension; +use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException; use Ibexa\Contracts\Core\Repository\Values\Content\Content; use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; use Ibexa\Contracts\Core\Repository\Values\Content\Location; @@ -103,14 +104,26 @@ 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); + } + + /** + * @param array $parameters + */ + 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 diff --git a/tests/bundle/Core/Converter/LocationParamConverterTest.php b/tests/bundle/Core/Converter/LocationParamConverterTest.php index 07d368e544..e2b69783f1 100644 --- a/tests/bundle/Core/Converter/LocationParamConverterTest.php +++ b/tests/bundle/Core/Converter/LocationParamConverterTest.php @@ -9,6 +9,7 @@ use Ibexa\Bundle\Core\Converter\LocationParamConverter; use Ibexa\Contracts\Core\Repository\LocationService; use Ibexa\Contracts\Core\Repository\Values\Content\Location; +use Ibexa\Core\Helper\ContentPreviewHelper; use Symfony\Component\HttpFoundation\Request; class LocationParamConverterTest extends AbstractParamConverterTest @@ -25,8 +26,9 @@ class LocationParamConverterTest extends AbstractParamConverterTest protected function setUp(): void { $this->locationServiceMock = $this->createMock(LocationService::class); + $contentPreviewHelper = $this->createMock(ContentPreviewHelper::class); - $this->converter = new LocationParamConverter($this->locationServiceMock); + $this->converter = new LocationParamConverter($this->locationServiceMock, $contentPreviewHelper); } public function testSupports()