Skip to content

Commit

Permalink
magento#1504: [2.1-develop] Insert rendition images to the content fr…
Browse files Browse the repository at this point in the history
…om media gallery instead of original images - Implement interceptors for insert and get paths
  • Loading branch information
jmonteros422 committed Aug 14, 2020
1 parent 3e16193 commit 195ac85
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
32 changes: 32 additions & 0 deletions MediaGalleryRenditions/Plugin/RemoveOnGetByPath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGalleryRenditions\Plugin;

use Magento\MediaGallery\Model\Asset\Command\GetByPath;

/**
* Remove rendition directory from path
*/
class RemoveOnGetByPath
{
private const RENDITIONS_DIRECTORY_NAME = '.renditions';

/**
* Remove renditions directory from path
*
* @param GetByPath $subject
* @param string $path
* @return array
*/
public function beforeExecute(GetByPath $subject, string $path): array
{
$path = ltrim($path, self::RENDITIONS_DIRECTORY_NAME . '/');

return [$path];
}
}
107 changes: 107 additions & 0 deletions MediaGalleryRenditions/Plugin/SetPathOnInsert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGalleryRenditions\Plugin;

use Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\OnInsert;
use Magento\Cms\Helper\Wysiwyg\Images;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\ReadInterface;
use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface;

/**
* Set renditions path on content insert
*/
class SetPathOnInsert
{
/**
* @var Filesystem
*/
private $filesystem;

/**
* @var GetRenditionPathInterface
*/
private $getRenditionPath;

/**
* @var RawFactory
*/
private $resultRawFactory;

/**
* @var Images
*/
private $imagesHelper;

/**
* SetPathOnInsert constructor.
* @param GetRenditionPathInterface $getRenditionPath
* @param RawFactory $resultRawFactory
* @param Filesystem $filesystem
* @param Images $imagesHelper
*/
public function __construct(
GetRenditionPathInterface $getRenditionPath,
RawFactory $resultRawFactory,
Filesystem $filesystem,
Images $imagesHelper
) {
$this->getRenditionPath = $getRenditionPath;
$this->resultRawFactory = $resultRawFactory;
$this->filesystem = $filesystem;
$this->imagesHelper = $imagesHelper;
}

/**
* Set Rendition's path on content insert
*
* @param OnInsert $subject
* @param callable $proceed
* @return ResultInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function aroundExecute(OnInsert $subject, callable $proceed): ResultInterface
{
$request = $subject->getRequest();

$filename = $request->getParam('filename');
$filename = $this->getRenditionPath->execute($this->imagesHelper->idDecode($filename));

if (!$this->getMediaDirectory()->isFile($filename)) {
return $proceed();
}

$storeId = $request->getParam('store');
$asIs = $request->getParam('as_is');
$forceStaticPath = $request->getParam('force_static_path');
$this->imagesHelper->setStoreId($storeId);

if ($forceStaticPath) {
$image = parse_url($this->imagesHelper->getCurrentUrl() . $filename, PHP_URL_PATH);
} else {
$image = $this->imagesHelper->getImageHtmlDeclaration($filename, $asIs);
}

$resultRaw = $this->resultRawFactory->create();

return $resultRaw->setContents($image);
}

/**
* Retrieve media directory instance with read access
*
* @return ReadInterface
*/
private function getMediaDirectory(): ReadInterface
{
return $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
}
}
6 changes: 6 additions & 0 deletions MediaGalleryRenditions/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@
<type name="Magento\MediaGallerySynchronizationApi\Model\ImportFilesComposite">
<plugin name="generate_rendtions" type="Magento\MediaGalleryRenditions\Plugin\CreateRenditions"/>
</type>
<type name="Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\OnInsert">
<plugin name="set_rendition_on_insert" type="Magento\MediaGalleryRenditions\Plugin\SetPathOnInsert"/>
</type>
<type name="Magento\MediaGallery\Model\Asset\Command\GetByPath">
<plugin name="remove_rendition_from_path" type="Magento\MediaGalleryRenditions\Plugin\RemoveOnGetByPath"/>
</type>
</config>

0 comments on commit 195ac85

Please sign in to comment.