Skip to content

Commit

Permalink
IBX-6504: Gracefully handled URL generation in RoutingExtension
Browse files Browse the repository at this point in the history
For more details see https://issues.ibexa.co/browse/IBX-6504 and #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.
  • Loading branch information
barw4 authored Oct 26, 2023
1 parent 336f0a6 commit 8880cf8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
19 changes: 16 additions & 3 deletions eZ/Bundle/EzPublishCoreBundle/Converter/LocationParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}
}
3 changes: 2 additions & 1 deletion eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8880cf8

Please sign in to comment.