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