@@ -32,27 +32,40 @@ class Tree
32
32
*/
33
33
protected $ treeFactory ;
34
34
35
+ /**
36
+ * @var \Magento\Catalog\Model\ResourceModel\Category\TreeFactory
37
+ */
38
+ private $ treeResourceFactory ;
39
+
35
40
/**
36
41
* @param \Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTree
37
42
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
38
43
* @param \Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection
39
44
* @param \Magento\Catalog\Api\Data\CategoryTreeInterfaceFactory $treeFactory
45
+ * @param \Magento\Catalog\Model\ResourceModel\Category\TreeFactory|null $treeResourceFactory
40
46
*/
41
47
public function __construct (
42
48
\Magento \Catalog \Model \ResourceModel \Category \Tree $ categoryTree ,
43
49
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
44
50
\Magento \Catalog \Model \ResourceModel \Category \Collection $ categoryCollection ,
45
- \Magento \Catalog \Api \Data \CategoryTreeInterfaceFactory $ treeFactory
51
+ \Magento \Catalog \Api \Data \CategoryTreeInterfaceFactory $ treeFactory ,
52
+ \Magento \Catalog \Model \ResourceModel \Category \TreeFactory $ treeResourceFactory = null
46
53
) {
47
54
$ this ->categoryTree = $ categoryTree ;
48
55
$ this ->storeManager = $ storeManager ;
49
56
$ this ->categoryCollection = $ categoryCollection ;
50
57
$ this ->treeFactory = $ treeFactory ;
58
+ $ this ->treeResourceFactory = $ treeResourceFactory ?? \Magento \Framework \App \ObjectManager::getInstance ()
59
+ ->get (\Magento \Catalog \Model \ResourceModel \Category \TreeFactory::class);
51
60
}
52
61
53
62
/**
63
+ * Get root node by category.
64
+ *
54
65
* @param \Magento\Catalog\Model\Category|null $category
55
66
* @return Node|null
67
+ * @throws \Magento\Framework\Exception\LocalizedException
68
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
56
69
*/
57
70
public function getRootNode ($ category = null )
58
71
{
@@ -71,21 +84,30 @@ public function getRootNode($category = null)
71
84
}
72
85
73
86
/**
87
+ * Get node by category.
88
+ *
74
89
* @param \Magento\Catalog\Model\Category $category
75
90
* @return Node
91
+ * @throws \Magento\Framework\Exception\LocalizedException
92
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
76
93
*/
77
94
protected function getNode (\Magento \Catalog \Model \Category $ category )
78
95
{
79
96
$ nodeId = $ category ->getId ();
80
- $ node = $ this ->categoryTree ->loadNode ($ nodeId );
97
+ $ categoryTree = $ this ->treeResourceFactory ->create ();
98
+ $ node = $ categoryTree ->loadNode ($ nodeId );
81
99
$ node ->loadChildren ();
82
100
$ this ->prepareCollection ();
83
101
$ this ->categoryTree ->addCollectionData ($ this ->categoryCollection );
84
102
return $ node ;
85
103
}
86
104
87
105
/**
106
+ * Prepare category collection.
107
+ *
88
108
* @return void
109
+ * @throws \Magento\Framework\Exception\LocalizedException
110
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
89
111
*/
90
112
protected function prepareCollection ()
91
113
{
@@ -104,6 +126,8 @@ protected function prepareCollection()
104
126
}
105
127
106
128
/**
129
+ * Get tree by node.
130
+ *
107
131
* @param \Magento\Framework\Data\Tree\Node $node
108
132
* @param int $depth
109
133
* @param int $currentLevel
@@ -127,6 +151,8 @@ public function getTree($node, $depth = null, $currentLevel = 0)
127
151
}
128
152
129
153
/**
154
+ * Get node children.
155
+ *
130
156
* @param \Magento\Framework\Data\Tree\Node $node
131
157
* @param int $depth
132
158
* @param int $currentLevel
0 commit comments