Adding magento attribute with custom input renderer

Featured Image

This is example which will explain you, how to add new attribute with custom input render. You will be able to modify existing functionality, add javascript, some other option or change default input renderer by your wishes.

You probably will ask yourself, what is magento input renderer and I will explain. This is Magento php class which is in charge for “rendering” html form elements. In magento there are different classes for rendering, you can find them in next folder: /lib/Varien/Data/Form/Element. In this folder you will notice next classes for rendering: price, date, image and so on.

Let’s go with our example.

First of all, you should create magento setup file which will add new product attribute with custom frontend input render, example is below:

<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'example_field', array(
    'group'             => 'General',
    'type'              => 'text',
    'backend'           => '',
    'input_renderer'    => 'test/catalog_product_helper_form_example',//definition of renderer
    'label'             => 'Example field',
    'class'             => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'unique'            => false,
    'apply_to'          => 'simple,configurable,bundle,grouped',
    'is_configurable'   => false,
));
$installer->endSetup();

You can see that we are adding attribute with method “addAttribute”, variable “$installer” is instance of “Mage_Catalog_Model_Resource_Setup”, and frontend input rendere is defined in array ‘input_renderer’ => ‘test/catalog_product_helper_form_example’

Next step, you have to create your own input renderer. My example of renderer class (Inchoo_Test_Block_Catalog_Product_Helper_Form_Price) is below, this is very simple class, only for demonstration purpose.

<?php
class Inchoo_Test_Block_Catalog_Product_Helper_Form_Example extends Varien_Data_Form_Element_Text
{
    public function getAfterElementHtml()
    {
        $html = parent::getAfterElementHtml();
        return $html."  <script>
        				$('".$this->getHtmlId()."').disable();
        				</script>";
    }
}

In my example you can see that I added javascript code which disable input element, admin user can’t edit this field.
I hope that is helpful :-) .

5
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Domagoj Potkoc

Magento backend developer

Domagoj Potkoc is part of the Casablanca team together with Ivan Weiler, Hrvoje and Vanja. Domagoj is Magento Certified Developer since March 2012.

Other posts from this author

Discussion 5 Comments

Add Comment
  1. That’s damn handy, I was just looking for a way of doing this.

    Saved hours, cheers!

  2. I am looking for this information from long time nice i got it from yours thanks for sharing this type of magrnto development information keep postings

  3. Hannes

    Hi,

    I’m trying to implement this becuase I need a upload form for flash files. Since I’m just getting started with Magento and all these paths are so confusing, could you tell me wehere those code samples have to go?

    I tried putting the path to the input renderer directly into the database for an existing attribute but there is no field called “input_renderer”. However there are many other field which sound simlilar, my guess is that it’s one of them.

    Also I am never sure where to put the code when yet another tutorial has no description what so ever ;) I placed it at /app/code/local/Inchoo/Test/Block/Catalog/Product/Helper/Form/Example.php (with my corresponding folder names for company and module name of course). Where does Magento get the information that there is a script anyways? I hope you can help me, thanks in advance!

  4. Charles

    I followed this tutorial, not sure if it is down to the version of Magento I am using (1.7.0.2) I found the way to do this was to create new class in:

    lib/Varian/Data/Form/Element/Example.php

    class Varien_Data_Form_Element_Example extends Varien_Data_Form_Element_Text
    {
    }
    

    Then your input_renderer variable would be called Example.

    From trying to follow this tutorial the class would append this to the start Varien_Data_Form_Element_ of the class so it needed to be in the varien folder.

    I also had to manually change the element in the db

    Eav_Attribute.frontend_input value Example

  5. Thiago

    it is possible that my custom field is unique? like the email field in the customer registration? tried to put the argument “unique” as true

    'unique'            =&gt; true,

    , but apparently it did not work.

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top