Skip to content

Commit

Permalink
Merged branch '4.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Oct 27, 2023
2 parents 78a7da9 + 20efe6b commit 4ced86d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
21 changes: 17 additions & 4 deletions src/bundle/Core/Converter/LocationParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/bundle/Core/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
17 changes: 15 additions & 2 deletions src/lib/MVC/Symfony/Templating/Twig/Extension/RoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\ContentAwareInterface;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo;
Expand Down Expand Up @@ -104,14 +105,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<string, mixed> $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
Expand Down
4 changes: 3 additions & 1 deletion tests/bundle/Core/Converter/LocationParamConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 4ced86d

Please sign in to comment.