From f8a344463e0196b9ba7f2c4880997c8fa3f7acd0 Mon Sep 17 00:00:00 2001 From: Ariel Ashri Date: Thu, 17 Aug 2023 09:13:52 +0300 Subject: [PATCH 1/4] CLOUDINARY-459 - show cloudinary image on admins cms preview --- .../Adminhtml/Ajax/UpdateAdminImage.php | 130 ++++++++++++++++++ view/adminhtml/layout/cms_page_edit.xml | 15 ++ view/adminhtml/requirejs-config.js | 6 +- view/adminhtml/templates/cms/images.phtml | 12 ++ view/adminhtml/web/js/cms/preview-update.js | 54 ++++++++ 5 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 Controller/Adminhtml/Ajax/UpdateAdminImage.php create mode 100644 view/adminhtml/layout/cms_page_edit.xml create mode 100644 view/adminhtml/templates/cms/images.phtml create mode 100644 view/adminhtml/web/js/cms/preview-update.js diff --git a/Controller/Adminhtml/Ajax/UpdateAdminImage.php b/Controller/Adminhtml/Ajax/UpdateAdminImage.php new file mode 100644 index 0000000..4ec672d --- /dev/null +++ b/Controller/Adminhtml/Ajax/UpdateAdminImage.php @@ -0,0 +1,130 @@ +imageFactory = $imageFactory; + $this->urlGenerator = $urlGenerator; + $this->configuration = $configuration; + $this->storeManager = $storeManager; + $this->urlInterface = $urlInterface; + $this->resultFactory = $resultFactory; + $this->filesystem = $filesystem; + $this->configurationBuilder = $configurationBuilder; + $this->transformation = $transformation; + } + + private function authorise() + { + if (!$this->_authorised && $this->configuration->isEnabled()) { + Configuration::instance($this->configurationBuilder->build()); + BaseApiClient::$userPlatform = $this->configuration->getUserPlatform(); + $this->_authorised = true; + } + } + + public function execute() + { + $this->authorise(); + if ($this->configuration->isEnabled()) { + try{ + $remoteImageUrl = $this->getRequest()->getParam('remote_image'); + $filedId = str_replace($this->storeManager->getStore()->getBaseUrl(), '', $remoteImageUrl); + + $result = Media::fromParams( + $filedId, + [ 'transformation' => $this->transformation->build(), + 'secure' => true, + 'sign_url' => $this->configuration->getUseSignedUrls(), + 'version' => 1 + ] + ) . '?_i=AB'; + + + + + } catch (\Exception $e) { + $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()]; + } + } + + + $response = $this->resultFactory->create(); + $response->setHeader('Content-type', 'text/plain'); + $response->setContents(json_encode($result)); + return $response; + } +} diff --git a/view/adminhtml/layout/cms_page_edit.xml b/view/adminhtml/layout/cms_page_edit.xml new file mode 100644 index 0000000..ec32bb2 --- /dev/null +++ b/view/adminhtml/layout/cms_page_edit.xml @@ -0,0 +1,15 @@ + + + + + + + + + + diff --git a/view/adminhtml/requirejs-config.js b/view/adminhtml/requirejs-config.js index 303017d..ab5f2c3 100644 --- a/view/adminhtml/requirejs-config.js +++ b/view/adminhtml/requirejs-config.js @@ -8,7 +8,8 @@ var config = { cloudinarySpinsetModal: 'Cloudinary_Cloudinary/js/cloudinary-spinset-modal', cldspinsetDialog: 'Cloudinary_Cloudinary/js/cloudinary-spinset-dialog', productGallery: 'Cloudinary_Cloudinary/js/product-gallery', - cloudinaryLazyload: 'Cloudinary_Cloudinary/js/cloudinary-lazyload' + cloudinaryLazyload: 'Cloudinary_Cloudinary/js/cloudinary-lazyload', + updateCmsImages: 'Cloudinary_Cloudinary/js/cms/preview-update', } }, paths: { @@ -24,6 +25,9 @@ var config = { 'uiComponent': { deps: ['jquery'] }, + 'Cloudinary_Cloudinary/js/cms/preview-update': { + deps: ['jquery'] + } }, config: { mixins: { diff --git a/view/adminhtml/templates/cms/images.phtml b/view/adminhtml/templates/cms/images.phtml new file mode 100644 index 0000000..90062ec --- /dev/null +++ b/view/adminhtml/templates/cms/images.phtml @@ -0,0 +1,12 @@ + + + diff --git a/view/adminhtml/web/js/cms/preview-update.js b/view/adminhtml/web/js/cms/preview-update.js new file mode 100644 index 0000000..9498264 --- /dev/null +++ b/view/adminhtml/web/js/cms/preview-update.js @@ -0,0 +1,54 @@ +define( + ['jquery','Magento_PageBuilder/js/events','mage/url'], + function($,_PBEvents, urlBuilder){ + 'use strict' + return function (config, element) { + var updateHandler = { + images: [], + init: function () { + let self = this; + _PBEvents.on('image:renderAfter', function (event){ + let elem = event.element, key = event.id; + let image = $(elem).find('img'); + let src = {'remote_image': image.attr('src')}; + self.images.push(src); + + self.update(key); + }); + $('#save-button').on('click', function (e){ + alert('saving...'); + self.images.each(function(elem){ + if (elem.cld_image) { + let cld_src = elem.cld_image; + let img = $('img[src="' + cld_src +'"]'); + return (img.length) ? img.attr('src', elem.remote_image) : ''; + } + }); + }); + }, + update: function(key) { + let self = this; + this.images.each(function(elem,ind){ + $.ajax({ + url: config.ajaxUrl, + type: 'POST', + dataType: 'json', + data: elem, + success: function(image) { + self.images[ind].cld_image = image; + let img = $('img[src="' + self.images[ind].remote_image +'"]'); + if (img.length) { + $('img[src="' + self.images[ind].remote_image +'"]').attr('src', self.images[ind].cld_image); + } + }, + error: function(xhr, textStatus, errorThrown) { + console.log('Error:', textStatus, errorThrown); + } + }); + + }) + } + }; + return updateHandler.init(); + } + }); From 010c540c52db591ed3adc612cbd0b3ed2ea89488 Mon Sep 17 00:00:00 2001 From: Ariel Ashri Date: Thu, 17 Aug 2023 09:38:36 +0300 Subject: [PATCH 2/4] CLOUDINARY-459 - show cloudinary image on admins cms preview - added support to cms block --- view/adminhtml/layout/cms_block_edit.xml | 14 ++++++++++++++ view/adminhtml/web/js/cms/preview-update.js | 1 - 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 view/adminhtml/layout/cms_block_edit.xml diff --git a/view/adminhtml/layout/cms_block_edit.xml b/view/adminhtml/layout/cms_block_edit.xml new file mode 100644 index 0000000..a00d101 --- /dev/null +++ b/view/adminhtml/layout/cms_block_edit.xml @@ -0,0 +1,14 @@ + + + + + + + + + diff --git a/view/adminhtml/web/js/cms/preview-update.js b/view/adminhtml/web/js/cms/preview-update.js index 9498264..9c511a1 100644 --- a/view/adminhtml/web/js/cms/preview-update.js +++ b/view/adminhtml/web/js/cms/preview-update.js @@ -16,7 +16,6 @@ define( self.update(key); }); $('#save-button').on('click', function (e){ - alert('saving...'); self.images.each(function(elem){ if (elem.cld_image) { let cld_src = elem.cld_image; From f0f6891105b46bb06e608d7b092b78bfa3039c70 Mon Sep 17 00:00:00 2001 From: Ariel Ashri Date: Thu, 24 Aug 2023 17:03:20 +0300 Subject: [PATCH 3/4] update version number --- composer.json | 2 +- etc/module.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index c1ba0ef..fd043af 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "cloudinary/cloudinary", "description": "Cloudinary Magento 2 Integration.", "type": "magento2-module", - "version": "1.19.6", + "version": "1.19.7", "license": "MIT", "require": { "cloudinary/cloudinary_php": ">=2.7 <2.8.1" diff --git a/etc/module.xml b/etc/module.xml index 92d81e2..c36d301 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - + From 8f680c969cf65d744463813670f753e4c81d492c Mon Sep 17 00:00:00 2001 From: Adam Rahwane Date: Mon, 28 Aug 2023 12:01:28 +0300 Subject: [PATCH 4/4] make sure cloudinary module is enabled before attempting to get cname in media library helper --- Helper/MediaLibraryHelper.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Helper/MediaLibraryHelper.php b/Helper/MediaLibraryHelper.php index 4d0376c..30d01f5 100644 --- a/Helper/MediaLibraryHelper.php +++ b/Helper/MediaLibraryHelper.php @@ -106,7 +106,6 @@ public function getCloudinaryMLshowOptions($resourceType = null, $path = "") */ public function getCname() { - $cname = isset($this->configuration->getCredentials()['cname']) ? $this->configuration->getCredentials()['cname'] : null; - return $cname; + return $this->configuration->isEnabled() && isset($this->configuration->getCredentials()['cname']) ? $this->configuration->getCredentials()['cname'] : null; } }