@@ -32,27 +32,40 @@ class Tree
3232 */
3333 protected $ treeFactory ;
3434
35+ /**
36+ * @var \Magento\Catalog\Model\ResourceModel\Category\TreeFactory
37+ */
38+ private $ treeResourceFactory ;
39+
3540 /**
3641 * @param \Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTree
3742 * @param \Magento\Store\Model\StoreManagerInterface $storeManager
3843 * @param \Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection
3944 * @param \Magento\Catalog\Api\Data\CategoryTreeInterfaceFactory $treeFactory
45+ * @param \Magento\Catalog\Model\ResourceModel\Category\TreeFactory|null $treeResourceFactory
4046 */
4147 public function __construct (
4248 \Magento \Catalog \Model \ResourceModel \Category \Tree $ categoryTree ,
4349 \Magento \Store \Model \StoreManagerInterface $ storeManager ,
4450 \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
4653 ) {
4754 $ this ->categoryTree = $ categoryTree ;
4855 $ this ->storeManager = $ storeManager ;
4956 $ this ->categoryCollection = $ categoryCollection ;
5057 $ this ->treeFactory = $ treeFactory ;
58+ $ this ->treeResourceFactory = $ treeResourceFactory ?? \Magento \Framework \App \ObjectManager::getInstance ()
59+ ->get (\Magento \Catalog \Model \ResourceModel \Category \TreeFactory::class);
5160 }
5261
5362 /**
63+ * Get root node by category.
64+ *
5465 * @param \Magento\Catalog\Model\Category|null $category
5566 * @return Node|null
67+ * @throws \Magento\Framework\Exception\LocalizedException
68+ * @throws \Magento\Framework\Exception\NoSuchEntityException
5669 */
5770 public function getRootNode ($ category = null )
5871 {
@@ -71,21 +84,30 @@ public function getRootNode($category = null)
7184 }
7285
7386 /**
87+ * Get node by category.
88+ *
7489 * @param \Magento\Catalog\Model\Category $category
7590 * @return Node
91+ * @throws \Magento\Framework\Exception\LocalizedException
92+ * @throws \Magento\Framework\Exception\NoSuchEntityException
7693 */
7794 protected function getNode (\Magento \Catalog \Model \Category $ category )
7895 {
7996 $ nodeId = $ category ->getId ();
80- $ node = $ this ->categoryTree ->loadNode ($ nodeId );
97+ $ categoryTree = $ this ->treeResourceFactory ->create ();
98+ $ node = $ categoryTree ->loadNode ($ nodeId );
8199 $ node ->loadChildren ();
82100 $ this ->prepareCollection ();
83101 $ this ->categoryTree ->addCollectionData ($ this ->categoryCollection );
84102 return $ node ;
85103 }
86104
87105 /**
106+ * Prepare category collection.
107+ *
88108 * @return void
109+ * @throws \Magento\Framework\Exception\LocalizedException
110+ * @throws \Magento\Framework\Exception\NoSuchEntityException
89111 */
90112 protected function prepareCollection ()
91113 {
@@ -104,6 +126,8 @@ protected function prepareCollection()
104126 }
105127
106128 /**
129+ * Get tree by node.
130+ *
107131 * @param \Magento\Framework\Data\Tree\Node $node
108132 * @param int $depth
109133 * @param int $currentLevel
@@ -127,6 +151,8 @@ public function getTree($node, $depth = null, $currentLevel = 0)
127151 }
128152
129153 /**
154+ * Get node children.
155+ *
130156 * @param \Magento\Framework\Data\Tree\Node $node
131157 * @param int $depth
132158 * @param int $currentLevel
0 commit comments