7
$fieldset->addField('rating', 'label', array(
        'name'      => 'rating',
        'label'     => Mage::helper('sellerreview')->__('Rating'),
        'title'     => Mage::helper('sellerreview')->__('Rating'),
        'renderer'  => 'sellerreview/adminhtml_review_renderer_image',

    ));

this is my image.php file

 public function render(Varien_Object $row) {

    $fullRating = $row->getRating();
    $html = '';

        $html = $html.'<input id="input-21d" name="rating" value='. $fullRating .' type="number" class="rating" min=0 max=5 step=0.5 data-size="sm" data-disabled="true">';


    return $html;
}

Is this right method to use renderer in addfield form? How to use renderer in addfield form

  • are you taking about admin form fields – Amit Bera Apr 10 '15 at 6:02
  • yes admin form field – ND17 Apr 10 '15 at 6:03
13

Renderer file:

class [NameSpace]_[ModuleName]_Block_Adminhtml_Review_Renderer_Images extends  Varien_Data_Form_Element_Abstract
{
    protected $_element;

    public function getElementHtml()
    {   

        $html = '';

            $html = $html.'<input id="input-21d" name="rating" value='2' type="number" class="rating" min=0 max=5 step=0.5 data-size="sm" data-disabled="true">';


        return $html;
    }
}

in your form.php

$fieldset->addType('customtype', '[NameSpace]_[ModuleName]_Block_Adminhtml_Review_Renderer_Images');     
     $fieldset->addField('rating', 'customtype', array(
        'name'      => 'rating',
        'label'     => Mage::helper('[modulename]')->__('Rating'),
    ));

using this you can use custom html in admin form field

  • It should called custom html element. not called renderer in basic magento – liyakat Apr 10 '15 at 8:44
  • @ND17 In this example, renderer file name should be Images.php, right ? – SagarPPanchal Apr 8 '18 at 9:06
  • @SagarPPanchal Yes – ND17 Apr 9 '18 at 12:19
  • To get the value of the element, use $this->getValue(). – Joe Jan 24 at 23:07
2

you are almost correct here i suggest you about my working example to display image of field in grid

Grid.php

$this->addColumn('after_image', array(
          'header'    => Mage::helper('testimonials')->__('After Image'),
          'align'     =>'left',
          'index'     => 'after_image',
          'renderer'  => 'testimonials/adminhtml_testimonials_edit_renderer_testimonialafimage',
      ));

where testimonials is your block tag in config.xml

and your render file should be like as define in your renderer index

<?php
class Yournamespace_Testimonials_Block_Adminhtml_Testimonials_Edit_Renderer_Testimonialafimage extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
  {
     public function render(Varien_Object $row)
    {       

        if($row->getId()) {             
         $imageName = $row->getAfterImage();                
        $imagePath = Mage::getBaseUrl("media").$imageName; 
        $dirImg = Mage::getBaseDir().str_replace("/",DS,strstr($imagePath,'/media'));        

        if (file_exists($dirImg) && !empty($imageName)) {           
            return  "<img width='50px' height='50px' src='".Mage::getBaseUrl("media").$imageName."' />"; 
        }else{
             return  "<img width='50px' height='50px' src='".Mage::getBaseUrl("media").'testimonial/not-available.jpg'."' />"; 
        }          
        }
    }  
}     
 ?>

EDIT

Use renderer in Edit form you can see rating edit form of admin

class Mage_Adminhtml_Block_Rating_Edit_Tab_Form

 $field = $fieldset->addField('stores', 'multiselect', array(
            'label' => Mage::helper('rating')->__('Visible In'),
            'name' => 'stores[]',
            'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm()
        ));
        $renderer = $this->getLayout()->createBlock('adminhtml/store_switcher_form_renderer_fieldset_element');
        $field->setRenderer($renderer);

hope this will be enough to get idea.

  • 1
    I want to display in admin edit form not in grid – ND17 Apr 10 '15 at 5:35
  • @ND17, see my updated answer – liyakat Apr 10 '15 at 5:48
  • not working.... – ND17 Apr 10 '15 at 5:53
  • it should work, check your block path ' adminhtml' is for default magento's change with your block and renderer path – liyakat Apr 10 '15 at 6:06
  • is this correct? $renderer = $this->getLayout()->createBlock('sellerreview/adminhtml_review_renderer_image'); $field->setRenderer($renderer); – ND17 Apr 10 '15 at 6:10

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.