Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Category Tree in Sytem Configuration with multi select……
 
imekhan
Jr. Member
 
Total Posts:  11
Joined:  2011-06-08
 

HI,
i want to show all categories tree in admin system configuration in multiselect field....(Like Sfore view)....

i have create a Model class for it which return toOptionArray() values, and extend from

Mage_Adminhtml_Model_System_Config_Source_Category
..

public function toOptionArray($addEmpty false)
    
{
        $tree 
Mage::getResourceModel('catalog/category_tree'); 

        
$collection Mage::getResourceModel('catalog/category_collection');
        
        
$collection->addAttributeToSelect('name')
            ->
load();
        
        
$options = array();
         
$nonEscapableNbspChar html_entity_decode(' 'ENT_NOQUOTES'UTF-8');
     
        if (
$addEmpty{
            $options[] 
= array(
                
'label' => Mage::helper('adminhtml')->__('-- Please Select a Category --'),
                
'value' => ''
            
);
        
}
    
        
foreach ($collection as $category{
        
if($category->getName()!=''){
        $options[] 
= array(
           
'label' => str_repeat($nonEscapableNbspChar4) .  $category->getName(),
           
'value' => $category->getId()
        );
        
}
    }
    
//echo '<pre>'; print_r($options); exit;
        
return $options;
    
}

But this code only return simply all categories without like tree style in store view option… Any idea how to do this. Thanx..

 
Magento Community Magento Community
Magento Community
Magento Community
 
Tim Hengeveld
Jr. Member
 
Total Posts:  21
Joined:  2010-12-14
 

I’m working on something similar, i found some code in this thread which i modified:
http://www.magentocommerce.com/boards/viewthread/24947/

public function buildCategoriesMultiselectValues(Varien_Data_Tree_Node $node$values$level 0)
    
{
        $level
++;
        
        
$values[$node->getId()]['value'=  $node->getId();
        
$values[$node->getId()]['label'str_repeat("--"$level) . $node->getName();
        
        foreach (
$node->getChildren() as $child)
        
{
            $values 
$this->buildCategoriesMultiselectValues($child$values$level);
        
}
        
        
return $values;
    
}
    
    
public function load_tree()
    
{
        $tree 
Mage::getResourceSingleton('catalog/category_tree')->load(); 
    
        
$store 1
        
$parentId 1
    
        
$tree Mage::getResourceSingleton('catalog/category_tree')->load();
    
        
$root $tree->getNodeById($parentId);
        
        if(
$root && $root->getId() == 1)
        

            $root
->setName(Mage::helper('catalog')->__('Root')); 
        
}
        
        $collection 
Mage::getModel('catalog/category')->getCollection() 
            ->
setStoreId($store
            ->
addAttributeToSelect('name'
            ->
addAttributeToSelect('is_active');
        
        
$tree->addCollectionData($collectiontrue); 

        return 
$this->buildCategoriesMultiselectValues($root, array());    
    
}

And now you can add your field:

$categories_values $this->load_tree();
$fieldset->addField('categories''multiselect', array(
                                
'name' => 'categories',
                                
'label' => Mage::helper('my_module')->__('Categories'),
                                
'title' => Mage::helper('my_module')->__('Categories'),
                                
'required' => false,
                                
'values' => $categories_values,
                                
'style' => 'height:150px',
            ));

Any idea how i can add a store filter to this? And how can i add indentation in the multiselect? If i change

$values[$node->getId()]['label'str_repeat("--"$level) . $node->getName();

To

$values[$node->getId()]['label'str_repeat("  "$level) . $node->getName();

Or

$values[$node->getId()]['label'str_repeat("&nbsp;&nbsp;"$level) . $node->getName();

The spaces are trimmed or &nbsp; becomes in the source &amp;nbsp;.

But i think this code will help you on your way to achieve what you want.

 
Magento Community Magento Community
Magento Community
Magento Community
 
imekhan
Jr. Member
 
Total Posts:  11
Joined:  2011-06-08
 

@Tim Hengeveld, Thank you for your Great post. Its working fine and i am getting category tree in multiselect field as i want....

 
Magento Community Magento Community
Magento Community
Magento Community
 
Tim Hengeveld
Jr. Member
 
Total Posts:  21
Joined:  2010-12-14
 

I also found a way to add the proper indentation:

$nonEscapableNbspChar html_entity_decode('&#160;'ENT_NOQUOTES'UTF-8');
...
$values[$node->getId()]['label'str_repeat($nonEscapableNbspChar, ($level 1) * 4) . $node->getName();
...
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top