1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace IntegerNet\GlobalCustomLayout\Plugin; |
||
5 | |||
6 | use Magento\Cms\Api\Data\PageInterface; |
||
7 | use Magento\Cms\Api\PageRepositoryInterface; |
||
8 | use Magento\Cms\Model\Page\CustomLayoutManagerInterface; |
||
9 | use Magento\Cms\Model\Page\CustomLayout\CustomLayoutManager; |
||
10 | use Magento\Cms\Model\Page\CustomLayout\Data\CustomLayoutSelectedInterface; |
||
11 | use Magento\Cms\Model\Page\IdentityMap; |
||
12 | use Magento\Framework\App\Area; |
||
13 | use Magento\Framework\View\Design\Theme\FlyweightFactory; |
||
14 | use Magento\Framework\View\DesignInterface; |
||
15 | use Magento\Framework\View\Model\Layout\Merge as LayoutProcessor; |
||
16 | use Magento\Framework\View\Model\Layout\MergeFactory as LayoutProcessorFactory; |
||
17 | use Magento\Framework\View\Result\Page as PageLayout; |
||
18 | |||
19 | class PageLayoutPlugin { |
||
20 | |||
21 | /** |
||
22 | * @var FlyweightFactory |
||
23 | */ |
||
24 | private $themeFactory; |
||
25 | |||
26 | /** |
||
27 | * @var DesignInterface |
||
28 | */ |
||
29 | private $design; |
||
30 | |||
31 | /** |
||
32 | * @var PageRepositoryInterface |
||
33 | */ |
||
34 | private $pageRepository; |
||
35 | |||
36 | /** |
||
37 | * @var LayoutProcessorFactory |
||
38 | */ |
||
39 | private $layoutProcessorFactory; |
||
40 | |||
41 | /** |
||
42 | * @var LayoutProcessor|null |
||
43 | */ |
||
44 | private $layoutProcessor; |
||
45 | |||
46 | /** |
||
47 | * @var IdentityMap |
||
48 | */ |
||
49 | private $identityMap; |
||
50 | |||
51 | /** |
||
52 | * @param FlyweightFactory $themeFactory |
||
53 | * @param DesignInterface $design |
||
54 | * @param PageRepositoryInterface $pageRepository |
||
55 | * @param LayoutProcessorFactory $layoutProcessorFactory |
||
56 | * @param IdentityMap $identityMap |
||
57 | */ |
||
58 | 2 | public function __construct( |
|
59 | FlyweightFactory $themeFactory, |
||
60 | DesignInterface $design, |
||
61 | PageRepositoryInterface $pageRepository, |
||
62 | LayoutProcessorFactory $layoutProcessorFactory, |
||
63 | IdentityMap $identityMap |
||
64 | ) { |
||
65 | 2 | $this->themeFactory = $themeFactory; |
|
66 | 2 | $this->design = $design; |
|
67 | 2 | $this->pageRepository = $pageRepository; |
|
68 | 2 | $this->layoutProcessorFactory = $layoutProcessorFactory; |
|
69 | 2 | $this->identityMap = $identityMap; |
|
70 | 2 | } |
|
71 | |||
72 | /** |
||
73 | * Get the processor instance. |
||
74 | * |
||
75 | * @return LayoutProcessor |
||
76 | * |
||
77 | * Unchanged private method copied over from @var CustomLayoutManager |
||
78 | */ |
||
79 | 2 | private function getLayoutProcessor(): LayoutProcessor |
|
80 | { |
||
81 | 2 | if (!$this->layoutProcessor) { |
|
82 | 2 | $this->layoutProcessor = $this->layoutProcessorFactory->create( |
|
83 | [ |
||
84 | 2 | 'theme' => $this->themeFactory->create( |
|
85 | 2 | $this->design->getConfigurationDesignTheme(Area::AREA_FRONTEND) |
|
86 | ) |
||
87 | ] |
||
88 | ); |
||
89 | 2 | $this->themeFactory = null; |
|
90 | 2 | $this->design = null; |
|
91 | } |
||
92 | |||
93 | 2 | return $this->layoutProcessor; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Fetch list of available global files/handles for the page. |
||
98 | * |
||
99 | * @param CustomLayoutManagerInterface $subject |
||
100 | * @param array $result |
||
101 | * @param PageInterface $page |
||
102 | * @return array |
||
103 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
104 | */ |
||
105 | 2 | public function afterFetchAvailableFiles( |
|
106 | CustomLayoutManagerInterface $subject, |
||
107 | array $result, |
||
108 | PageInterface $page |
||
109 | ): array { |
||
110 | 2 | $handles = $this->getLayoutProcessor()->getAvailableHandles(); |
|
111 | |||
112 | 2 | return array_merge($result, array_filter( |
|
113 | 2 | array_map( |
|
114 | function(string $handle) : ?string { |
||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
115 | 2 | preg_match( |
|
116 | 2 | '/^cms\_page\_view\_selectable\_0\_([a-z0-9]+)/i', |
|
117 | $handle, |
||
118 | $selectable |
||
119 | ); |
||
120 | 2 | if (!empty($selectable[1])) { |
|
121 | return $selectable[1]; |
||
122 | } |
||
123 | |||
124 | 2 | return null; |
|
125 | 2 | }, |
|
126 | $handles |
||
127 | ) |
||
128 | )); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param CustomLayoutManagerInterface $subject |
||
133 | * @param $result |
||
134 | * @param PageLayout $layout |
||
135 | * @param CustomLayoutSelectedInterface $layoutSelected |
||
136 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
137 | */ |
||
138 | 2 | public function afterApplyUpdate( |
|
139 | CustomLayoutManagerInterface $subject, |
||
140 | $result, |
||
141 | PageLayout $layout, |
||
142 | CustomLayoutSelectedInterface $layoutSelected |
||
143 | ): void { |
||
144 | 2 | $layout->addPageLayoutHandles( |
|
145 | 2 | ['selectable_0' => $layoutSelected->getLayoutFileId()] |
|
146 | ); |
||
147 | 2 | } |
|
148 | } |
||
149 |