Skip to content

Commit 7f60c51

Browse files
committed
MC-16152: Login Only B2B breaks Page Builder preview
- Resolve static failures - Refactor code to offload emulation to Preview class
1 parent 5d9149f commit 7f60c51

File tree

4 files changed

+147
-128
lines changed

4 files changed

+147
-128
lines changed

app/code/Magento/PageBuilder/Controller/Adminhtml/Stage/Preview.php

Lines changed: 7 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
/**
1515
* Preview controller to render blocks preview on Stage
1616
*
17-
* This isn't placed within the adminhtml folder as it has to extend from the front-end controllers app action to
18-
* ensure the content is rendered in the storefront scope.
19-
*
2017
* @api
2118
*/
2219
class Preview extends \Magento\Backend\App\Action implements HttpPostActionInterface
@@ -29,72 +26,24 @@ class Preview extends \Magento\Backend\App\Action implements HttpPostActionInter
2926
private $rendererPool;
3027

3128
/**
32-
* @var \Magento\Framework\App\State
33-
*/
34-
private $appState;
35-
36-
/**
37-
* @var \Magento\Theme\Model\View\Design
29+
* @var \Magento\PageBuilder\Model\Stage\Preview
3830
*/
39-
private $design;
40-
41-
/**
42-
* @var \Magento\Framework\View\Design\Theme\ThemeProviderInterface
43-
*/
44-
private $themeProvider;
45-
46-
/**
47-
* @var \Magento\Store\Model\StoreManagerInterface
48-
*/
49-
private $storeManager;
50-
51-
/**
52-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
53-
*/
54-
private $scopeConfig;
55-
56-
/**
57-
* @var \Magento\Store\Model\App\Emulation
58-
*/
59-
private $emulation;
60-
61-
/**
62-
* @var \Magento\PageBuilder\Model\Stage\PreviewRegistry
63-
*/
64-
private $previewRegistry;
31+
private $preview;
6532

6633
/**
6734
* @param \Magento\Backend\App\Action\Context $context
6835
* @param \Magento\PageBuilder\Model\Stage\RendererPool $rendererPool
69-
* @param \Magento\Store\Model\App\Emulation $emulation
70-
* @param \Magento\Framework\App\State $appState
71-
* @param \Magento\Framework\View\DesignInterface $design
72-
* @param \Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider
73-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
74-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
75-
* @param \Magento\PageBuilder\Model\Stage\PreviewRegistry $previewRegistry
36+
* @param \Magento\PageBuilder\Model\Stage\Preview $preview
7637
*/
7738
public function __construct(
7839
\Magento\Backend\App\Action\Context $context,
7940
\Magento\PageBuilder\Model\Stage\RendererPool $rendererPool,
80-
\Magento\Store\Model\App\Emulation $emulation,
81-
\Magento\Framework\App\State $appState,
82-
\Magento\Framework\View\DesignInterface $design,
83-
\Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider,
84-
\Magento\Store\Model\StoreManagerInterface $storeManager,
85-
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
86-
\Magento\PageBuilder\Model\Stage\PreviewRegistry $previewRegistry
41+
\Magento\PageBuilder\Model\Stage\Preview $preview
8742
) {
8843
parent::__construct($context);
8944

9045
$this->rendererPool = $rendererPool;
91-
$this->emulation = $emulation;
92-
$this->appState = $appState;
93-
$this->design = $design;
94-
$this->themeProvider = $themeProvider;
95-
$this->storeManager = $storeManager;
96-
$this->scopeConfig = $scopeConfig;
97-
$this->previewRegistry = $previewRegistry;
46+
$this->preview = $preview;
9847
}
9948

