Skip to content

Commit 85c7cc6

Browse files
authored
Merge pull request #1756 from jmonteros422/1504-Insert-rendition-images-to-the-content-from-media-gallery-instead-of-original-images
#1504: [2.1-develop] Insert rendition images to the content from media gallery instead of original images
2 parents 84a6296 + 2ec9f68 commit 85c7cc6

File tree

5 files changed

+252
-1
lines changed

5 files changed

+252
-1
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGalleryRenditions\Plugin;
9+
10+
use Magento\Cms\Helper\Wysiwyg\Images;
11+
use Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent;
12+
use Magento\Framework\App\Filesystem\DirectoryList;
13+
use Magento\Framework\Filesystem;
14+
use Magento\Framework\Filesystem\Directory\ReadInterface;
15+
use Magento\MediaContentApi\Model\Config;
16+
use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface;
17+
18+
/**
19+
* Intercept and set renditions path on PrepareImage
20+
*/
21+
class SetRenditionPath
22+
{
23+
/**
24+
* @var Filesystem
25+
*/
26+
private $filesystem;
27+
28+
/**
29+
* @var GetRenditionPathInterface
30+
*/
31+
private $getRenditionPath;
32+
33+
/**
34+
* @var Images
35+
*/
36+
private $imagesHelper;
37+
38+
/**
39+
* @var Config
40+
*/
41+
private $config;
42+
43+
/**
44+
* SetPathOnInsert constructor.
45+
* @param GetRenditionPathInterface $getRenditionPath
46+
* @param Filesystem $filesystem
47+
* @param Images $imagesHelper
48+
* @param Config $config
49+
*/
50+
public function __construct(
51+
GetRenditionPathInterface $getRenditionPath,
52+
Filesystem $filesystem,
53+
Images $imagesHelper,
54+
Config $config
55+
) {
56+
$this->getRenditionPath = $getRenditionPath;
57+
$this->filesystem = $filesystem;
58+
$this->imagesHelper = $imagesHelper;
59+
$this->config = $config;
60+
}
61+
62+
/**
63+
* Set's image rendition's path to filename parameter
64+
*
65+
* @param GetInsertImageContent $subject
66+
* @param string $encodedFilename
67+
* @param bool $forceStaticPath
68+
* @param bool $renderAsTag
69+
* @param int|null $storeId
70+
* @return array
71+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
72+
*/
73+
public function beforeExecute(
74+
GetInsertImageContent $subject,
75+
string $encodedFilename,
76+
bool $forceStaticPath,
77+
bool $renderAsTag,
78+
?int $storeId = null
79+
): array {
80+
$imageContent = [
81+
$encodedFilename,
82+
$forceStaticPath,
83+
$renderAsTag,
84+
$storeId
85+
];
86+
87+
if (!$this->config->isEnabled()) {
88+
return $imageContent;
89+
}
90+
91+
$renditionFilePath = $this->getRenditionPath
92+
->execute($this->imagesHelper->idDecode($encodedFilename));
93+
94+
if (!$this->getMediaDirectory()->isFile($renditionFilePath)) {
95+
return $imageContent;
96+
}
97+
98+
$imageContent[0] = $this->imagesHelper->idEncode($renditionFilePath);
99+
100+
return $imageContent;
101+
}
102+
103+
/**
104+
* Retrieve media directory instance with read access
105+
*
106+
* @return ReadInterface
107+
*/
108+
private function getMediaDirectory(): ReadInterface
109+
{
110+
return $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
111+
}
112+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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\MediaGalleryRenditions\Test\Integration\Model;
10+
11+
use Magento\MediaContentApi\Api\ExtractAssetsFromContentInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Test for Extracting assets from rendition paths/urls in content
17+
*/
18+
class ExtractAssetsFromContentWithRenditionTest extends TestCase
19+
{
20+
/**
21+
* @var ExtractAssetsFromContentInterface
22+
*/
23+
private $extractAssetsFromContent;
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
protected function setUp(): void
29+
{
30+
$this->extractAssetsFromContent = Bootstrap::getObjectManager()
31+
->get(ExtractAssetsFromContentInterface::class);
32+
}
33+
34+
/**
35+
* Assert rendition urls/path in the content are associated with an asset
36+
*
37+
* @magentoDataFixture Magento/MediaGallery/_files/media_asset.php
38+
*
39+
* @dataProvider contentProvider
40+
* @param string $content
41+
* @param array $assetIds
42+
*/
43+
public function testExecute(string $content, array $assetIds): void
44+
{
45+
$assets = $this->extractAssetsFromContent->execute($content);
46+
47+
$extractedAssetIds = [];
48+
foreach ($assets as $asset) {
49+
$extractedAssetIds[] = $asset->getId();
50+
}
51+
52+
sort($assetIds);
53+
sort($extractedAssetIds);
54+
55+
$this->assertEquals($assetIds, $extractedAssetIds);
56+
}
57+
58+
/**
59+
* Data provider for testExecute
60+
*
61+
* @return array
62+
*/
63+
public function contentProvider()
64+
{
65+
return [
66+
'Empty Content' => [
67+
'',
68+
[]
69+
],
70+
'No paths in content' => [
71+
'content without paths',
72+
[]
73+
],
74+
'Relevant rendition path in content' => [
75+
'content {{media url=".renditions/testDirectory/path.jpg"}} content',
76+
[
77+
2020
78+
]
79+
],
80+
'Relevant wysiwyg rendition path in content' => [
81+
'content <img src="https://domain.com/media/.renditions/testDirectory/path.jpg"}} content',
82+
[
83+
2020
84+
]
85+
],
86+
'Relevant rendition path content with pub' => [
87+
'/pub/media/.renditions/testDirectory/path.jpg',
88+
[
89+
2020
90+
]
91+
],
92+
'Relevant rendition path content' => [
93+
'/media/.renditions/testDirectory/path.jpg',
94+
[
95+
2020
96+
]
97+
],
98+
'Relevant existing media paths w/o rendition in content' => [
99+
'content {{media url="testDirectory/path.jpg"}} content',
100+
[
101+
2020
102+
]
103+
],
104+
'Relevant existing paths w/o rendition in content with pub' => [
105+
'/pub/media/testDirectory/path.jpg',
106+
[
107+
2020
108+
]
109+
],
110+
'Non-existing rendition paths in content' => [
111+
'content {{media url=".renditions/non-existing-path.png"}} content',
112+
[]
113+
]
114+
];
115+
}
116+
}

MediaGalleryRenditions/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"php": "~7.3.0||~7.4.0",
66
"magento/framework": "*",
77
"magento/module-media-gallery-renditions-api": "*",
8-
"magento/module-media-gallery-synchronization-api": "*"
8+
"magento/module-media-gallery-synchronization-api": "*",
9+
"magento/module-media-content-api": "*",
10+
"magento/module-cms": "*"
911
},
1012
"type": "magento2-module",
1113
"license": [

MediaGalleryRenditions/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@
1111
<type name="Magento\MediaGallerySynchronizationApi\Model\ImportFilesComposite">
1212
<plugin name="generate_rendtions" type="Magento\MediaGalleryRenditions\Plugin\CreateRenditions"/>
1313
</type>
14+
<type name="Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent">
15+
<plugin name="set_rendition_path" type="Magento\MediaGalleryRenditions\Plugin\SetRenditionPath"/>
16+
</type>
1417
</config>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_MediaContentApi:etc/media_content.xsd">
9+
<search>
10+
<patterns>
11+
<pattern name="media_gallery_renditions">/{{media url=(?:"|&amp;quot;)(?:.renditions)?(.*?)(?:"|&amp;quot;)}}/</pattern>
12+
<pattern name="media_gallery">/{{media url="?((?!.*.renditions).*?)"?}}/</pattern>
13+
<pattern name="wysiwyg">/src=".*\/media\/(?:.renditions\/)*(.*?)"/</pattern>
14+
<pattern name="catalog_image">/^\/?media\/(?:.renditions\/)?(.*)/</pattern>
15+
<pattern name="catalog_image_with_pub">/^\/pub\/?media\/(?:.renditions\/)?(.*)/</pattern>
16+
</patterns>
17+
</search>
18+
</config>

0 commit comments

Comments
 (0)