1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace IntegerNet\GlobalCustomLayout\Plugin; |
||
5 | |||
6 | use Magento\Catalog\Api\Data\CategoryInterface; |
||
7 | use Magento\Catalog\Model\Category; |
||
8 | use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager; |
||
9 | use Magento\Framework\App\Area; |
||
10 | use Magento\Framework\DataObject; |
||
11 | use Magento\Framework\View\Design\Theme\FlyweightFactory; |
||
12 | use Magento\Framework\View\DesignInterface; |
||
13 | use Magento\Framework\View\Model\Layout\Merge as LayoutProcessor; |
||
14 | use Magento\Framework\View\Model\Layout\MergeFactory as LayoutProcessorFactory; |
||
15 | |||
16 | class CategoryLayoutPlugin |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @var FlyweightFactory |
||
21 | */ |
||
22 | private $themeFactory; |
||
23 | |||
24 | /** |
||
25 | * @var DesignInterface |
||
26 | */ |
||
27 | private $design; |
||
28 | |||
29 | /** |
||
30 | * @var LayoutProcessorFactory |
||
31 | */ |
||
32 | private $layoutProcessorFactory; |
||
33 | |||
34 | /** |
||
35 | * @var LayoutProcessor|null |
||
36 | */ |
||
37 | private $layoutProcessor; |
||
38 | |||
39 | /** |
||
40 | * @param FlyweightFactory $themeFactory |
||
41 | * @param DesignInterface $design |
||
42 | * @param LayoutProcessorFactory $layoutProcessorFactory |
||
43 | */ |
||
44 | 2 | public function __construct( |
|
45 | FlyweightFactory $themeFactory, |
||
46 | DesignInterface $design, |
||
47 | LayoutProcessorFactory $layoutProcessorFactory) |
||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
48 | { |
||
0 ignored issues
–
show
|
|||
49 | 2 | $this->themeFactory = $themeFactory; |
|
50 | 2 | $this->design = $design; |
|
51 | 2 | $this->layoutProcessorFactory = $layoutProcessorFactory; |
|
52 | 2 | } |
|
53 | |||
54 | /** |
||
55 | * Get the processor instance. |
||
56 | * |
||
57 | * @return LayoutProcessor |
||
58 | * |
||
59 | * Unchanged private method copied over from @var LayoutUpdateManager |
||
60 | */ |
||
61 | 2 | private function getLayoutProcessor(): LayoutProcessor |
|
62 | { |
||
63 | 2 | if (!$this->layoutProcessor) { |
|
64 | 2 | $this->layoutProcessor = $this->layoutProcessorFactory->create( |
|
65 | [ |
||
66 | 2 | 'theme' => $this->themeFactory->create( |
|
67 | 2 | $this->design->getConfigurationDesignTheme(Area::AREA_FRONTEND) |
|
68 | ), |
||
69 | ] |
||
70 | ); |
||
71 | 2 | $this->themeFactory = null; |
|
72 | 2 | $this->design = null; |
|
73 | } |
||
74 | |||
75 | 2 | return $this->layoutProcessor; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Fetch list of available global files/handles for the category. |
||
80 | * |
||
81 | * @param LayoutUpdateManager $subject |
||
82 | * @param array $result |
||
83 | * @param CategoryInterface $category |
||
84 | * @return array |
||
85 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
86 | */ |
||
87 | 2 | public function afterFetchAvailableFiles( |
|
88 | LayoutUpdateManager $subject, |
||
89 | array $result, |
||
90 | CategoryInterface $category): array |
||
0 ignored issues
–
show
|
|||
91 | { |
||
0 ignored issues
–
show
|
|||
92 | 2 | $handles = $this->getLayoutProcessor()->getAvailableHandles(); |
|
93 | |||
94 | 2 | return array_merge( |
|
95 | 2 | $result, |
|
96 | 2 | array_filter( |
|
97 | 2 | array_map( |
|
98 | function (string $handle): ?string { |
||
99 | 2 | preg_match( |
|
100 | 2 | '/^catalog\_category\_view\_selectable\_0\_([a-z0-9]+)/i', |
|
101 | $handle, |
||
102 | $selectable |
||
103 | ); |
||
104 | 2 | if (!empty($selectable[1])) { |
|
105 | return $selectable[1]; |
||
106 | } |
||
107 | |||
108 | 2 | return null; |
|
109 | 2 | }, |
|
110 | $handles |
||
111 | ) |
||
112 | ) |
||
113 | ); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Extract selected global custom layout settings. |
||
118 | * |
||
119 | * If no update is selected none will apply. |
||
120 | * |
||
121 | * @param LayoutUpdateManager $subject |
||
122 | * @param $result |
||
123 | * @param CategoryInterface $category |
||
124 | * @param DataObject $intoSettings |
||
125 | * @return void |
||
126 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
127 | */ |
||
128 | 2 | public function afterExtractCustomSettings( |
|
129 | LayoutUpdateManager $subject, |
||
130 | $result, |
||
131 | CategoryInterface $category, |
||
132 | DataObject $intoSettings): void |
||
0 ignored issues
–
show
|
|||
133 | { |
||
0 ignored issues
–
show
|
|||
134 | 2 | if ($category->getId() && $value = $this->extractAttributeValue($category)) { |
|
135 | 2 | $handles = $intoSettings->getPageLayoutHandles() ?? []; |
|
136 | 2 | $handles = array_merge_recursive( |
|
137 | 2 | $handles, |
|
138 | 2 | ['selectable_0' => $value] |
|
139 | ); |
||
140 | 2 | $intoSettings->setPageLayoutHandles($handles); |
|
141 | } |
||
142 | 2 | } |
|
143 | |||
144 | /** |
||
145 | * Extract custom layout attribute value. |
||
146 | * |
||
147 | * @param CategoryInterface $category |
||
148 | * @return mixed |
||
149 | * |
||
150 | * Unchanged private method copied over from @var LayoutUpdateManager |
||
151 | */ |
||
152 | 2 | private function extractAttributeValue(CategoryInterface $category) |
|
153 | { |
||
154 | 2 | if ($category instanceof Category && !$category->hasData(CategoryInterface::CUSTOM_ATTRIBUTES)) { |
|
155 | 2 | return $category->getData('custom_layout_update_file'); |
|
156 | } |
||
157 | if ($attr = $category->getCustomAttribute('custom_layout_update_file')) { |
||
158 | return $attr->getValue(); |
||
159 | } |
||
160 | |||
161 | return null; |
||
162 | } |
||
163 | } |
||
164 |