44
55use Loki \Components \Layout \LayoutHandlerComposite ;
66use Magento \Framework \View \Layout \BuilderFactory ;
7+ use Magento \Framework \View \Layout \ProcessorInterface ;
78use Magento \Framework \View \LayoutFactory ;
89use Magento \Framework \View \LayoutInterface ;
10+ use Magento \Framework \View \Model \PageLayout \Config \BuilderInterface as PageLayoutConfigBuilder ;
911use Magento \Framework \View \Page \Config as PageConfig ;
1012use Magento \Framework \View \Page \ConfigFactory as PageConfigFactory ;
1113use Magento \Framework \View \Page \Layout \Reader as PageLayoutReader ;
@@ -20,22 +22,26 @@ public function __construct(
2022 private readonly PageConfig $ pageConfig ,
2123 private readonly PageConfigFactory $ pageConfigFactory ,
2224 private readonly PageLayoutReader $ pageLayoutReader ,
25+ private readonly PageLayoutConfigBuilder $ pageLayoutConfigBuilder ,
2326 ) {
2427 }
2528
2629 public function load (array $ handles = [], array $ pageHandles = [], bool $ isolated = false ): LayoutInterface
2730 {
2831 if ($ isolated ) {
32+ $ handles = $ this ->layoutHandlerComposite ->getHandles ($ handles );
2933 $ layout = $ this ->layoutFactory ->create ();
3034 $ update = $ layout ->getUpdate ();
31- $ update ->addHandle ('default ' );
3235
33- $ update ->addPageHandles ($ pageHandles );
36+ $ this ->applyPageHandles ($ update , $ pageHandles );
37+
38+ $ update ->addHandle ('default ' );
3439 foreach ($ handles as $ handle ) {
3540 $ update ->addHandle ($ this ->sanitizeHandle ($ handle ));
3641 }
3742
3843 $ pageConfig = $ this ->pageConfigFactory ->create (); // important: fresh instance
44+ $ this ->applyPageLayout ($ pageConfig , $ pageHandles );
3945
4046 $ builder = $ this ->layoutBuilderFactory ->create (
4147 BuilderFactory::TYPE_PAGE ,
@@ -53,12 +59,16 @@ public function load(array $handles = [], array $pageHandles = [], bool $isolate
5359
5460 $ handles = $ this ->layoutHandlerComposite ->getHandles ($ handles );
5561 $ update = $ this ->layout ->getUpdate ();
56- $ update ->addPageHandles ($ pageHandles );
5762
63+ $ this ->applyPageHandles ($ update , $ pageHandles );
64+
65+ $ update ->addHandle ('default ' );
5866 foreach ($ handles as $ handle ) {
5967 $ update ->addHandle ($ this ->sanitizeHandle ($ handle ));
6068 }
6169
70+ $ this ->applyPageLayout ($ this ->pageConfig , $ pageHandles );
71+
6272 $ builder = $ this ->layoutBuilderFactory ->create (
6373 BuilderFactory::TYPE_PAGE ,
6474 [
@@ -73,6 +83,38 @@ public function load(array $handles = [], array $pageHandles = [], bool $isolate
7383 return $ this ->layout ;
7484 }
7585
86+ private function applyPageHandles (ProcessorInterface $ update , array $ pageHandles ): void
87+ {
88+ if (empty ($ pageHandles )) {
89+ return ;
90+ }
91+
92+ $ update ->addPageHandles ($ pageHandles );
93+
94+ $ pageLayoutsConfig = $ this ->pageLayoutConfigBuilder ->getPageLayoutsConfig ();
95+ foreach ($ pageHandles as $ pageHandle ) {
96+ $ sanitized = $ this ->sanitizeHandle ($ pageHandle );
97+ if ($ pageLayoutsConfig ->hasPageLayout ($ sanitized )) {
98+ $ update ->addHandle ($ sanitized );
99+ }
100+ }
101+ }
102+
103+ private function applyPageLayout (PageConfig $ pageConfig , array $ pageHandles ): void
104+ {
105+ if (empty ($ pageHandles )) {
106+ return ;
107+ }
108+
109+ $ pageLayoutsConfig = $ this ->pageLayoutConfigBuilder ->getPageLayoutsConfig ();
110+ foreach ($ pageHandles as $ pageHandle ) {
111+ $ sanitized = $ this ->sanitizeHandle ($ pageHandle );
112+ if ($ pageLayoutsConfig ->hasPageLayout ($ sanitized )) {
113+ $ pageConfig ->setPageLayout ($ sanitized );
114+ }
115+ }
116+ }
117+
76118 private function sanitizeHandle (string $ handle ): string
77119 {
78120 return preg_replace ('/([^a-z0-9\-_]+)/ ' , '' , $ handle );
0 commit comments