I have created custom CRUD on new database table. I just wanted to save comma separated categories in a field.
I have this code for multiselect categories:
$fieldset->addField(
'categories',
'multiselect',
[
'label' => __('Categories'),
'title' => __('Categories'),
'name' => 'categories',
'values' => \Vendor\Module\Block\Adminhtml\Products\Grid::getCategories(),
'note' => __('Could be multiple'),
'disabled' => $isElementDisabled
]
);
I have created getCategories
function in Grid.php.
Can anyone suggested how to populate categories in tree form and return categories array
Best Answer
After so many tries, I have figured out the solution for my own question.
I used this code to make multiselect (as mentioned in my question):
In Grid.php file I have created two functions one is to create simple array for multi select values:
And the 2nd one is a recursive function to generate category tree
My final array
$data_array
contains the IDs in array key and Category title in values.