10049
/**
@@ -105,20 +54,8 @@ public function __construct(
10554
*/
10655
public function execute()
10756
{
108-
$defaultStore = $this->storeManager->getDefaultStoreView();
109-
$this->previewRegistry->setIsPreview(true);
110-
$this->emulation->startEnvironmentEmulation($defaultStore->getId());
111-
112-
return $this->appState->emulateAreaCode(
113-
$this->previewRegistry->getPreviewArea(),
57+
return $this->preview->startPreviewMode(
11458
function () {
115-
$themeId = $this->scopeConfig->getValue(
116-
'design/theme/theme_id',
117-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
118-
);
119-
$theme = $this->themeProvider->getThemeById($themeId);
120-
$this->design->setDesignTheme($theme, $this->previewRegistry->getPreviewArea());
121-
12259
$pageResult = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
12360
// Some template filters and directive processors expect this to be called in order to function.
12461
$pageResult->initLayout();
@@ -127,10 +64,7 @@ function () {
12764
$renderer = $this->rendererPool->getRenderer($params['role']);
12865
$result = ['data' => $renderer->render($params)];
12966

130-
$response = $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
131-
$this->emulation->stopEnvironmentEmulation();
132-
$this->previewRegistry->setIsPreview(false);
133-
return $response;
67+
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
13468
}
13569
);
13670
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageBuilder\Model\Stage;
9+
10+
/**
11+
* Handle placing Magento into Page Builder Preview mode and emulating the store front
12+
*/
13+
class Preview
14+
{
15+
/**
16+
* @var \Magento\Store\Model\App\Emulation
17+
*/
18+
private $emulation;
19+
20+
/**
21+
* @var \Magento\Framework\App\State
22+
*/
23+
private $appState;
24+
25+
/**
26+
* @var \Magento\Framework\View\DesignInterface
27+
*/
28+
private $design;
29+
30+
/**
31+
* @var \Magento\Framework\View\Design\Theme\ThemeProviderInterface
32+
*/
33+
private $themeProvider;
34+
35+
/**
36+
* @var \Magento\Store\Model\StoreManagerInterface
37+
*/
38+
private $storeManager;
39+
40+
/**
41+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
42+
*/
43+
private $scopeConfig;
44+
45+
/**
46+
* Preview constructor.
47+
* @param \Magento\Store\Model\App\Emulation $emulation
48+
* @param \Magento\Framework\App\State $appState
49+
* @param \Magento\Framework\View\DesignInterface $design
50+
* @param \Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider
51+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
52+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
53+
*/
54+
public function __construct(
55+
\Magento\Store\Model\App\Emulation $emulation,
56+
\Magento\Framework\App\State $appState,
57+
\Magento\Framework\View\DesignInterface $design,
58+
\Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider,
59+
\Magento\Store\Model\StoreManagerInterface $storeManager,
60+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
61+
) {
62+
$this->emulation = $emulation;
63+
$this->appState = $appState;
64+
$this->design = $design;
65+
$this->themeProvider = $themeProvider;
66+
$this->storeManager = $storeManager;
67+
$this->scopeConfig = $scopeConfig;
68+
}
69+
70+
/**
71+
* @var bool
72+
*/
73+
private $isPreview;
74+
75+
/**
76+
* Retrieve the area in which the preview needs to be ran in
77+
*
78+
* @return string
79+
*/
80+
public function getPreviewArea() : string
81+
{
82+
return \Magento\Framework\App\Area::AREA_FRONTEND;
83+
}
84+
85+
/**
86+
* Start Page Builder preview mode and emulate store front
87+
*
88+
* @param callable $callback
89+
* @param int $storeId
90+
* @return mixed
91+
* @throws \Exception
92+
*/
93+
public function startPreviewMode($callback, $storeId = null)
94+
{
95+
$this->isPreview = true;
96+
97+
if (!$storeId) {
98+
$storeId = $this->storeManager->getDefaultStoreView()->getId();
99+
}
100+
$this->emulation->startEnvironmentEmulation($storeId);
101+
102+
return $this->appState->emulateAreaCode(
103+
$this->getPreviewArea(),
104+
function () use ($callback) {
105+
$themeId = $this->scopeConfig->getValue(
106+
'design/theme/theme_id',
107+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
108+
);
109+
$theme = $this->themeProvider->getThemeById($themeId);
110+
$this->design->setDesignTheme($theme, $this->getPreviewArea());
111+
112+
try {
113+
$result = $callback();
114+
} catch (\Exception $e) {
115+
$this->isPreview = false;
116+
throw $e;
117+
}
118+
119+
$this->emulation->stopEnvironmentEmulation();
120+
return $result;
121+
}
122+
);
123+
}
124+
125+
/**
126+
* Determine if the system is in preview mode
127+
*
128+
* @return bool
129+
*/
130+
public function isPreviewMode() : bool
131+
{
132+
return $this->isPreview;
133+
}
134+
}

app/code/Magento/PageBuilder/Model/Stage/PreviewRegistry.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

app/code/Magento/PageBuilder/Plugin/DesignLoader.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,26 @@ class DesignLoader
3131
private $appState;
3232

3333
/**
34-
* @var \Magento\PageBuilder\Model\Stage\PreviewRegistry
34+
* @var \Magento\PageBuilder\Model\Stage\Preview
3535
*/
36-
private $previewRegistry;
36+
private $preview;
3737

3838
/**
3939
* @param \Magento\Framework\View\DesignLoader $designLoader
4040
* @param \Magento\Framework\Message\ManagerInterface $messageManager
4141
* @param \Magento\Framework\App\State $appState
42-
* @param \Magento\PageBuilder\Model\Stage\PreviewRegistry $previewRegistry
42+
* @param \Magento\PageBuilder\Model\Stage\Preview $previewRegistry
4343
*/
4444
public function __construct(
4545
\Magento\Framework\View\DesignLoader $designLoader,
4646
\Magento\Framework\Message\ManagerInterface $messageManager,
4747
\Magento\Framework\App\State $appState,
48-
\Magento\PageBuilder\Model\Stage\PreviewRegistry $previewRegistry
48+
\Magento\PageBuilder\Model\Stage\Preview $preview
4949
) {
5050
$this->designLoader = $designLoader;
5151
$this->messageManager = $messageManager;
5252
$this->appState = $appState;
53-
$this->previewRegistry = $previewRegistry;
53+
$this->preview = $preview;
5454
}
5555

5656
/**
@@ -70,7 +70,7 @@ public function beforeCreate(
7070
string $imageId,
7171
array $attributes = null
7272
) {
73-
if ($this->previewRegistry->isPreview()) {
73+
if ($this->preview->isPreviewMode()) {
7474
$this->appState->emulateAreaCode(
7575
$this->previewRegistry->getPreviewArea(),
7676
[$this, 'loadDesignConfig']

0 commit comments

Comments
 (0)