5
5
*/
6
6
namespace Magento \Cms \Block \Adminhtml \Wysiwyg \Images ;
7
7
8
+ use Magento \Framework \Filesystem ;
9
+
8
10
/**
9
11
* Directory tree renderer for Cms Wysiwyg Images
10
12
*
@@ -32,25 +34,33 @@ class Tree extends \Magento\Backend\Block\Template
32
34
*/
33
35
private $ serializer ;
34
36
37
+ /**
38
+ * @var Filesystem
39
+ */
40
+ private $ fileSystem ;
41
+
35
42
/**
36
43
* @param \Magento\Backend\Block\Template\Context $context
37
44
* @param \Magento\Cms\Helper\Wysiwyg\Images $cmsWysiwygImages
38
45
* @param \Magento\Framework\Registry $registry
39
46
* @param array $data
47
+ * @param Filesystem $fileSystem
40
48
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
41
49
* @throws \RuntimeException
42
50
*/
43
51
public function __construct (
44
52
\Magento \Backend \Block \Template \Context $ context ,
45
53
\Magento \Cms \Helper \Wysiwyg \Images $ cmsWysiwygImages ,
46
54
\Magento \Framework \Registry $ registry ,
55
+ Filesystem $ fileSystem ,
47
56
array $ data = [],
48
57
\Magento \Framework \Serialize \Serializer \Json $ serializer = null
49
58
) {
50
59
$ this ->_coreRegistry = $ registry ;
51
60
$ this ->_cmsWysiwygImages = $ cmsWysiwygImages ;
52
61
$ this ->serializer = $ serializer ?: \Magento \Framework \App \ObjectManager::getInstance ()
53
62
->get (\Magento \Framework \Serialize \Serializer \Json::class);
63
+ $ this ->fileSystem = $ fileSystem ;
54
64
parent ::__construct ($ context , $ data );
55
65
}
56
66
@@ -76,7 +86,7 @@ public function getTreeJson()
76
86
'path ' => substr ($ item ->getFilename (), strlen ($ storageRoot )),
77
87
'cls ' => 'folder ' ,
78
88
];
79
- $ nestedDirectories = $ this ->getMediaDirectory ()-> readRecursively ( $ item ->getFilename ());
89
+ $ nestedDirectories = $ this ->getFilteredNestedDirectories ( $ storageRoot , $ item ->getFilename ());
80
90
$ hasNestedDirectories = count ($ nestedDirectories ) > 0 ;
81
91
82
92
// if no nested directories inside dir, add 'leaf' state so that jstree hides dropdown arrow next to dir
@@ -89,6 +99,27 @@ public function getTreeJson()
89
99
return $ this ->serializer ->serialize ($ jsonArray );
90
100
}
91
101
102
+ /**
103
+ * Get nested directories without files
104
+ *
105
+ * @param string $storageRoot
106
+ * @param string $fileName
107
+ * @return array
108
+ */
109
+ private function getFilteredNestedDirectories (string $ storageRoot , string $ fileName ): array
110
+ {
111
+ $ result = [];
112
+ $ pathList = $ this ->getMediaDirectory ()->read ($ fileName );
113
+ foreach ($ pathList as $ directoryPath ) {
114
+ $ directory = $ this ->fileSystem ->getDirectoryReadByPath ($ storageRoot . $ directoryPath );
115
+ if (!$ directory ->isDirectory ()) {
116
+ continue ;
117
+ }
118
+ $ result [] = $ directoryPath ;
119
+ }
120
+ return $ result ;
121
+ }
122
+
92
123
/**
93
124
* Json source URL
94
125
*
0 commit comments