Skip to content

Commit 18dcdf0

Browse files
authored
Merge pull request magento#1720 from Nazar65/ASI-1692
Open in Media Gallery button doesn't search for the image if the Title has been Edited
2 parents 800bca8 + d7022ce commit 18dcdf0

File tree

6 files changed

+199
-6
lines changed

6 files changed

+199
-6
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\AdobeStockImageAdminUi\Controller\Adminhtml\Asset;
10+
11+
use Magento\Backend\App\Action;
12+
use Magento\Framework\App\Action\HttpPostActionInterface;
13+
use Magento\Framework\Controller\ResultFactory;
14+
use Psr\Log\LoggerInterface;
15+
use Magento\AdobeStockImageAdminUi\Model\Asset\GetMediaGalleryAssetByAdobeId;
16+
17+
/**
18+
* Backend controller for retrieving asset information by adobeId
19+
*/
20+
class GetMediaGalleryAsset extends Action implements HttpPostActionInterface
21+
{
22+
private const HTTP_OK = 200;
23+
private const HTTP_INTERNAL_ERROR = 500;
24+
private const HTTP_BAD_REQUEST = 400;
25+
26+
/**
27+
* @see _isAllowed()
28+
*/
29+
public const ADMIN_RESOURCE = 'Magento_Cms::media_gallery';
30+
31+
/**
32+
* @var GetMediaGalleryAssetByAdobeId
33+
*/
34+
private $getAssetByAdobeId;
35+
36+
/**
37+
* @var LoggerInterface
38+
*/
39+
private $logger;
40+
41+
/**
42+
* @param Action\Context $context
43+
* @param LoggerInterface $logger
44+
* @param GetMediaGalleryAssetByAdobeId $getAssetByAdobeId
45+
*/
46+
public function __construct(
47+
Action\Context $context,
48+
LoggerInterface $logger,
49+
GetMediaGalleryAssetByAdobeId $getAssetByAdobeId
50+
) {
51+
parent::__construct($context);
52+
53+
$this->logger = $logger;
54+
$this->getAssetByAdobeId = $getAssetByAdobeId;
55+
}
56+
57+
/**
58+
* @inheritdoc
59+
*/
60+
public function execute()
61+
{
62+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
63+
try {
64+
65+
$params = $this->getRequest()->getParams();
66+
$adobeId = isset($params['adobe_id']) ? $params['adobe_id'] : null;
67+
68+
if (empty($adobeId)) {
69+
$responseContent = [
70+
'success' => false,
71+
'message' => __('Adobe id is required.'),
72+
];
73+
$resultJson->setHttpResponseCode(self::HTTP_BAD_REQUEST);
74+
$resultJson->setData($responseContent);
75+
76+
return $resultJson;
77+
}
78+
79+
$responseCode = self::HTTP_OK;
80+
$responseContent = $this->getAssetByAdobeId->execute((int) $adobeId);
81+
} catch (\Exception $exception) {
82+
$responseCode = self::HTTP_INTERNAL_ERROR;
83+
$this->logger->critical($exception);
84+
$responseContent = [
85+
'success' => false,
86+
'message' => __('An error occurred on attempt to retrieve asset information.'),
87+
];
88+
}
89+
90+
$resultJson->setHttpResponseCode($responseCode);
91+
$resultJson->setData($responseContent);
92+
93+
return $resultJson;
94+
}
95+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\AdobeStockImageAdminUi\Model\Asset;
10+
11+
use Magento\AdobeStockAssetApi\Model\Asset\Command\LoadByIdsInterface;
12+
use Magento\MediaGalleryApi\Api\GetAssetsByIdsInterface;
13+
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
14+
use Magento\Framework\Reflection\DataObjectProcessor;
15+
16+
/**
17+
* Return media gallery asset by adobe id
18+
*/
19+
class GetMediaGalleryAssetByAdobeId
20+
{
21+
/**
22+
* @var LoadByIdsInterface
23+
*/
24+
private $getAssetByAdobeId;
25+
26+
/**
27+
* @var GetAssetsByIdsInterface
28+
*/
29+
private $getMediaGalleryAssetsById;
30+
31+
/**
32+
* @var DataObjectProcessor
33+
*/
34+
private $objectProcessor;
35+
36+
/**
37+
* Constructor
38+
*
39+
* @param LoadByIdsInterface $getAssetByAdobeId
40+
* @param GetAssetsByIdsInterface $getAssetById
41+
* @param DataObjectProcessor $objectProcessor
42+
*/
43+
public function __construct(
44+
LoadByIdsInterface $getAssetByAdobeId,
45+
GetAssetsByIdsInterface $getAssetById,
46+
DataObjectProcessor $objectProcessor
47+
) {
48+
$this->getAssetByAdobeId = $getAssetByAdobeId;
49+
$this->getMediaGalleryAssetsById = $getAssetById;
50+
$this->objectProcessor = $objectProcessor;
51+
}
52+
53+
/**
54+
* Return media gallery asset by adobe id
55+
*
56+
* @param int $adobeId
57+
* @return array
58+
*/
59+
public function execute(int $adobeId): array
60+
{
61+
$mediaGalleryId = $this->getAssetByAdobeId->execute([$adobeId])[$adobeId]->getMediaGalleryId();
62+
$asset = $this->getMediaGalleryAssetsById->execute([$mediaGalleryId]);
63+
64+
return $this->objectProcessor->buildOutputDataArray(current($asset), AssetInterface::class);
65+
}
66+
}

AdobeStockImageAdminUi/Ui/Component/Listing/Columns/ImagePreview.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public function prepare(): void
5656
'saveLicensedAndDownloadUrl' => $this->url->getUrl('adobe_stock/license/saveLicensed'),
5757
'confirmationUrl' => $this->url->getUrl('adobe_stock/license/confirmation'),
5858
'relatedImagesUrl' => $this->url->getUrl('adobe_stock/preview/relatedimages'),
59-
'buyCreditsUrl' => 'https://stock.adobe.com/'
59+
'getMediaGalleryAsset' => $this->url->getUrl('adobe_stock/asset/getmediagalleryasset'),
60+
'buyCreditsUrl' => 'https://stock.adobe.com/',
61+
'imageEditDetailsUrl' => $this->url->getUrl('media_gallery/image/details')
6062
]
6163
)
6264
);

