Skip to content

Commit 50d8385

Browse files
committed
CLOUDINARY-525 - Additional graphql media enhancements
1 parent 3e7c748 commit 50d8385

5 files changed

+73
-8
lines changed

Model/Api/ProductGalleryManagement.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ private function getProductCldUrlsBySku($sku)
850850
$urls['thumbnail'] = $gallItem->getUrl();
851851
}
852852
}
853-
$urls['gallery_widget_parameters'] = $this->jsonHelper->jsonEncode($this->productGalleryHelper->getCloudinaryPGOptions());
853+
$urls['gallery_widget_parameters'] = $this->jsonHelper->jsonEncode($this->productGalleryHelper->getCloudinaryPGOptions(true, true));
854854
} catch (\Exception $e) {
855855
$urls = [
856856
'error' => 1,

Model/GraphQLResolver/ProductAttributeCldResolver.php

+62-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
declare(strict_types=1);
33
namespace Cloudinary\Cloudinary\Model\GraphQLResolver;
44

5+
use Cloudinary\Cloudinary\Model\Configuration;
6+
use Magento\Framework\Exception\LocalizedException;
57
use Magento\Framework\GraphQl\Config\Element\Field;
68
use Magento\Framework\GraphQl\Query\ResolverInterface;
79
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
810
use Cloudinary\Cloudinary\Model\Api\ProductGalleryManagement;
911
use Magento\Framework\GraphQl\Type\Definition\ObjectType;
10-
12+
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\Framework\UrlInterface;
1115
/**
1216
* Class ProductAttributeCldResolver
1317
**/
@@ -18,25 +22,78 @@ class ProductAttributeCldResolver implements ResolverInterface
1822
*/
1923
private $productGalleryManagement;
2024

25+
protected $productRepository;
26+
27+
private $urlBuilder;
28+
private $storeManager;
29+
30+
protected $_sku;
31+
32+
protected $_configuration;
33+
2134
/**
2235
* ProductAttributeCldResolver constructor.
2336
* @param ProductGalleryManagement $productGalleryManagement
2437
*/
2538
public function __construct(
26-
ProductGalleryManagement $productGalleryManagement
39+
ProductGalleryManagement $productGalleryManagement,
40+
ProductRepositoryInterface $productRepository,
41+
Configuration $configuration,
42+
UrlInterface $urlBuilder,
43+
StoreManagerInterface $storeManager,
2744
) {
2845
$this->productGalleryManagement = $productGalleryManagement;
46+
$this->productRepository = $productRepository;
47+
$this->urlBuilder = $urlBuilder;
48+
$this->storeManager = $storeManager;
49+
$this->_configuration = $configuration;
2950
}
3051

3152
/**
3253
* @inheritdoc
3354
*/
3455
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
3556
{
36-
$productId = $value['sku'];
37-
$productMediaStr = $this->productGalleryManagement->getProductMedia($productId);
57+
$sku = $value['sku'] ?? null;
58+
if ($sku) {
59+
$this->_sku = $sku;
60+
}
61+
62+
$attributeCodes = $args['attribute_codes'] ?? null;
63+
if ($attributeCodes && is_array($attributeCodes)) {
64+
$this->checkEnabled();
65+
$product = $this->productRepository->get($this->_sku);
66+
$mediaAttributes = [];
67+
foreach ($attributeCodes as $attributeCode) {
68+
$attrValue = $product->getData($attributeCode);
69+
if ($attrValue) {
70+
foreach ($product->getMediaGalleryImages() as $gallItem) {
71+
if ($attrValue == $gallItem->getFile()) {
72+
$mediaAttributes[] = [
73+
'attribute_code' => $attributeCode,
74+
'url' => $gallItem->getUrl()
75+
];
76+
}
77+
}
78+
79+
}
80+
}
81+
return $mediaAttributes;
82+
}
83+
$productMediaStr = $this->productGalleryManagement->getProductMedia($this->_sku);
3884
$jsonDecoder = new \Magento\Framework\Serialize\Serializer\Json();
3985
$productMedia = $jsonDecoder->unserialize($productMediaStr);
86+
4087
return $productMedia['data'];
4188
}
42-
}
89+
90+
91+
private function checkEnabled() {
92+
if (!$this->_configuration->isEnabled()) {
93+
throw new LocalizedException(
94+
__("Cloudinary module is disabled. Please enable it first in order to use this API.")
95+
);
96+
}
97+
return $this;
98+
}
99+
}

etc/module.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3-
<module name="Cloudinary_Cloudinary" setup_version="1.20.4">
3+
<module name="Cloudinary_Cloudinary" setup_version="1.20.5">
44
<sequence>
55
<module name="Magento_ProductVideo"/>
66
<module name="Magento_PageBuilder"/>

etc/schema.graphqls

+8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
type CustomMediaAttribute {
2+
attribute_code: String
3+
url: String
4+
}
5+
16
type CloudinaryData {
27
image: String
38
small_image: String
49
thumbnail: String
510
media_gallery: [String]
611
gallery_widget_parameters: String @doc(description: "Cloudinary gallery widget parameters (json)")
12+
custom_media_attributes(attribute_codes: [String]!): [CustomMediaAttribute]
13+
@resolver(class: "\\Cloudinary\\Cloudinary\\Model\\GraphQLResolver\\ProductAttributeCldResolver")
14+
@doc(description: "Fetch a custom media attributes for the product based on the provided attribute code.")
715
}
816

917
interface ProductInterface {

marketplace.composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"version": "1.18.0",
66
"license": "MIT",
77
"require": {
8-
"cloudinary/cloudinary_php": "^1.20.4"
8+
"cloudinary/cloudinary_php": "^1.20.5"
99
},
1010
"autoload": {
1111
"files": [

0 commit comments

Comments
 (0)