Skip to content

Commit ff37e3f

Browse files
committed
magento/adobe-stock-integration#1504:[Related PR] Insert rendition images to the content from media gallery instead of original images - Extract logic from controller to a model to enable before plugin interception of parameters
1 parent 18da36b commit ff37e3f

File tree

2 files changed

+71
-19
lines changed

2 files changed

+71
-19
lines changed

app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/OnInsert.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,25 @@ class OnInsert extends \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images
1313
*/
1414
protected $resultRawFactory;
1515

16+
/**
17+
* @var \Magento\Cms\Model\Wysiwyg\Images\PrepareImage
18+
*/
19+
protected $prepareImage;
20+
1621
/**
1722
* @param \Magento\Backend\App\Action\Context $context
1823
* @param \Magento\Framework\Registry $coreRegistry
1924
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
25+
* @param \Magento\Cms\Model\Wysiwyg\Images\PrepareImage $prepareImage
2026
*/
2127
public function __construct(
2228
\Magento\Backend\App\Action\Context $context,
2329
\Magento\Framework\Registry $coreRegistry,
24-
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory
30+
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
31+
\Magento\Cms\Model\Wysiwyg\Images\PrepareImage $prepareImage
2532
) {
2633
$this->resultRawFactory = $resultRawFactory;
34+
$this->prepareImage = $prepareImage;
2735
parent::__construct($context, $coreRegistry);
2836
}
2937

@@ -34,26 +42,10 @@ public function __construct(
3442
*/
3543
public function execute()
3644
{
37-
$imagesHelper = $this->_objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class);
3845
$request = $this->getRequest();
3946

40-
$storeId = $request->getParam('store');
41-
42-
$filename = $request->getParam('filename');
43-
$filename = $imagesHelper->idDecode($filename);
44-
45-
$asIs = $request->getParam('as_is');
46-
47-
$forceStaticPath = $request->getParam('force_static_path');
48-
49-
$this->_objectManager->get(\Magento\Catalog\Helper\Data::class)->setStoreId($storeId);
50-
$imagesHelper->setStoreId($storeId);
51-
52-
if ($forceStaticPath) {
53-
$image = parse_url($imagesHelper->getCurrentUrl() . $filename, PHP_URL_PATH);
54-
} else {
55-
$image = $imagesHelper->getImageHtmlDeclaration($filename, $asIs);
56-
}
47+
/** @var \Magento\Cms\Model\Wysiwyg\Images\PrepareImage $image */
48+
$image = $this->prepareImage->execute($request->getParams());
5749

5850
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
5951
$resultRaw = $this->resultRawFactory->create();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\Cms\Model\Wysiwyg\Images;
10+
11+
use Magento\Catalog\Helper\Data;
12+
use Magento\Cms\Helper\Wysiwyg\Images as ImagesHelper;
13+
14+
class PrepareImage
15+
{
16+
/**
17+
* @var ImagesHelper
18+
*/
19+
private $imagesHelper;
20+
21+
/**
22+
* @var Data
23+
*/
24+
private $catalogHelper;
25+
26+
/**
27+
* PrepareImage constructor.
28+
* @param ImagesHelper $imagesHelper
29+
* @param Data $catalogHelper
30+
*/
31+
public function __construct(
32+
ImagesHelper $imagesHelper,
33+
Data $catalogHelper
34+
) {
35+
$this->imagesHelper = $imagesHelper;
36+
$this->catalogHelper = $catalogHelper;
37+
}
38+
39+
/**
40+
* @param array $data
41+
* @return string
42+
*/
43+
public function execute(array $data): string
44+
{
45+
$filename = $this->imagesHelper->idDecode($data['filename']);
46+
$storeId = (int)$data['store_id'];
47+
48+
$this->catalogHelper->setStoreId($storeId);
49+
$this->imagesHelper->setStoreId($storeId);
50+
51+
if ($data['force_static_path']) {
52+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
53+
$image = parse_url($this->imagesHelper->getCurrentUrl() . $filename, PHP_URL_PATH);
54+
} else {
55+
$image = $this->imagesHelper->getImageHtmlDeclaration($filename, $data['as_is']);
56+
}
57+
58+
return $image;
59+
}
60+
}

0 commit comments

Comments
 (0)