/** * {@inheritdoc} */ public function create($sku, ProductAttributeMediaGalleryEntryInterface $entry) { /** @var $entry ProductAttributeMediaGalleryEntryInterface */ $entryContent = $entry->getContent(); if (!$this->contentValidator->isValid($entryContent)) { throw new InputException(__('The image content is not valid.')); } $product = $this->productRepository->get($sku); $existingMediaGalleryEntries = $product->getMediaGalleryEntries(); $existingEntryIds = []; if ($existingMediaGalleryEntries == null) { $existingMediaGalleryEntries = [$entry]; } else { foreach ($existingMediaGalleryEntries as $existingEntries) { $existingEntryIds[$existingEntries->getId()] = $existingEntries->getId(); } $existingMediaGalleryEntries[] = $entry; } $product->setMediaGalleryEntries($existingMediaGalleryEntries); try { $product = $this->productRepository->save($product); } catch (InputException $inputException) { throw $inputException; } catch (\Exception $e) { throw new StateException(__('Cannot save product.')); } foreach ($product->getMediaGalleryEntries() as $entry) { if (!isset($existingEntryIds[$entry->getId()])) { return $entry->getId(); } } throw new StateException(__('Failed to save new media gallery entry.')); }
/** * @param ProductAttributeMediaGalleryEntryInterface $entry * @return array */ protected function convertFromMediaGalleryInterface(ProductAttributeMediaGalleryEntryInterface $entry) { $entryArray = ["value_id" => $entry->getId(), "file" => $entry->getFile(), "label" => $entry->getLabel(), "position" => $entry->getPosition(), "disabled" => $entry->isDisabled(), "types" => $entry->getTypes(), "content" => $this->convertFromMediaGalleryEntryContentInterface($entry->getContent())]; return $entryArray; }
/** * @param ProductAttributeMediaGalleryEntryInterface $entry * @return array */ public function convertFrom(ProductAttributeMediaGalleryEntryInterface $entry) { $entryArray = ['value_id' => $entry->getId(), 'file' => $entry->getFile(), 'label' => $entry->getLabel(), 'position' => $entry->getPosition(), 'disabled' => $entry->isDisabled(), 'types' => $entry->getTypes(), 'media_type' => $entry->getMediaType(), 'content' => $this->convertFromMediaGalleryEntryContentInterface($entry->getContent())]; return $entryArray; }