PHP Magento Tips

Magento Admin Edit form with a Multiselect Field with Product Categories Raw

Rate this posting:
{[['']]}

In Magento Admin Edit form, you want to display Multiselect field with product categories. It is quite difficult sometimes. But with below code you can achieve it very easily.


Step 1: Add Multiselect field with addField function.

in app/code/community/Vijay/CustomBackground/Block/Adminhtml/Background/Edit/Tab/Form.php add below code.

<?php

class Vijay_CustomBackground_Block_Adminhtml_Background_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
 protected function _prepareForm()
    {
 
 ...................
 
 $fieldset->addField(
            'background_category_id',
            'multiselect',
            array(
                'label' => Mage::helper('vijay_custombackground')->__('Background Target Category'),
                'name'  => 'background_category_id',
                'values'   => Mage::getModel('vijay_custombackground/background_attribute_source_productcategories')->getOptionArray(),
           )
        );
  
 ....................
 }
}


Step 2: Add Multiselect options values from your custom Model.

in app/code/community/Vijay/CustomBackground/Model/Background/Attribute/Source/Productcategories.php add below code.

<?php
class Vijay_CustomBackground_Model_Background_Attribute_Source_Productcategories extends Varien_Object
{
    const REPEATER = '_';
    const PREFIX_END = '';
    protected $_options = array();
    /**
     * @param int $parentId
     * @param int $recursionLevel
     *
     * @return 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);
  $storeCategories = $category->getCategories($parentId, $recursionLevel, TRUE, FALSE, TRUE);
  
        foreach ($storeCategories as $node) {
          
            /* @var $node Varien_Data_Tree_Node */
            if($node->getEntityId() == 2) {
       $this->_options = '';
     } else {
      $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());
            }
        }
    }
}

Note: here i added $recursionLevel = 3. You can add your recursionLevel as your wish to go more deeper level.

Tag : Magento
0 Comments On "Magento Admin Edit form with a Multiselect Field with Product Categories Raw"

Back To Top