Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-6504: Gracefully handled URL generation in RoutingExtension #389

Merged
merged 7 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading