Skip to content

Commit 29d0707

Browse files
committed
magento/adobe-stock-integration#1504: Insert rendition images to the content from media gallery instead of original images - Extracted logic from wysiwyg OnInsert controller to a model
1 parent ff37e3f commit 29d0707

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,26 @@ class OnInsert extends \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images
1414
protected $resultRawFactory;
1515

1616
/**
17-
* @var \Magento\Cms\Model\Wysiwyg\Images\PrepareImage
17+
* @var \Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent
1818
*/
19-
protected $prepareImage;
19+
protected $getInsertImageContent;
2020

2121
/**
2222
* @param \Magento\Backend\App\Action\Context $context
2323
* @param \Magento\Framework\Registry $coreRegistry
2424
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
25-
* @param \Magento\Cms\Model\Wysiwyg\Images\PrepareImage $prepareImage
25+
* @param \Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent $getInsertImageContent
2626
*/
2727
public function __construct(
2828
\Magento\Backend\App\Action\Context $context,
2929
\Magento\Framework\Registry $coreRegistry,
3030
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
31-
\Magento\Cms\Model\Wysiwyg\Images\PrepareImage $prepareImage
31+
?\Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent $getInsertImageContent = null
3232
) {
3333
$this->resultRawFactory = $resultRawFactory;
34-
$this->prepareImage = $prepareImage;
3534
parent::__construct($context, $coreRegistry);
35+
$this->getInsertImageContent = $getInsertImageContent ?: $this->_objectManager
36+
->get('Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent');
3637
}
3738

3839
/**
@@ -42,13 +43,18 @@ public function __construct(
4243
*/
4344
public function execute()
4445
{
45-
$request = $this->getRequest();
46-
47-
/** @var \Magento\Cms\Model\Wysiwyg\Images\PrepareImage $image */
48-
$image = $this->prepareImage->execute($request->getParams());
46+
$data = $this->getRequest()->getParams();
4947

5048
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
5149
$resultRaw = $this->resultRawFactory->create();
52-
return $resultRaw->setContents($image);
50+
51+
return $resultRaw->setContents(
52+
$this->getInsertImageContent->execute(
53+
$data['filename'],
54+
(int)$data['store_id'],
55+
$data['force_static_path'],
56+
$data['as_is']
57+
)
58+
);
5359
}
5460
}

app/code/Magento/Cms/Model/Wysiwyg/Images/PrepareImage.php renamed to app/code/Magento/Cms/Model/Wysiwyg/Images/GetInsertImageContent.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\Catalog\Helper\Data;
1212
use Magento\Cms\Helper\Wysiwyg\Images as ImagesHelper;
1313

14-
class PrepareImage
14+
class GetInsertImageContent
1515
{
1616
/**
1717
* @var ImagesHelper
@@ -25,6 +25,7 @@ class PrepareImage
2525

2626
/**
2727
* PrepareImage constructor.
28+
*
2829
* @param ImagesHelper $imagesHelper
2930
* @param Data $catalogHelper
3031
*/
@@ -37,24 +38,33 @@ public function __construct(
3738
}
3839

3940
/**
40-
* @param array $data
41+
* Prepare Image Contents for Insert
42+
*
43+
* @param string $encodedFilename
44+
* @param int $storeId
45+
* @param bool $forceStaticPath
46+
* @param bool $renderAsTag
4147
* @return string
4248
*/
43-
public function execute(array $data): string
44-
{
45-
$filename = $this->imagesHelper->idDecode($data['filename']);
46-
$storeId = (int)$data['store_id'];
49+
public function execute(
50+
string $encodedFilename,
51+
int $storeId,
52+
bool $forceStaticPath,
53+
bool $renderAsTag
54+
): string {
55+
$filename = $this->imagesHelper->idDecode($encodedFilename);
4756

4857
$this->catalogHelper->setStoreId($storeId);
4958
$this->imagesHelper->setStoreId($storeId);
5059

51-
if ($data['force_static_path']) {
60+
if ($forceStaticPath) {
5261
// 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']);
62+
return parse_url(
63+
$this->imagesHelper->getCurrentUrl() . $filename,
64+
PHP_URL_PATH
65+
);
5666
}
5767

58-
return $image;
68+
return $this->imagesHelper->getImageHtmlDeclaration($filename, $renderAsTag);
5969
}
6070
}

0 commit comments

Comments
 (0)