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
|
1 |
|
public function __construct( |
45
|
|
|
FlyweightFactory $themeFactory, |
46
|
|
|
DesignInterface $design, |
47
|
|
|
LayoutProcessorFactory $layoutProcessorFactory |
48
|
|
|
) |
49
|
|
|
{ |
50
|
1 |
|
$this->themeFactory = $themeFactory; |
51
|
1 |
|
$this->design = $design; |
52
|
1 |
|
$this->layoutProcessorFactory = $layoutProcessorFactory; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the processor instance. |
57
|
|
|
* |
58
|
|
|
* @return LayoutProcessor |
59
|
|
|
* |
60
|
|
|
* Unchanged private method copied over from @var LayoutUpdateManager |
61
|
|
|
*/ |
62
|
1 |
|
private function getLayoutProcessor(): LayoutProcessor |
63
|
|
|
{ |
64
|
1 |
|
if (!$this->layoutProcessor) { |
65
|
1 |
|
$this->layoutProcessor = $this->layoutProcessorFactory->create( |
66
|
|
|
[ |
67
|
1 |
|
'theme' => $this->themeFactory->create( |
68
|
1 |
|
$this->design->getConfigurationDesignTheme(Area::AREA_FRONTEND) |
69
|
|
|
) |
70
|
|
|
] |
71
|
|
|
); |
72
|
1 |
|
$this->themeFactory = null; |
73
|
1 |
|
$this->design = null; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return $this->layoutProcessor; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Fetch list of available global files/handles for the category. |
81
|
|
|
* |
82
|
|
|
* @param LayoutUpdateManager $subject |
83
|
|
|
* @param array $result |
84
|
|
|
* @param CategoryInterface $category |
85
|
|
|
* @return array |
86
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
87
|
|
|
*/ |
88
|
1 |
|
public function afterFetchAvailableFiles( |
89
|
|
|
LayoutUpdateManager $subject, |
|
|
|
|
90
|
|
|
array $result, |
91
|
|
|
CategoryInterface $category |
92
|
|
|
): array |
93
|
|
|
{ |
94
|
1 |
|
if (!$category->getId()) { |
|
|
|
|
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
$handles = $this->getLayoutProcessor()->getAvailableHandles(); |
99
|
|
|
|
100
|
1 |
|
return array_merge($result, array_filter( |
101
|
1 |
|
array_map( |
102
|
1 |
|
function(string $handle) use ($category) : ?string { |
|
|
|
|
103
|
1 |
|
preg_match( |
104
|
1 |
|
'/^catalog\_category\_view\_selectable\_0\_([a-z0-9]+)/i', |
105
|
1 |
|
$handle, |
106
|
1 |
|
$selectable |
107
|
|
|
); |
108
|
1 |
|
if (!empty($selectable[1])) { |
109
|
|
|
return $selectable[1]; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
return null; |
113
|
1 |
|
}, |
114
|
1 |
|
$handles |
115
|
|
|
) |
116
|
|
|
)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Extract selected global custom layout settings. |
121
|
|
|
* |
122
|
|
|
* If no update is selected none will apply. |
123
|
|
|
* |
124
|
|
|
* @param LayoutUpdateManager $subject |
125
|
|
|
* @param $result |
126
|
|
|
* @param CategoryInterface $category |
127
|
|
|
* @param DataObject $intoSettings |
128
|
|
|
* @return void |
129
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
130
|
|
|
*/ |
131
|
1 |
|
public function afterExtractCustomSettings( |
132
|
|
|
LayoutUpdateManager $subject, |
|
|
|
|
133
|
|
|
$result, |
|
|
|
|
134
|
|
|
CategoryInterface $category, |
135
|
|
|
DataObject $intoSettings |
136
|
|
|
): void |
137
|
|
|
{ |
138
|
1 |
|
if ($category->getId() && $value = $this->extractAttributeValue($category)) { |
|
|
|
|
139
|
1 |
|
$handles = $intoSettings->getPageLayoutHandles() ?? []; |
140
|
1 |
|
$handles = array_merge_recursive( |
141
|
1 |
|
$handles, |
142
|
1 |
|
['selectable_0' => $value] |
143
|
|
|
); |
144
|
1 |
|
$intoSettings->setPageLayoutHandles($handles); |
145
|
|
|
} |
146
|
1 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Extract custom layout attribute value. |
150
|
|
|
* |
151
|
|
|
* @param CategoryInterface $category |
152
|
|
|
* @return mixed |
153
|
|
|
* |
154
|
|
|
* Unchanged private method copied over from @var LayoutUpdateManager |
155
|
|
|
*/ |
156
|
1 |
|
private function extractAttributeValue(CategoryInterface $category) |
157
|
|
|
{ |
158
|
1 |
|
if ($category instanceof Category && !$category->hasData(CategoryInterface::CUSTOM_ATTRIBUTES)) { |
159
|
1 |
|
return $category->getData('custom_layout_update_file'); |
160
|
|
|
} |
161
|
|
|
if ($attr = $category->getCustomAttribute('custom_layout_update_file')) { |
162
|
|
|
return $attr->getValue(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return null; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths