From 5267ac50d2d6f99e35daa2231e904d51c2c6c49a Mon Sep 17 00:00:00 2001
From: Vidar Langseid
Date: Wed, 7 Feb 2024 14:27:00 +0100
Subject: [PATCH] IBX-6312: View matcher ParentContentType should not throw
execption if parent is not available
---
.../Resources/config/services.yml | 2 +
.../Controller/Content/PreviewController.php | 51 +++++++++++++++++--
2 files changed, 48 insertions(+), 5 deletions(-)
diff --git a/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml b/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml
index 967c64fbb7..2902742338 100644
--- a/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml
+++ b/eZ/Bundle/EzPublishCoreBundle/Resources/config/services.yml
@@ -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 }
diff --git a/eZ/Publish/Core/MVC/Symfony/Controller/Content/PreviewController.php b/eZ/Publish/Core/MVC/Symfony/Controller/Content/PreviewController.php
index 6ec36517f2..49eca62dbf 100644
--- a/eZ/Publish/Core/MVC/Symfony/Controller/Content/PreviewController.php
+++ b/eZ/Publish/Core/MVC/Symfony/Controller/Content/PreviewController.php
@@ -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;
@@ -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';
@@ -52,6 +59,8 @@ class PreviewController
/** @var \eZ\Publish\Core\MVC\Symfony\View\CustomLocationControllerChecker */
private $controllerChecker;
+ private bool $debugMode;
+
public function __construct(
ContentService $contentService,
LocationService $locationService,
@@ -59,7 +68,9 @@ public function __construct(
ContentPreviewHelper $previewHelper,
AuthorizationCheckerInterface $authorizationChecker,
PreviewLocationProvider $locationProvider,
- CustomLocationControllerChecker $controllerChecker
+ CustomLocationControllerChecker $controllerChecker,
+ bool $debugMode,
+ ?LoggerInterface $logger = null,
) {
$this->contentService = $contentService;
$this->locationService = $locationService;
@@ -68,6 +79,8 @@ public function __construct(
$this->authorizationChecker = $authorizationChecker;
$this->locationProvider = $locationProvider;
$this->controllerChecker = $controllerChecker;
+ $this->debugMode = $debugMode;
+ $this->logger = $logger ?? new NullLogger();
}
/**
@@ -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 = <<isDraft() && $this->controllerChecker->usesCustomController($content, $location)) {
+ // @todo This should probably be an exception that embeds the original one
+ $message = <<The view that rendered this location draft uses a custom controller, and resulted in a fatal error.
Location View is deprecated, as it causes issues with preview, such as an empty location id when previewing the first version of a content.
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 = <<$message
+Unable to load the preview page
+See logs for more information
+EOF;
+
+ return new Response($message);
}
$response->setPrivate();