I must admit that I am having trouble understanding the category tree/node thing in magento. I have extended the Topmenu class to build my own supermenu. It works fine except the category children are always sorted by their id instead of the defined sort order.

So I tried to dig into this but I simply cannot wrap my head around how magento actually pulls and organizes the categories from the database. It all boils down to this function:

public function getMenu()
    if (!$this->_menu) {
        $this->_menu = $this->nodeFactory->create(
                'data' => [],
                'idField' => 'root',
                'tree' => $this->treeFactory->create()
    return $this->_menu;

Somehow magento magically comes up with the categories here. Nowhere I see the typical collection which I could understand.

So my question is: can someone please explain to me how this tree/node thing works and how magento pulls the categories from the database? And in addition to that: how can I manipulate it to my needs? As mentioned I need to get the children sorted by sort order instead of their ids.

1 Answer 1


Have a look into the vendor/magento/module-catalog/Plugin/Block/Topmenu.php.

The method beforeGetHtml() adds the category data to the topmenu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.