浏览文章
文章信息
Magento2 小部件添加图片被破坏选择问题解决|Magento2 widget image upload break problem solver
12423
因为图片选择器的图片url会被转化为{{media url="***.jpg"}},会打断外层的{{widget type="" ...}}导致识别失败。
解决就是利用插件把部件widget图片内容中存在{{media url="替换成‘ ’(空),"}}替换成‘ ’(空),只留下文件相对路径就可以。
1、etc/di.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <!--解决部件双引号导致部件截断被破坏问题--> <type name="Magento\Widget\Model\Widget"> <plugin name="Aiweline_Core_Widget_image_uploader_solve" type="Aiweline\Core\Plugin\Widget\UploaderImageSolver" sortOrder="1"/> </type> </config>2、插件类:Aiweline\Core\Plugin\Widget\UploaderImageSolver
<?php /** * @Author 秋枫雁飞 * @Email aiweline@qq.com/1714255949@qq.com * @Desc 文件由Aiweline(秋枫雁飞)编写,若有升级需要 * 建议不要随意修改文件源码。 **/ namespace Aiweline\Core\Plugin\Widget; class UploaderImageSolver { /** * @var \Magento\Backend\Helper\Data */ protected $backendData; /** * Widget constructor. * * @param \Magento\Backend\Helper\Data $backendData */ public function __construct( \Magento\Backend\Helper\Data $backendData ) { $this->backendData = $backendData; } public function beforeGetWidgetDeclaration( \Magento\Widget\Model\Widget $subject, $type, $params = [], $asIs = true ) { foreach ($params as $name => $value) { if (preg_match('/(___directive\/)([a-zA-Z0-9,_-]+)/', $value, $matches)) { $directive = base64_decode(strtr($matches[2], '-_,', '+/=')); $params[$name] = str_replace(['{{media url="', '"}}'], ['', ''], $directive); } } return [$type, $params, $asIs]; } }