7
7
8
8
namespace Magento \PageBuilder \Model \Stage ;
9
9
10
+ use Magento \Framework \App \Area ;
11
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
12
+ use Magento \Framework \App \State ;
13
+ use Magento \Framework \View \Design \Theme \ThemeProviderInterface ;
14
+ use Magento \Framework \View \DesignInterface ;
15
+ use Magento \Store \Model \App \Emulation ;
16
+ use Magento \Store \Model \ScopeInterface ;
17
+ use Magento \Store \Model \StoreManagerInterface ;
18
+
10
19
/**
11
20
* Handle placing Magento into Page Builder Preview mode and emulating the store front
12
21
*/
13
22
class Preview
14
23
{
15
24
/**
16
- * @var \Magento\Store\Model\App\ Emulation
25
+ * @var Emulation
17
26
*/
18
27
private $ emulation ;
19
28
20
29
/**
21
- * @var \Magento\Framework\App\ State
30
+ * @var State
22
31
*/
23
32
private $ appState ;
24
33
25
34
/**
26
- * @var \Magento\Framework\View\ DesignInterface
35
+ * @var DesignInterface
27
36
*/
28
37
private $ design ;
29
38
30
39
/**
31
- * @var \Magento\Framework\View\Design\Theme\ ThemeProviderInterface
40
+ * @var ThemeProviderInterface
32
41
*/
33
42
private $ themeProvider ;
34
43
35
44
/**
36
- * @var \Magento\Store\Model\ StoreManagerInterface
45
+ * @var StoreManagerInterface
37
46
*/
38
47
private $ storeManager ;
39
48
40
49
/**
41
- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
50
+ * @var ScopeConfigInterface
42
51
*/
43
52
private $ scopeConfig ;
44
53
@@ -49,20 +58,20 @@ class Preview
49
58
50
59
/**
51
60
* Preview constructor.
52
- * @param \Magento\Store\Model\App\ Emulation $emulation
53
- * @param \Magento\Framework\App\ State $appState
54
- * @param \Magento\Framework\View\ DesignInterface $design
55
- * @param \Magento\Framework\View\Design\Theme\ ThemeProviderInterface $themeProvider
56
- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
57
- * @param \Magento\Framework\App\Config\ ScopeConfigInterface $scopeConfig
61
+ * @param Emulation $emulation
62
+ * @param State $appState
63
+ * @param DesignInterface $design
64
+ * @param ThemeProviderInterface $themeProvider
65
+ * @param StoreManagerInterface $storeManager
66
+ * @param ScopeConfigInterface $scopeConfig
58
67
*/
59
68
public function __construct (
60
- \ Magento \ Store \ Model \ App \ Emulation $ emulation ,
61
- \ Magento \ Framework \ App \ State $ appState ,
62
- \ Magento \ Framework \ View \ DesignInterface $ design ,
63
- \ Magento \ Framework \ View \ Design \ Theme \ ThemeProviderInterface $ themeProvider ,
64
- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
65
- \ Magento \ Framework \ App \ Config \ ScopeConfigInterface $ scopeConfig
69
+ Emulation $ emulation ,
70
+ State $ appState ,
71
+ DesignInterface $ design ,
72
+ ThemeProviderInterface $ themeProvider ,
73
+ StoreManagerInterface $ storeManager ,
74
+ ScopeConfigInterface $ scopeConfig
66
75
) {
67
76
$ this ->emulation = $ emulation ;
68
77
$ this ->appState = $ appState ;
@@ -79,7 +88,7 @@ public function __construct(
79
88
*/
80
89
public function getPreviewArea () : string
81
90
{
82
- return \ Magento \ Framework \ App \ Area::AREA_FRONTEND ;
91
+ return Area::AREA_FRONTEND ;
83
92
}
84
93
85
94
/**
@@ -95,7 +104,7 @@ public function startPreviewMode($callback, $storeId = null)
95
104
$ this ->isPreview = true ;
96
105
97
106
if (!$ storeId ) {
98
- $ storeId = $ this ->storeManager -> getDefaultStoreView ()-> getId ();
107
+ $ storeId = $ this ->getStoreId ();
99
108
}
100
109
$ this ->emulation ->startEnvironmentEmulation ($ storeId );
101
110
@@ -104,7 +113,7 @@ public function startPreviewMode($callback, $storeId = null)
104
113
function () use ($ callback ) {
105
114
$ themeId = $ this ->scopeConfig ->getValue (
106
115
'design/theme/theme_id ' ,
107
- \ Magento \ Store \ Model \ ScopeInterface::SCOPE_STORE
116
+ ScopeInterface::SCOPE_STORE
108
117
);
109
118
$ theme = $ this ->themeProvider ->getThemeById ($ themeId );
110
119
$ this ->design ->setDesignTheme ($ theme , $ this ->getPreviewArea ());
@@ -131,4 +140,26 @@ public function isPreviewMode() : bool
131
140
{
132
141
return $ this ->isPreview ;
133
142
}
143
+
144
+ /**
145
+ * Returns store id by default store view or store id from the available store if default store view is null
146
+ *
147
+ * @return int|null
148
+ */
149
+ private function getStoreId (): ?int
150
+ {
151
+ $ storeId = null ;
152
+ $ store = $ this ->storeManager ->getDefaultStoreView ();
153
+ if ($ store ) {
154
+ $ storeId = (int ) $ store ->getId ();
155
+ } else {
156
+ $ stores = $ this ->storeManager ->getStores ();
157
+ if (count ($ stores )) {
158
+ $ store = array_shift ($ stores );
159
+ $ storeId = (int ) $ store ->getId ();
160
+ }
161
+ }
162
+
163
+ return $ storeId ;
164
+ }
134
165
}
0 commit comments