3

I need to display magento 2 category tree on footer and on some other places. I think best practice is creating a module and call it on desired block (place). How to do that ? I already created a module but it is not working it returns an error "Object DOMDocument should be created."

app/code/Muaw/CategoriesList/registration.php

content:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Muaw_CategoriesList',
    __DIR__
);

app/code/Muaw/CategoriesList/Block/CategorisCollection.phtml

<?php
namespace Muaw\CategoriesList\Block;

class CategorisCollection extends \Magento\Framework\View\Element\Template
{

    protected $_categoryHelper;
    protected $categoryFlatConfig;
    protected $topMenu;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Catalog\Helper\Category $categoryHelper
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Helper\Category $categoryHelper,
        \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
        \Magento\Theme\Block\Html\Topmenu $topMenu
    )
    {

        $this->_categoryHelper = $categoryHelper;
        $this->categoryFlatConfig = $categoryFlatState;
        $this->topMenu = $topMenu;
        parent::__construct($context);
    }

    /**
     * Return categories helper
     */
    public function getCategoryHelper()
    {
        return $this->_categoryHelper;
    }

    /**
     * Return categories helper
     * getHtml($outermostClass = '', $childrenWrapClass = '', $limit = 0)
     * example getHtml('level-top', 'submenu', 0)
     */
    public function getHtml()
    {
        return $this->topMenu->getHtml();
    }

    /**
     * Retrieve current store categories
     *
     * @param bool|string $sorted
     * @param bool $asCollection
     * @param bool $toLoad
     * @return \Magento\Framework\Data\Tree\Node\Collection|\Magento\Catalog\Model\Resource\Category\Collection|array
     */
    public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
    {
        return $this->_categoryHelper->getStoreCategories($sorted, $asCollection, $toLoad);
    }

    /**
     * Retrieve child store categories
     *
     */
    public function getChildCategories($category)
    {
        if ($this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource()) {
            $subcategories = (array)$category->getChildrenNodes();
        } else {
            $subcategories = $category->getChildren();
        }
        return $subcategories;
    }
}

app/code/Muaw/CategoriesList/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="Muaw\CategoriesList\Block\CategorisCollection">
        <arguments>
            <argument name="deleteorderAction" xsi:type="array">
                <item name="context" xsi:type="string">\Magento\Framework\View\Element\Template\Context</item>
                <item name="helper" xsi:type="string">\Magento\Catalog\Helper\Category</item>
                <item name="flatstate" xsi:type="string">\Magento\Catalog\Model\Indexer\Category\Flat\State</item>
                <item name="menu" xsi:type="string">\Magento\Theme\Block\Html\Topmenu</item>
            </argument>
        </arguments>
    </type>

</config>

app/code/Muaw/CategoriesList/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Muaw_CategoriesList" setup_version="2.0.1" schema_version="2.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

app/code/Muaw/CategoriesList/view/frontend/template/storecategories.phtml

<?php
$categories = $this->getStoreCategories(true, false, true);
$categoryHelper = $this->getCategoryHelper();
?>
<ul>
    <?php
    foreach ($categories as $category):
        if (!$category->getIsActive()) {
            continue;
        }

        ?>
        <li><a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>"><?php echo $category->getName() ?></a>
        </li>
        <?php
        if ($childrenCategories = $this->getChildCategories($category)):
            ?>
            <ul>
                <?php
                foreach ($childrenCategories as $childrenCategory):
                    if (!$childrenCategory->getIsActive()) {
                        continue;
                    }
                    ?>
                    <li>
                        <a href="<?php echo $categoryHelper->getCategoryUrl($childrenCategory) ?>"><?php echo $childrenCategory->getName() ?></a>
                    </li>
                    <?php
                endforeach;
                ?>
            </ul>
            <?php
        endif;
    endforeach;
    ?>
</ul>

and I called it on my layout file as below

 <container name="categories" after="page_top" htmlTag="div" htmlClass="categories">
            <block class="Muaw\CategoriesList\Block\CategorisCollection" name="categoriecollection" template="Muaw_CategoriesList::storecategories.phtml"/>
        </container>

I don't know what is the wrong with this , please help me by fixing this module or using another method

1
  • Your block should be .php not .phtml. Did you fix it? Commented Jul 10, 2016 at 22:51

1 Answer 1

1

The error you're getting is thrown by the getDom() method from Magento\Framework\View\Element\UiComponent\Config\DomMerger.php

public function getDom()
{
    if (!isset($this->domDocument)) {
        throw new \Magento\Framework\Exception\LocalizedException(
            new \Magento\Framework\Phrase('Object DOMDocument should be created.')
        );
    }

    return $this->domDocument;
}

That is definitely an XML related problem and I'm pretty sure it's caused by your layout file missing some important tags.

I suggest you replace your layout file with the following:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <container name="categories" after="page_top" htmlTag="div" htmlClass="categories">
            <block class="Muaw\CategoriesList\Block\CategorisCollection" name="categoriecollection" template="Muaw_CategoriesList::storecategories.phtml"/>
        </container>
    </body>
</page>

Your Answer

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

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