10
10
use Magento \Framework \Exception \CouldNotSaveException ;
11
11
use Magento \Framework \Exception \NoSuchEntityException ;
12
12
use Magento \Framework \Exception \StateException ;
13
+ use Magento \Catalog \Api \Data \CategoryInterface ;
13
14
14
15
class CategoryRepository implements \Magento \Catalog \Api \CategoryRepositoryInterface
15
16
{
@@ -33,6 +34,11 @@ class CategoryRepository implements \Magento\Catalog\Api\CategoryRepositoryInter
33
34
*/
34
35
protected $ categoryResource ;
35
36
37
+ /**
38
+ * @var \Magento\Framework\Model\Entity\MetadataPool
39
+ */
40
+ protected $ metadataPool ;
41
+
36
42
/**
37
43
* List of fields that can used config values in case when value does not defined directly
38
44
*
@@ -44,15 +50,18 @@ class CategoryRepository implements \Magento\Catalog\Api\CategoryRepositoryInter
44
50
* @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
45
51
* @param \Magento\Catalog\Model\ResourceModel\Category $categoryResource
46
52
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
53
+ * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool
47
54
*/
48
55
public function __construct (
49
56
\Magento \Catalog \Model \CategoryFactory $ categoryFactory ,
50
57
\Magento \Catalog \Model \ResourceModel \Category $ categoryResource ,
51
- \Magento \Store \Model \StoreManagerInterface $ storeManager
58
+ \Magento \Store \Model \StoreManagerInterface $ storeManager ,
59
+ \Magento \Framework \Model \Entity \MetadataPool $ metadataPool
52
60
) {
53
61
$ this ->categoryFactory = $ categoryFactory ;
54
62
$ this ->categoryResource = $ categoryResource ;
55
63
$ this ->storeManager = $ storeManager ;
64
+ $ this ->metadataPool = $ metadataPool ;
56
65
}
57
66
58
67
/**
@@ -61,10 +70,21 @@ public function __construct(
61
70
public function save (\Magento \Catalog \Api \Data \CategoryInterface $ category )
62
71
{
63
72
$ existingData = $ category ->toFlatArray ();
73
+
64
74
/** 'available_sort_by' should be set separately because fields of array type are destroyed by toFlatArray() */
65
75
$ existingData ['available_sort_by ' ] = $ category ->getAvailableSortBy ();
76
+
66
77
if ($ category ->getId ()) {
78
+ $ metadata = $ this ->metadataPool ->getMetadata (
79
+ CategoryInterface::class
80
+ );
81
+
67
82
$ existingCategory = $ this ->get ($ category ->getId ());
83
+
84
+ $ existingData [$ metadata ->getLinkField ()] = $ existingCategory ->getData (
85
+ $ metadata ->getLinkField ()
86
+ );
87
+
68
88
if (isset ($ existingData ['image ' ]) && is_array ($ existingData ['image ' ])) {
69
89
$ existingData ['image_additional_data ' ] = $ existingData ['image ' ];
70
90
unset($ existingData ['image ' ]);
0 commit comments