0

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?

1
  • value is static or dynamic ???? Commented May 10, 2019 at 12:26

2 Answers 2

0

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;
    }
}
0

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());
            }
        }
    }

The output for this is as enter image description here

This worked for me. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.