Question

In my customization I need a multiselect field in form so I added new field and created a renderer hoping it will work and I've done it this way:

... form.php

   $fieldset->addField('test', 'multiselect', array(
        'name'      => 'test',
        'label'     => Mage::helper('cms')->__('Test'),
        'title'     => Mage::helper('cms')->__('Test'),
        //'onchange'    => "alert('onchange')",
        'class'     => 'sortable',

        'values'    => new Mage_Adminhtml_Block_Cms_Block_Renderer_Test(),
    ));

and so far .... it works, then my renderer

class Mage_Adminhtml_Block_Cms_Block_Renderer_Test
extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {

public function render() {
    // getting banners data filtering by "is_slider = true"
    $myBanners = Mage::getModel( 'cms/block' )->getCollection()
      ->addFieldToSelect('identifier')
      ->addFieldToFilter( 'is_slider', true )
      ;

    foreach($myBanners as $banner){
        $output[] = array(
            'value' => $banner->getData('entity_id'),
            'label' => $banner->getData('identifier'),
            );

    }

    return $output;

}

}

Is my approach correct ? Of course it does not work yet, at least I can't see any positive results except a multiselect grid/table with around 7 selected "empty lines" How should I do it ?

In effect of this I'm getting:

<tr>
    <td class="label"><label for="block_bannerIds">Test</label></td>
<td class="value">
    <select id="block_bannerIds" name="test[]" title="Test" class="sortable select multiselect" size="10" multiple="multiple">
<option value="" selected="selected"></option>
<option value="" selected="selected"></option>
<option value="" selected="selected"></option>
<option value="" selected="selected"></option>
<option value="" selected="selected"></option>
<option value="" selected="selected"></option>
<option value="" selected="selected"></option>
    </select>
    </td>
</tr>

enter image description here

Was it helpful?

Solution 2

The solution came to me in the night... Simplest possible way was just add in form.php:

        $myBanners = $fieldset->addField('mySliders', 'text',
        array(
            'id'        => 'mySliders',
            'name'      => 'banners',
        ));

        $renderer = new Mage_Adminhtml_Block_Cms_Block_Renderer_Table();
        $myBanners->setRenderer($renderer);

and create renderer to generate table (in my case the best option) then display as a second form with all the required functionalities like sortable so I can drag and drop to change sliders order now :). enter image description here

For now it is a vertical list but will change it to horizontal and each bar will be represented by banner/slider thumbnail to make it look better.

OTHER TIPS

Sorry, I think I got you wrong initially - I thought you wanted to add a multiselect to System -> Configuration -> ....

Simplified, for a given form, you could do something like this I think:

app/code/local/Vendor/Module/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Vendor_Module>
            <version>0.0.1</version>
        </Vendor_Module>
    </modules>
    <global>
        <models>
            <vendor_module>
                <class>Vendor_Module_Model</class>
            </vendor_module>
        </models>
        <blocks>
            <adminhtml>
                <rewrite>
                     <cms_block_edit_form>Vendor_Module_Block_Adminhtml_Cms_Block_Edit_Form</cms_block_edit_form>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
</config>

app/code/local/Vendor/Module/Block/Adminhtml/Cms/Block/Edit/Form.php:

<?php

class Vendor_Module_Block_Adminhtml_Cms_Block_Edit_Form extends Mage_Adminhtml_Block_Cms_Block_Edit_Form {

    protected function _prepareForm() {

        $form = parent::_prepareForm()->getForm();
        $fieldset = $form->getElement('base_fieldset');

        $fieldset->addField('block_ids', 'multiselect', 
            array(
                'name' => 'blocks[]',
                'label' => $this->__('Blocks'),
                'title' => $this->__('Blocks'),
                'values' => Mage::getModel('vendor_module/cms_block_blocks')->getBlockOptions(),
            ));

        return $this;

    }

}

app/code/local/Vendor/Module/Model/Cms/Block/Blocks.php:

<?php

class Vendor_Module_Model_Cms_Block_Blocks {

    public function getBlockOptions() {

        $cmsBlocks = Mage::getModel('cms/block')->getCollection()
            ->addFieldToSelect(array('identifier', 'title'));

        foreach($cmsBlocks as $block){
            $output[] = array(
                'value' => $block->getData('block_id'),
                'label' => $block->getData('title')." (".$block->getData('identifier').")",
            );

        }

        return $output;

    }

}

Which will result into something like this:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top