#StackBounty: #magento2 #category #backend #system.xml How to show Categories in System.xml in magento 2

Bounty: 50

I’m getting all the categories in System.xml using the class

VenderModuleModelConfigSourceCategory

<?php
namespace VenderModuleModelConfigSource;

use MagentoFrameworkDataOptionSourceInterface;

class Category implements OptionSourceInterface{

    protected $_categoryFactory;
    protected $_categoryCollectionFactory;

    public function __construct(
        MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryCollectionFactory,
        MagentoCatalogModelCategoryFactory $categoryFactory
    ) {
        $this->_categoryCollectionFactory = $categoryCollectionFactory;
        $this->_categoryFactory = $categoryFactory;
    }

    /**
    * Get category collection
    *
    * @param bool $isActive
    * @param bool|int $level
    * @param bool|string $sortBy
    * @param bool|int $pageSize
    * @return MagentoCatalogModelResourceModelCategoryCollection or array
    */


    public function getAllCategoryCollection($isActive = true, $level = false, $sortBy = false, $pageSize = false)
    {
        $collection = $this->_categoryCollectionFactory->create();
        $collection->addAttributeToSelect('*');

        // select only active categories
        if ($isActive) {
            $collection->addIsActiveFilter();
        }

        // select categories of certain level
        if ($level) {
            $collection->addLevelFilter($level);
        }

        // sort categories by some value
        if ($sortBy) {
            $collection->addOrderField($sortBy);
        }

        // select certain number of categories
        if ($pageSize) {
            $collection->setPageSize($pageSize);
        }

        return $collection;
    }



    public function toOptionArray(){

        $arr = $this->_toCheckArray();
        $ret = [];
        foreach ($arr as $key => $value){
            $ret[] = [
                'value' => $key,
                'label' => $value
            ];
        }    
        return $ret;
    } 

    private function _toCheckArray(){

        $categories = $this->getAllCategoryCollection(true, false, false, false);
        $catagoryList = array();
        foreach ($categories as $category){
           $catagoryList[$category->getEntityId()] = __($this->_getCategoryParentName($category->getPath()) . $category->getName());
        }
        return $catagoryList;
    }



    private function _getCategoryParentName($path = ''){
        $parentName = '';
        $rootCats = array(1,2);  
        $catTree = explode("/", $path);
        array_pop($catTree);     
        if($catTree && (count($catTree) > count($rootCats))){
            foreach ($catTree as $catId){
                if(!in_array($catId, $rootCats)){
                    $category = $this->_categoryFactory->create()->load($catId);
                    $categoryName = $category->getName();
                    $parentName .= $categoryName . ' -> ';
                }
            }
        }
        return $parentName;
    }
}

I get this class form Magento 2 – Category list for custom magento system configuration section
Now its works perfectly for me and its output is like
enter image description here
But when I submit my module to Magento QA they send me the Error
enter image description here

Now how can I solve this problem?unable to identify the problem

Note: I have submitted this Extension before by changing the Name of Class I thought that it will be the problem because of the Class name or Methods name I changed all the methods name and class name but again they send me the same problem.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.