Skip to content

Commit

Permalink
IBX-6312: View matcher ParentContentType should not throw execption i…
Browse files Browse the repository at this point in the history
…f parent is not available
  • Loading branch information
vidarl committed Feb 7, 2024
1 parent 19a4434 commit 5267ac5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
2 changes: 2 additions & 0 deletions eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ services:
$authorizationChecker: '@security.authorization_checker'
$locationProvider: '@ezpublish.content_preview.location_provider'
$controllerChecker: '@ezpublish.view.custom_location_controller_checker'
$debugMode: '%kernel.debug%'
$logger: '@logger'
tags:
- { name: controller.service_arguments }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use eZ\Publish\Core\Helper\ContentPreviewHelper;
use eZ\Publish\Core\Helper\PreviewLocationProvider;
use eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator;
Expand All @@ -21,13 +22,19 @@
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
use eZ\Publish\Core\MVC\Symfony\View\CustomLocationControllerChecker;
use eZ\Publish\Core\MVC\Symfony\View\ViewManagerInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

class PreviewController
{
use LoggerAwareTrait;

public const PREVIEW_PARAMETER_NAME = 'isPreview';
public const CONTENT_VIEW_ROUTE = '_ez_content_view';

Expand All @@ -52,14 +59,18 @@ class PreviewController
/** @var \eZ\Publish\Core\MVC\Symfony\View\CustomLocationControllerChecker */
private $controllerChecker;

private bool $debugMode;

public function __construct(
ContentService $contentService,
LocationService $locationService,
HttpKernelInterface $kernel,
ContentPreviewHelper $previewHelper,
AuthorizationCheckerInterface $authorizationChecker,
PreviewLocationProvider $locationProvider,
CustomLocationControllerChecker $controllerChecker
CustomLocationControllerChecker $controllerChecker,
bool $debugMode,
?LoggerInterface $logger = null,
) {
$this->contentService = $contentService;
$this->locationService = $locationService;
Expand All @@ -68,6 +79,8 @@ public function __construct(
$this->authorizationChecker = $authorizationChecker;
$this->locationProvider = $locationProvider;
$this->controllerChecker = $controllerChecker;
$this->debugMode = $debugMode;
$this->logger = $logger ?? new NullLogger();
}

/**
Expand Down Expand Up @@ -118,17 +131,45 @@ public function previewContentAction(
false
);
} catch (\Exception $e) {
if ($location->isDraft() && $this->controllerChecker->usesCustomController($content, $location)) {
// @todo This should probably be an exception that embeds the original one
$message = <<<EOF
try {
if ($location->isDraft() && $this->controllerChecker->usesCustomController($content, $location)) {
// @todo This should probably be an exception that embeds the original one
$message = <<<EOF
<p>The view that rendered this location draft uses a custom controller, and resulted in a fatal error.</p>
<p>Location View is deprecated, as it causes issues with preview, such as an empty location id when previewing the first version of a content.</p>
EOF;

throw new Exception($message, 0, $e);
throw new Exception($message, 0, $e);
}
} catch (\Exception $e2) {
$this->logger->warning('Unable to check if location uses a custom controller when loading the preview page', ['exception' => $e2]);
if ($this->debugMode) {
throw $e2;
}
}
$message = '';

if ($e instanceof NotFoundException) {
$message .= 'Location not found or not available in requested language';
$this->logger->warning('Location not found or not available in requested language when loading the preview page', ['exception' => $e]);
if ($this->debugMode) {
throw new Exception($message, 0, $e);
}
} else {
$this->logger->warning('Unable to load the preview page', ['exception' => $e]);
}

if ($this->debugMode) {
throw $e;
}

$message = <<<EOF
<p>$message</p>
<p>Unable to load the preview page</p>
<p>See logs for more information</p>
EOF;

return new Response($message);
}
$response->setPrivate();

Expand Down

0 comments on commit 5267ac5

Please sign in to comment.