diff --git a/Api/ProductGalleryManagementInterface.php b/Api/ProductGalleryManagementInterface.php index 3fc70d2..dd20b79 100644 --- a/Api/ProductGalleryManagementInterface.php +++ b/Api/ProductGalleryManagementInterface.php @@ -45,6 +45,14 @@ public function addProductMedia($sku, $urls); */ public function getProductMedia($sku); + /** + * Get product gallery items. + * @method getProductMedia + * @param string $sku + * @return string + */ + public function getProductMediaData($sku); + /** * Get products gallery items as Cloudinary URLs. * @method getProductsMedia diff --git a/Model/Api/ProductGalleryManagement.php b/Model/Api/ProductGalleryManagement.php index 7bb673a..23bdf84 100644 --- a/Model/Api/ProductGalleryManagement.php +++ b/Model/Api/ProductGalleryManagement.php @@ -285,7 +285,15 @@ public function addProductMedia($sku, $urls) */ public function getProductMedia($sku) { - return $this->_getProductMedia($sku); + return $this->_getProductMedia($sku, true); + } + + /** + * {@inheritdoc} + */ + public function getProductMediaData($sku) + { + return $this->_getProductMedia($sku, false); } /** @@ -302,7 +310,7 @@ public function getProductsMedia($skus) * @param mixed $sku * @return string (json result) */ - private function _getProductMedia($sku) + private function _getProductMedia($sku, $onlyUrls = true) { $result = ["data" => []]; @@ -311,10 +319,10 @@ private function _getProductMedia($sku) $this->checkEnabled(); if (is_array($sku) || is_object($sku)) { foreach ($sku as $key => $_sku) { - $result['data'][$_sku] = $this->getProductCldUrlsBySku($_sku); + $result['data'][$_sku] = ($onlyUrls ? $this->getProductCldUrlsBySku($_sku) : $this->getProductCldDataBySku($_sku)); } } else { - $result['data'] = $this->getProductCldUrlsBySku($sku); + $result['data'] = ($onlyUrls ? $this->getProductCldUrlsBySku($sku) : $this->getProductCldDataBySku($sku)); } } catch (\Exception $e) { $result["error"] = 1; @@ -768,6 +776,36 @@ private function getProductCldUrlsBySku($sku) return $urls; } + /** + * @method getProductCldDataBySku + * @param string $sku + * @return array + */ + private function getProductCldDataBySku($sku) + { + $data = []; + try { + $product = $this->productRepository->get($sku); + $attributes = $product->getAttributes(); + foreach ($product->getMediaGalleryImages() as $gallItem) { + $gallItemData = $gallItem->getData(); + foreach ($attributes as $attribute) { + $attrCode = $attribute->getAttributeCode(); + if($product->getData($attrCode) === $gallItem->getFile()) { + $gallItemData['role'] = $attrCode; + } + } + array_push($data, $gallItemData); + } + } catch (\Exception $e) { + $data = [ + 'error' => 1, + 'message' => $e->getMessage() + ]; + } + return $data; + } + /////////////////////////////// // App Environment Emulation // /////////////////////////////// diff --git a/Model/GraphQLResolver/ProductAttributeCldDataResolver.php b/Model/GraphQLResolver/ProductAttributeCldDataResolver.php new file mode 100644 index 0000000..23eaf47 --- /dev/null +++ b/Model/GraphQLResolver/ProductAttributeCldDataResolver.php @@ -0,0 +1,42 @@ +productGalleryManagement = $productGalleryManagement; + } + + /** + * @inheritdoc + */ + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) + { + $productId = $value['sku']; + $productMediaStr = $this->productGalleryManagement->getProductMediaData($productId); + $jsonDecoder = new \Magento\Framework\Serialize\Serializer\Json(); + $productMedia = $jsonDecoder->unserialize($productMediaStr); + return $productMedia['data']; + } + } \ No newline at end of file diff --git a/etc/schema.graphqls b/etc/schema.graphqls index 8d2e385..c68abcf 100644 --- a/etc/schema.graphqls +++ b/etc/schema.graphqls @@ -5,8 +5,38 @@ type CloudinaryData { media_gallery: [String] } +type CloudinaryImage { + role: String + disabled: String + disabled_default: String + entity_id: String + file: String + id: String + label: String + label_default: String + media_type: String + path: String + position: String + position_default: String + url: String + value_id: String + video_description: String + video_description_default: String + video_metadata: String + video_metadata_default: String + video_provider: String + video_provider_default: String + video_title: String + video_title_default: String + video_url: String + video_url_default: String +} + interface ProductInterface { cld_data: CloudinaryData @resolver(class: "\\Cloudinary\\Cloudinary\\Model\\GraphQLResolver\\ProductAttributeCldResolver") @doc(description: "Cloudinary urls generated for product images") + cloudinary_images: [CloudinaryImage] + @resolver(class: "\\Cloudinary\\Cloudinary\\Model\\GraphQLResolver\\ProductAttributeCldDataResolver") + @doc(description: "Cloudinary urls generated for product images") } \ No newline at end of file