Exemplo n.º 1
0
 /**
  * Retrieve backend model of product media gallery attribute
  *
  * @param Product $product
  * @return \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
  * @throws StateException
  */
 protected function getGalleryAttributeBackend(Product $product)
 {
     $galleryAttributeBackend = $product->getGalleryAttributeBackend();
     if ($galleryAttributeBackend == null) {
         throw new StateException(__('Requested product does not support images.'));
     }
     return $galleryAttributeBackend;
 }
Exemplo n.º 2
0
 /**
  * @param ProductInterface $product
  * @param array $mediaGalleryEntries
  * @return $this
  * @throws InputException
  * @throws StateException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function processMediaGallery(ProductInterface $product, $mediaGalleryEntries)
 {
     $existingMediaGallery = $product->getMediaGallery('images');
     $newEntries = [];
     if (!empty($existingMediaGallery)) {
         $entriesById = [];
         foreach ($mediaGalleryEntries as $entry) {
             if (isset($entry['id'])) {
                 $entry['value_id'] = $entry['id'];
                 $entriesById[$entry['value_id']] = $entry;
             } else {
                 $newEntries[] = $entry;
             }
         }
         foreach ($existingMediaGallery as $key => &$existingEntry) {
             if (isset($entriesById[$existingEntry['value_id']])) {
                 $updatedEntry = $entriesById[$existingEntry['value_id']];
                 $existingMediaGallery[$key] = array_merge($existingEntry, $updatedEntry);
             } else {
                 //set the removed flag
                 $existingEntry['removed'] = true;
             }
         }
         $product->setData('media_gallery', ["images" => $existingMediaGallery]);
     } else {
         $newEntries = $mediaGalleryEntries;
     }
     /** @var \Magento\Catalog\Model\Product\Attribute\Backend\Media $galleryAttributeBackend */
     $galleryAttributeBackend = $product->getGalleryAttributeBackend();
     $galleryAttributeBackend->clearMediaAttribute($product, array_keys($product->getMediaAttributes()));
     $images = $product->getMediaGallery('images');
     if ($images) {
         foreach ($images as $image) {
             if (!isset($image['removed']) && !empty($image['types'])) {
                 $galleryAttributeBackend->setMediaAttribute($product, $image['types'], $image['file']);
             }
         }
     }
     foreach ($newEntries as $newEntry) {
         if (!isset($newEntry['content'])) {
             throw new InputException(__('The image content is not valid.'));
         }
         /** @var ImageContentInterface $contentDataObject */
         $contentDataObject = $this->contentFactory->create()->setName($newEntry['content'][ImageContentInterface::NAME])->setBase64EncodedData($newEntry['content'][ImageContentInterface::BASE64_ENCODED_DATA])->setType($newEntry['content'][ImageContentInterface::TYPE]);
         $newEntry['content'] = $contentDataObject;
         $this->processNewMediaGalleryEntry($product, $newEntry);
     }
     return $this;
 }