forked from magento/adobe-stock-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
magento#1504: [2.1-develop] Insert rendition images to the content fr…
…om media gallery instead of original images - Implement interceptors for insert and get paths
- Loading branch information
1 parent
3e16193
commit 195ac85
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters