I would like to display *Category Tree Names** in multi select field in magento 2 admin end?
I have tried with below code but displaying only first cateogires could you please any one help on this?
$field = $fieldset->addField(
'wp_test',
'multiselect',
[
'name' => 'wp_test',
'label' => __("Test"),
'values' => $categorylistsource->toOptionArray(),
'value' => $this->_getTest(),
'title' => __('Test.'),
'note' => __('Test')
]
);
Hello\ConfigurableSku\Model\Config\Source
<?php
/**
@copyright Copyright
*/
namespace Hello\ConfigurableSku\Model\Config\Source;
use Magento\Framework\Option\ArrayInterface;
class Categorylist implements ArrayInterface
{
protected $_categoryFactory;
protected $_categoryCollectionFactory;
public function __construct(
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory
)
{
$this->_categoryFactory = $categoryFactory;
$this->_categoryCollectionFactory = $categoryCollectionFactory;
}
public function getCategoryCollection($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->_toArray();
$ret = [];
foreach ($arr as $key => $value)
{
$ret[] = [
'value' => $key,
'label' => $value
];
}
return $ret;
}
private function _toArray()
{
$categories = $this->getCategoryCollection(true, false, false, false);
$catagoryList = array();
foreach ($categories as $category)
{
$catagoryList[$category->getEntityId()] = __($this->_getParentName($category->getPath()) . $category->getName());
}
return $catagoryList;
}
private function _getParentName($path = '')
{
$parentName = '';
$rootCats = array(1,2);
$catTree = explode("/", $path);
// Deleting category itself
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;
}
}
But As per the above logic displaying only single line but i need to display like tree as mentioned below screenshot. how to achieve?