AdobeStockImageAdminUi/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"magento/module-adobe-stock-client-api": "*",
1212
"magento/module-backend": "*",
1313
"magento/module-ui": "*",
14-
"magento/module-media-gallery-ui": "*"
14+
"magento/module-media-gallery-ui": "*",
15+
"magento/module-media-gallery-api": "*"
1516
},
1617
"suggest": {
1718
"magento/module-cms": "*"

AdobeStockImageAdminUi/view/adminhtml/web/js/components/grid/column/image-preview.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ define([
4444
mediaGallerySortBy: '${ $.mediaGallerySortBy }',
4545
mediaGallerySearchInput: '${ $.mediaGallerySearchInput }',
4646
mediaGalleryListingFilters: '${ $.mediaGalleryListingFilters }',
47+
getMediaGalleryAsset: '${ $.getMediaGalleryAsset }',
48+
imageEditDetailsUrl: '${ $.imageEditDetailsUrl }',
4749
listingPaging: '${ $.listingPaging }'
4850
}
4951
],

AdobeStockImageAdminUi/view/adminhtml/web/js/components/grid/column/preview/actions.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,47 @@ define([
138138
this.preview().getAdobeModal().trigger('closeModal');
139139

140140
if (!this.isMediaBrowser()) {
141-
this.selectImageInNewMediaGalleryBySearch(this.preview().displayedRecord().title);
141+
this.selectImageInNewMediaGalleryBySearch(this.preview().displayedRecord().id);
142142
} else {
143143
this.selectDisplayedImageForOldMediaGallery(this.preview().displayedRecord().path);
144144
}
145145
},
146146

147+
/**
148+
* Return adobe stock asset by adobe id
149+
*
150+
* @param {String} adobeId
151+
*/
152+
getAssetDetails: function (adobeId) {
153+
return $.ajax({
154+
url: this.getMediaGalleryAsset,
155+
data: {
156+
'adobe_id': adobeId
157+
},
158+
context: this,
159+
showLoader: true
160+
});
161+
},
162+
147163
/**
148164
* Select image in new media gallery via search input
149165
*
150-
* @param {String} title
166+
* @param {String} imageId
151167
*/
152-
selectImageInNewMediaGalleryBySearch: function (title) {
168+
selectImageInNewMediaGalleryBySearch: function (imageId) {
169+
var path;
170+
153171
this.mediaGalleryListingFilters().clear();
154-
this.mediaGallerySearchInput().apply(title);
172+
this.getAssetDetails(imageId).then(function (assetDetails) {
173+
path = assetDetails.path;
174+
path = path.substring(0, path.lastIndexOf('/'));
175+
176+
if (path !== '') {
177+
this.imageDirectory().locateNode(path);
178+
}
179+
this.mediaGallerySearchInput().apply(assetDetails.title);
180+
}.bind(this));
181+
155182
},
156183

157184
/**

0 commit comments

Comments
 (0)