|
8 | 8 | use Magento\Catalog\Model\ProductFactory;
|
9 | 9 | use Magento\Catalog\Model\ResourceModel\Product\Gallery as GalleryResource;
|
10 | 10 | use Magento\Framework\Setup\SampleData\Context as SampleDataContext;
|
| 11 | +use Magento\Framework\App\ObjectManager; |
| 12 | +use Magento\Catalog\Api\Data\ProductInterface; |
11 | 13 |
|
12 | 14 | /**
|
13 | 15 | * Class Gallery
|
@@ -44,6 +46,11 @@ class Gallery
|
44 | 46 | */
|
45 | 47 | protected $eavConfig;
|
46 | 48 |
|
| 49 | + /** |
| 50 | + * @var \Magento\Framework\EntityManager\MetadataPool |
| 51 | + */ |
| 52 | + private $metadataPool; |
| 53 | + |
47 | 54 | /**
|
48 | 55 | * @param SampleDataContext $sampleDataContext
|
49 | 56 | * @param ProductFactory $productFactory
|
@@ -122,15 +129,16 @@ protected function storeImage($product, $images)
|
122 | 129 | if (strpos($image, '_main') !== false) {
|
123 | 130 | $baseImage = $image;
|
124 | 131 | }
|
| 132 | + $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); |
125 | 133 | $id = $this->galleryResource->insertGallery([
|
126 | 134 | 'attribute_id' => $mediaAttribute->getAttributeId(),
|
127 |
| - 'entity_id' => $product->getId(), |
| 135 | + $linkField => $product->getId(), |
128 | 136 | 'value' => $image,
|
129 | 137 | ]);
|
130 | 138 | $this->galleryResource->insertGalleryValueInStore([
|
131 | 139 | 'value_id' => $id,
|
132 | 140 | 'store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
|
133 |
| - 'entity_id' => $product->getId(), |
| 141 | + $linkField => $product->getId(), |
134 | 142 | 'label' => 'Image',
|
135 | 143 | 'position' => $i,
|
136 | 144 | 'disables' => 0,
|
@@ -160,4 +168,20 @@ protected function storeImage($product, $images)
|
160 | 168 | }
|
161 | 169 | }
|
162 | 170 | }
|
| 171 | + |
| 172 | + /** |
| 173 | + * @deprecated |
| 174 | + * |
| 175 | + * @return \Magento\Framework\EntityManager\MetadataPool|mixed |
| 176 | + */ |
| 177 | + private function getMetadataPool() |
| 178 | + { |
| 179 | + if (!($this->metadataPool)) { |
| 180 | + return ObjectManager::getInstance()->get( |
| 181 | + '\Magento\Framework\EntityManager\MetadataPool' |
| 182 | + ); |
| 183 | + } else { |
| 184 | + return $this->metadataPool; |
| 185 | + } |
| 186 | + } |
163 | 187 | }
|
0 commit comments