56 if (!$this->contentValidator->isValid($entryContent)) {
57 throw new InputException(
__(
'The image content is invalid. Verify the content and try again.'));
59 $product = $this->productRepository->get($sku,
true);
61 $existingMediaGalleryEntries =
$product->getMediaGalleryEntries();
62 $existingEntryIds = [];
63 if ($existingMediaGalleryEntries ==
null) {
64 $existingMediaGalleryEntries = [$entry];
66 foreach ($existingMediaGalleryEntries as $existingEntries) {
67 $existingEntryIds[$existingEntries->getId()] = $existingEntries->getId();
69 $existingMediaGalleryEntries[] = $entry;
71 $product->setMediaGalleryEntries($existingMediaGalleryEntries);
74 }
catch (InputException $inputException) {
75 throw $inputException;
76 }
catch (\Exception $e) {
77 throw new StateException(
__(
"The product can't be saved."));
80 foreach (
$product->getMediaGalleryEntries() as $entry) {
81 if (!isset($existingEntryIds[$entry->
getId()])) {
82 return $entry->
getId();
85 throw new StateException(
__(
'The new media gallery entry failed to save.'));
93 $product = $this->productRepository->get($sku,
true);
94 $existingMediaGalleryEntries =
$product->getMediaGalleryEntries();
95 if ($existingMediaGalleryEntries ==
null) {
97 __(
'No image with the provided ID was found. Verify the ID and try again.')
101 foreach ($existingMediaGalleryEntries as $key => $existingEntry) {
102 $entryTypes = (array)$entry->
getTypes();
103 $existingEntryTypes = (array)$existingMediaGalleryEntries[$key]->getTypes();
104 $existingMediaGalleryEntries[$key]->setTypes(array_diff($existingEntryTypes, $entryTypes));
106 if ($existingEntry->getId() == $entry->
getId()) {
111 $existingMediaGalleryEntries[$key] = $entry;
116 __(
'No image with the provided ID was found. Verify the ID and try again.')
119 $product->setMediaGalleryEntries($existingMediaGalleryEntries);
122 $this->productRepository->save(
$product);
132 public function remove($sku, $entryId)
134 $product = $this->productRepository->get($sku,
true);
135 $existingMediaGalleryEntries =
$product->getMediaGalleryEntries();
136 if ($existingMediaGalleryEntries ==
null) {
138 __(
'No image with the provided ID was found. Verify the ID and try again.')
142 foreach ($existingMediaGalleryEntries as $key => $entry) {
143 if ($entry->
getId() == $entryId) {
144 unset($existingMediaGalleryEntries[$key]);
151 __(
'No image with the provided ID was found. Verify the ID and try again.')
154 $product->setMediaGalleryEntries($existingMediaGalleryEntries);
155 $this->productRepository->save(
$product);
162 public function get($sku, $entryId)
165 $product = $this->productRepository->get($sku);
170 $mediaGalleryEntries =
$product->getMediaGalleryEntries();
171 foreach ($mediaGalleryEntries as $entry) {
172 if ($entry->getId() == $entryId) {
186 $product = $this->productRepository->get($sku);
188 return $product->getMediaGalleryEntries();
update($sku, ProductAttributeMediaGalleryEntryInterface $entry)
__construct(\Magento\Catalog\Api\ProductRepositoryInterface $productRepository, ImageContentValidatorInterface $contentValidator)