Skip to content

Commit 0751c41

Browse files
committed
Merge branch 'ACP2E-16' of https://github.com/magento-l3/magento2ce into PR-2022-02-15-CE2
2 parents 4a32270 + 0890d00 commit 0751c41

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Test\Fixture;
9+
10+
use Magento\Framework\DataObject;
11+
12+
class MultiselectAttribute extends SelectAttribute
13+
{
14+
private const DEFAULT_DATA = [
15+
'frontend_input' => 'multiselect',
16+
];
17+
18+
/**
19+
* @inheritdoc
20+
*/
21+
public function apply(array $data = []): ?DataObject
22+
{
23+
$data = $this->prepareData($data);
24+
25+
return parent::apply($data);
26+
}
27+
28+
/**
29+
* Prepare attribute data
30+
*
31+
* @param array $data
32+
* @return array
33+
*/
34+
private function prepareData(array $data): array
35+
{
36+
$data = array_merge(self::DEFAULT_DATA, $data);
37+
38+
return $data;
39+
}
40+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Test\Fixture;
9+
10+
use Magento\Framework\DataObject;
11+
12+
class SelectAttribute extends Attribute
13+
{
14+
private const DEFAULT_DATA = [
15+
'frontend_input' => 'select',
16+
'options' => [
17+
[
18+
'label' => 'option_1',
19+
'sort_order' => 0,
20+
],
21+
[
22+
'label' => 'option_2',
23+
'sort_order' => 1,
24+
],
25+
],
26+
];
27+
28+
/**
29+
* {@inheritdoc}
30+
* @param array $data Parameters. Same format as \Magento\Catalog\Test\Fixture\Attribute::DEFAULT_DATA.
31+
* Additional fields:
32+
* - $data['options']: Array of options.
33+
* Option ID can be retrieved as follows:
34+
* <pre>
35+
* $attribute->getData('option_1')
36+
* </pre>
37+
*/
38+
public function apply(array $data = []): ?DataObject
39+
{
40+
$data = $this->prepareData($data);
41+
42+
$attribute = parent::apply($data);
43+
44+
// add options data to attribute data [option_label => option_id]
45+
$options = $attribute->getSource()->getAllOptions(false);
46+
$attribute->addData(array_column($options, 'value', 'label'));
47+
48+
return $attribute;
49+
}
50+
51+
/**
52+
* Prepare attribute data
53+
*
54+
* @param array $data
55+
* @return array
56+
*/
57+
private function prepareData(array $data): array
58+
{
59+
$data = array_merge(self::DEFAULT_DATA, $data);
60+
61+
$options = [];
62+
$sortOrder = 0;
63+
foreach ($data['options'] as $option) {
64+
$options[] = [
65+
'label' => is_array($option) ? $option['label'] : $option,
66+
'sort_order' => $sortOrder++,
67+
];
68+
}
69+
70+
$data['options'] = $options;
71+
72+
return $data;
73+
}
74+
}

0 commit comments

Comments
 (0)