You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Magento 2 WYSIWYG编辑器文件夹按名称升序排序实现咨询

Magento WYSIWYG编辑器文件夹按名称升序排序解决方案

你之前调整的Magento\Cms\Model\Wysiwyg\Images\Storage类排序规则仅作用于文件集合,文件夹集合的排序需要单独调整,可按以下两种方案实现:

方案1:基于已有的重写类扩展

在你已经通过首选项重写的Storage类中,新增getDirsCollection方法的覆盖逻辑:

/**
 * 重写文件夹集合加载方法,新增名称升序排序
 * @param string $path
 * @return \Magento\Framework\Data\Collection\Filesystem
 */
public function getDirsCollection($path)
{
    $collection = parent::getDirsCollection($path);
    $collection->setOrder('name', \Magento\Framework\Data\Collection::SORT_ORDER_ASC);
    return $collection;
}

修改完成后执行php bin/magento cache:clean清理缓存即可生效。

方案2:使用插件实现(推荐,兼容版本升级)

不需要重写整个类,通过后置插件修改返回的文件夹集合即可:

  1. 在你的自定义模块etc/di.xml中新增插件配置:
<type name="Magento\Cms\Model\Wysiwyg\Images\Storage">
    <plugin name="sort_wysiwyg_dirs_by_name" type="Vendor\Module\Plugin\Wysiwyg\StoragePlugin" sortOrder="10"/>
</type>
  1. 新建插件类Vendor\Module\Plugin\Wysiwyg\StoragePlugin:
<?php
namespace Vendor\Module\Plugin\Wysiwyg;

use Magento\Cms\Model\Wysiwyg\Images\Storage;

class StoragePlugin
{
    /**
     * 后置插件处理文件夹集合排序
     * @param Storage $subject
     * @param \Magento\Framework\Data\Collection\Filesystem $result
     * @return \Magento\Framework\Data\Collection\Filesystem
     */
    public function afterGetDirsCollection(Storage $subject, $result)
    {
        $result->setOrder('name', \Magento\Framework\Data\Collection::SORT_ORDER_ASC);
        return $result;
    }
}
  1. 执行以下命令生效:
php bin/magento setup:di:compile
php bin/magento cache:clean

内容的提问来源于stack exchange,提问作者ramon guardia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 10:00:00