eZ\Publish\Core\MVC\Symfony\Controller\Content\PreviewController::previewContentAction PHP Method

previewContentAction() public method

public previewContentAction ( Request $request, $contentId, $versionNo, $language, $siteAccessName = null )
$request Symfony\Component\HttpFoundation\Request
    public function previewContentAction(Request $request, $contentId, $versionNo, $language, $siteAccessName = null)
    {
        $this->previewHelper->setPreviewActive(true);
        try {
            $content = $this->contentService->loadContent($contentId, array($language), $versionNo);
            $location = $this->locationProvider->loadMainLocation($contentId);
            if (!$location instanceof Location) {
                throw new NotImplementedException('Preview for content without locations');
            }
            $this->previewHelper->setPreviewedContent($content);
            $this->previewHelper->setPreviewedLocation($location);
        } catch (UnauthorizedException $e) {
            throw new AccessDeniedException();
        }
        if (!$this->authorizationChecker->isGranted(new AuthorizationAttribute('content', 'versionread', array('valueObject' => $content)))) {
            throw new AccessDeniedException();
        }
        $siteAccess = $this->previewHelper->getOriginalSiteAccess();
        // Only switch if $siteAccessName is set and different from original
        if ($siteAccessName !== null && $siteAccessName !== $siteAccess->name) {
            $siteAccess = $this->previewHelper->changeConfigScope($siteAccessName);
        }
        try {
            $response = $this->kernel->handle($this->getForwardRequest($location, $content, $siteAccess, $request, $language), HttpKernelInterface::SUB_REQUEST, 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
<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);
            } else {
                throw $e;
            }
        }
        $response->headers->remove('cache-control');
        $response->headers->remove('expires');
        $this->previewHelper->restoreConfigScope();
        $this->previewHelper->setPreviewActive(false);
        return $response;
    }