How to set and get values in multiselect in admin form. Magento 1.9
-
01-03-2021 - |
Pergunta
I want to create one multi-selector on the form to select multiple categories.
I have created a field set for that but how can I display values into that to select multiple categories.
this is my fieldset
$fieldset->addField("visible_in", "multiselect", array(
"label" => Mage::helper("slider")->__("Visible In"),
"name" => "visible-in",
'values' => 'hello',
'required' => true,
));
Currently multiselector showing blank
how to add values into it and where to create the function for it?
Solução 2
You need to set the field for multi-select as
$fieldset->addField("visible_in", "multiselect", array(
"label" => Mage::helper("slider")->__("Visible In"),
"name" => "visible-in",
'values' => $this->getOptionArray(),
'required' => true,
));
In this Most important value, here we are calling the function to select categories
'values' => $this->getOptionArray(),
In the same file write this code
const REPEATER = '_';
const PREFIX_END = '';
protected $_options = array();
public function getOptionArray($parentId = 1, $recursionLevel = 3)
{
$recursionLevel = (int)$recursionLevel;
$parentId = (int)$parentId;
$category = Mage::getModel('catalog/category');
/* @var $category Mage_Catalog_Model_Category */
$storeCategories = $category->getCategories($parentId, $recursionLevel, TRUE, FALSE, TRUE);
foreach ($storeCategories as $node) {
/* @var $node Varien_Data_Tree_Node */
$this->_options[] = array(
'label' => $node->getName(),
'value' => $node->getEntityId()
);
if ($node->hasChildren()) {
$this->_getChildOptions($node->getChildren());
}
}
return $this->_options;
}
/**
* @param Varien_Data_Tree_Node_Collection $nodeCollection
*/
protected function _getChildOptions(Varien_Data_Tree_Node_Collection $nodeCollection)
{
foreach ($nodeCollection as $node) {
/* @var $node Varien_Data_Tree_Node */
$prefix = str_repeat(self::REPEATER, $node->getLevel() * 1) . self::PREFIX_END;
$this->_options[] = array(
'label' => $prefix . $node->getName(),
'value' => $node->getEntityId()
);
if ($node->hasChildren()) {
$this->_getChildOptions($node->getChildren());
}
}
}
This worked for me. Thanks.
Outras dicas
In Field Call Your Block :-
$fieldset->addField("visible_in", "multiselect", array(
"label" => Mage::helper("slider")->__("Visible In"),
"name" => "visible-in",
'values' => Mage::getModel('vendor_module/cms_block_blocks')->getBlockOptions(),
'required' => true,
));
app/code/local/Vendor/Module/Model/Cms/Block/Blocks.php :-
<?php
class Vendor_Module_Model_Cms_Block_Blocks {
public function getBlockOptions() {
foreach($cmsBlocks as $block){
$output[] = array(
'value' => 'pass value',
'label' => 'pass label'
);
}
return $output;
}
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange