2

Taking example from this link

http://inchoo.net/magento/adding-magento-attribute-with-custom-input-renderer/

I create my renderer and associate it to a attribute in setup.php file:

                'price_increase' => array(
                    'group' => 'General setting',
                    'type' => 'decimal',
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Decimal element',
                    'input' => 'text',
                    'class' => 'validate-number',
                    'source' => '',
                    'global' => 0,
                    'visible' => true,
                    'required' => true,
                    'user_defined' => true,
                    'default' => 1,
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'unique' => true,
                    'input_renderer' =>'my_module/adminhtml_setting_render_percentage'
                ),

<?php

Percentage.php:

class My_Module_Block_Adminhtml_Setting_Render_Percentage extends Varien_Data_Form_Element_Text{

    public function getElementHtml()
    {
        $html = parent::getElementHtml();
        return $html."%";
    }
}

I use a Widget_Form_Container with Widget_Form to render my attribute, this is the code:

class My_Module_Block_Adminhtml_Setting_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm() {

        $setting=Mage::getModel('my_module/setting')->load(1);
        $form = new Varien_Data_Form(array(
            'id' => 'my_module_form',
            'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
            'method' => 'post',
            'enctype' => 'multipart/form-data'
        ));
        $form->setUseContainer(true);


        $form->setDataObject($setting);
        $fieldset = $form->addFieldset('registry_form',
            array(
                'legend'=>Mage::helper('my_module')->__('Generel option'),
                'class'=>'fieldset-wide',
            )
        );

        $attributes = Mage::getResourceModel('eav/entity_attribute_collection')
            ->setEntityTypeFilter($setting->getEntityTypeId());
        $attributes->addFieldToFilter('attribute_code', array('nin'=>array('meta_title', 'meta_description', 'meta_keywords')));

        foreach ($attributes as $attribute) {
            $attribute->setEntity(Mage::getResourceModel('my_module/setting'));
        }
        $this->_setFieldset($attributes, $fieldset, array());

        $formValues = $setting->getData();
        $form->addValues($formValues);

        $entityTypeId=Mage::getResourceModel('my_module/setting')->getEntityType()->getId();


        $form->setFieldNameSuffix('setting');
        $this->setForm($form);
    }

The problem is My_Module_Block_Adminhtml_Setting_Render_Percentage does not come into action.

  • Did you declare the blocks in the configuration of your module (config.xml) ? – hellimac Aug 13 '15 at 9:34
  • yes. It all works properly but the render doesn't work. – tonyB Aug 13 '15 at 9:45

Your Answer

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

Browse other questions tagged or ask your own question.