Skip to main content
Typo, delault should be default
Source Link
Manoj Deswal
  • 5.8k
  • 25
  • 28
  • 50

Create a file Categorylist.php in Companyname\Modulename\Model\Config\Source

Create a file Categorylist.php in Companyname\Modulename\Model\Config\Source

Here is a screenshot:   

enter image description here

Create a file Categorylist.php in Companyname\Modulename\Model\Config\Source

Here is a screenshot:  enter image description here

Create a file Categorylist.php in Companyname\Modulename\Model\Config\Source

Here is a screenshot: 

enter image description here

<group id="bannerblock_setting" translate="label" type="text" delault="1"default="1" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Setting</label>
    <field id="bannerlist" translate="label" type="multiselect" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>Select Category</label>
        <!-- <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>-->
        <source_model>Ipragmatech\Bannerblock\Model\Config\Source\Categorylist</source_model>
    </field>
</group>
 
<group id="bannerblock_setting" translate="label" type="text" delault="1" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Setting</label>
    <field id="bannerlist" translate="label" type="multiselect" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>Select Category</label>
        <!-- <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>-->
        <source_model>Ipragmatech\Bannerblock\Model\Config\Source\Categorylist</source_model>
    </field>
</group>
<group id="bannerblock_setting" translate="label" type="text" default="1" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Setting</label>
    <field id="bannerlist" translate="label" type="multiselect" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>Select Category</label>
        <!-- <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>-->
        <source_model>Ipragmatech\Bannerblock\Model\Config\Source\Categorylist</source_model>
    </field>
</group>
 
You don't need this row because you've used '\Magento\Catalog\Helper\Category $catalogCategory'
Source Link
namespace Companyname\Modulename\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;
use Magento\Catalog\Helper\Category;

class Categorylist implements ArrayInterface
{
    protected $_categoryHelper;

    public function __construct(\Magento\Catalog\Helper\Category $catalogCategory)
    {
        $this->_categoryHelper = $catalogCategory;
    }

    /*
     * Return categories helper
     */

    public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
    {
        return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
    }

    /*  
     * Option getter
     * @return array
     */
    public function toOptionArray()
    {
    
    
        $arr = $this->toArray();
        $ret = [];
    
        foreach ($arr as $key => $value)
        {
        
            $ret[] = [
                'value' => $key,
                'label' => $value
            ];
        }
    
        return $ret;
    }

    /*
     * Get options in "key-value" format
     * @return array
     */
    public function toArray()
    {
        
        $categories = $this->getStoreCategories(true,false,true);
    
        $catagoryList = array();
        foreach ($categories as $category){
        
            $catagoryList[$category->getEntityId()] = __($category->getName());
        }
            
        return $catagoryList;
    }

}
namespace Companyname\Modulename\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;
use Magento\Catalog\Helper\Category;

class Categorylist implements ArrayInterface
{
    protected $_categoryHelper;

    public function __construct(\Magento\Catalog\Helper\Category $catalogCategory)
    {
        $this->_categoryHelper = $catalogCategory;
    }

    /*
     * Return categories helper
     */

    public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
    {
        return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
    }

    /*  
     * Option getter
     * @return array
     */
    public function toOptionArray()
    {
    
    
        $arr = $this->toArray();
        $ret = [];
    
        foreach ($arr as $key => $value)
        {
        
            $ret[] = [
                'value' => $key,
                'label' => $value
            ];
        }
    
        return $ret;
    }

    /*
     * Get options in "key-value" format
     * @return array
     */
    public function toArray()
    {
        
        $categories = $this->getStoreCategories(true,false,true);
    
        $catagoryList = array();
        foreach ($categories as $category){
        
            $catagoryList[$category->getEntityId()] = __($category->getName());
        }
            
        return $catagoryList;
    }

}
namespace Companyname\Modulename\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;

class Categorylist implements ArrayInterface
{
    protected $_categoryHelper;

    public function __construct(\Magento\Catalog\Helper\Category $catalogCategory)
    {
        $this->_categoryHelper = $catalogCategory;
    }

    /*
     * Return categories helper
     */

    public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
    {
        return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
    }

    /*  
     * Option getter
     * @return array
     */
    public function toOptionArray()
    {
    
    
        $arr = $this->toArray();
        $ret = [];
    
        foreach ($arr as $key => $value)
        {
        
            $ret[] = [
                'value' => $key,
                'label' => $value
            ];
        }
    
        return $ret;
    }

    /*
     * Get options in "key-value" format
     * @return array
     */
    public function toArray()
    {
        
        $categories = $this->getStoreCategories(true,false,true);
    
        $catagoryList = array();
        foreach ($categories as $category){
        
            $catagoryList[$category->getEntityId()] = __($category->getName());
        }
            
        return $catagoryList;
    }

}
added 167 characters in body
Source Link
7ochem
  • 7.6k
  • 14
  • 52
  • 80
Loading
Source Link
Manish
  • 3.1k
  • 7
  • 32
  • 47
Loading