Skip to content

Commit

Permalink
[TASK] Use ViewFactory in page preview
Browse files Browse the repository at this point in the history
  • Loading branch information
nhovratov committed Sep 20, 2024
1 parent 455944a commit abf7c7e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
28 changes: 17 additions & 11 deletions Classes/Backend/Preview/PageLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace TYPO3\CMS\ContentBlocks\Backend\Preview;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Controller\Event\ModifyPageLayoutContentEvent;
use TYPO3\CMS\Backend\Module\ModuleData;
use TYPO3\CMS\Backend\Utility\BackendUtility;
Expand All @@ -28,7 +29,9 @@
use TYPO3\CMS\ContentBlocks\Utility\ContentBlockPathUtility;
use TYPO3\CMS\Core\Domain\RecordFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Core\View\ViewFactoryData;
use TYPO3\CMS\Core\View\ViewFactoryInterface;
use TYPO3\CMS\Core\View\ViewInterface;

/**
* @internal Not part of TYPO3's public API.
Expand All @@ -42,6 +45,7 @@ public function __construct(
protected ContentBlockDataDecorator $contentBlockDataDecorator,
protected RootPathsSettings $rootPathsSettings,
protected ContentTypeResolver $contentTypeResolver,
protected ViewFactoryInterface $viewFactory,
) {}

public function __invoke(ModifyPageLayoutContentEvent $event): void
Expand Down Expand Up @@ -74,22 +78,24 @@ public function __invoke(ModifyPageLayoutContentEvent $event): void
return;
}
$contentBlockData = $this->contentBlockDataDecorator->decorate($resolvedRecord);
$view = $this->createView($contentTypeDefinition, $pageUid);
$view->setRequest($request);
$view = $this->createView($contentTypeDefinition, $pageUid, $request);
$view->assign('data', $contentBlockData);
$renderedView = (string)$view->render();
$renderedView = $view->render();
$event->addHeaderContent($renderedView);
}

protected function createView(ContentTypeInterface $contentTypeDefinition, int $pageUid): StandaloneView
protected function createView(ContentTypeInterface $contentTypeDefinition, int $pageUid, ServerRequestInterface $request): ViewInterface
{
$contentBlockPrivatePath = $this->getContentBlockPrivatePath($contentTypeDefinition);
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setLayoutRootPaths($this->getContentBlocksLayoutRootPaths($contentBlockPrivatePath, $pageUid));
$view->setPartialRootPaths($this->getContentBlocksPartialRootPaths($contentBlockPrivatePath, $pageUid));
$view->setTemplateRootPaths([$contentBlockPrivatePath]);
$view->setTemplate(ContentBlockPathUtility::getBackendPreviewFileNameWithoutExtension());
return $view;
$layoutRootPaths = $this->getContentBlocksLayoutRootPaths($contentBlockPrivatePath, $pageUid);
$partialRootPaths = $this->getContentBlocksPartialRootPaths($contentBlockPrivatePath, $pageUid);
$viewData = new ViewFactoryData(
partialRootPaths: $partialRootPaths,
layoutRootPaths: $layoutRootPaths,
templatePathAndFilename: $contentBlockPrivatePath . '/' . ContentBlockPathUtility::getBackendPreviewFileName(),
request: $request,
);
return $this->viewFactory->create($viewData);
}

/**
Expand Down
10 changes: 0 additions & 10 deletions Classes/Utility/ContentBlockPathUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public static function getBackendPreviewFileName(): string
return 'preview.html';
}

public static function getBackendPreviewFileNameWithoutExtension(): string
{
return substr(self::getBackendPreviewFileName(), 0, -5);
}

public static function getBackendPreviewPath(): string
{
return self::getTemplatesFolder() . '/' . self::getBackendPreviewFileName();
Expand All @@ -60,11 +55,6 @@ public static function getFrontendTemplateFileName(): string
return 'frontend.html';
}

public static function getFrontendTemplateFileNameWithoutExtension(): string
{
return substr(self::getFrontendTemplateFileName(), 0, -5);
}

public static function getFrontendTemplatePath(): string
{
return self::getTemplatesFolder() . '/' . self::getFrontendTemplateFileName();
Expand Down

0 comments on commit abf7c7e

Please sign in to comment.