1

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

  • Did you try using addOptionArray() rather than render()? – Christoph Farnleitner Mar 2 '18 at 17:31
  • Hello Chris, I tried but it did not worked. – Rob D. A. Mar 5 '18 at 10:48
  • Chris, do you suggest that if I use addOptionArray it may work ? Have you got any examples ? – Rob D. A. Mar 6 '18 at 12:43
1

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

  • Hello Christoph, thank you, will try this way. Is it possible to add there (form) element like a table ? For example table containing all blocks with certain attributes? In mu customization as the original theme does not allow to display for example banners using date so you must manually change values/string with block names to be displayed. I already changed the code, added required attributes etc and it works pretty good but there is one more thing to do - sort/order of the banners. That's why I need this multiselect field which at the end will be used as a sortable. – Rob D. A. Mar 7 '18 at 9:50
  • not sure it is possible but .... if you not try ... then you will never know :P right ? – Rob D. A. Mar 7 '18 at 9:50
  • I think I did a quiet big leap forward .... – Rob D. A. Mar 7 '18 at 12:43
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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