|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\PageBuilder\Controller\Adminhtml\Stage; |
| 10 | + |
| 11 | +use Magento\Framework\Controller\ResultFactory; |
| 12 | +use Magento\Framework\App\Action\HttpPostActionInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Preview controller to render blocks preview on Stage |
| 16 | + * |
| 17 | + * @api |
| 18 | + */ |
| 19 | +class Preview extends \Magento\Backend\App\Action implements HttpPostActionInterface |
| 20 | +{ |
| 21 | + const ADMIN_RESOURCE = 'Magento_Backend::content'; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Magento\PageBuilder\Model\Stage\RendererPool |
| 25 | + */ |
| 26 | + private $rendererPool; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \Magento\PageBuilder\Model\Stage\Preview |
| 30 | + */ |
| 31 | + private $preview; |
| 32 | + |
| 33 | + /** |
| 34 | + * @param \Magento\Backend\App\Action\Context $context |
| 35 | + * @param \Magento\PageBuilder\Model\Stage\RendererPool $rendererPool |
| 36 | + * @param \Magento\PageBuilder\Model\Stage\Preview $preview |
| 37 | + */ |
| 38 | + public function __construct( |
| 39 | + \Magento\Backend\App\Action\Context $context, |
| 40 | + \Magento\PageBuilder\Model\Stage\RendererPool $rendererPool, |
| 41 | + \Magento\PageBuilder\Model\Stage\Preview $preview |
| 42 | + ) { |
| 43 | + parent::__construct($context); |
| 44 | + |
| 45 | + $this->rendererPool = $rendererPool; |
| 46 | + $this->preview = $preview; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Generates an HTML preview for the stage |
| 51 | + * |
| 52 | + * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|mixed |
| 53 | + * @throws \Exception |
| 54 | + */ |
| 55 | + public function execute() |
| 56 | + { |
| 57 | + return $this->preview->startPreviewMode( |
| 58 | + function () { |
| 59 | + $pageResult = $this->resultFactory->create(ResultFactory::TYPE_PAGE); |
| 60 | + // Some template filters and directive processors expect this to be called in order to function. |
| 61 | + $pageResult->initLayout(); |
| 62 | + |
| 63 | + $params = $this->getRequest()->getParams(); |
| 64 | + $renderer = $this->rendererPool->getRenderer($params['role']); |
| 65 | + $result = ['data' => $renderer->render($params)]; |
| 66 | + |
| 67 | + return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result); |
| 68 | + } |
| 69 | + ); |
| 70 | + } |
| 71 | +} |
0 commit comments