Magento – How to Add an Input Renderer for Custom Customer Attribute

adminformadminhtmlcustomermagento-1.8

I added a custom attribute for customer collection named customer_mode.

It have 2 values first, and second in the following maner:

$data = array(
'customer' => array(
    'customer_mode' => array(
        'label'      => 'Customer Mode',
        'sort_order' => 285,
        'required'   => false,
        'type' => 'int',
        'default' => 1 // First
    )
));

In back office admin area I want to show this field not as int but as an custom input type (select box).

I added the Input renderer:

<?php

/**
 * Class Company_Customer_Block_Adminhtml_Customer_Edit_Form_Renderer_CustomerModeType
 *
 * @author Alexandru Olaru <alxolr@gmail.com>
 */
class Company_Customer_Block_Adminhtml_Customer_Edit_Form_Renderer_Mode extends
    Varien_Data_Form_Element_Select
{
    public function __construct(array $attributes)
    {
        parent::__construct($attributes);

        $this->setValues(
            array(
                array(
                    'label' => Mage::helper('company_customer')->__('First'),
                    'value' => 1,
                ),
                array(
                    'label' => Mage::helper('company_customer')->__('Second'),
                    'value' => 2,
                )
            )
        );
    }
}

Now my problem is what value should I put on the input_renderer ?

$data = array(
    'customer' => array(
        'customer_mode' => array(
            'label'      => 'Customer Mode',
            'sort_order' => 285,
            'required'   => false,
            'type' => 'int',
            'default' => 1,
            'input_renderer' => **????**
        )
    )
);

Best Answer

You will need to create customer attribute this way

<?php
$installer = $this;
$installer->startSetup();


$installer->addAttribute("customer", "customer_mode",  array(
    "type"     => "int",
    "backend"  => "",
    "label"    => "Customer Mode",
    "input"    => "select",
    "source"   => "customer/eav_entity_attribute_source_customeroptions",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""

    ));

$attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "customer_mode");


$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
        $attribute->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100)
        ;
        $attribute->save();
$installer->endSetup();

Create your own model class like

Namespace_Module_Model_Eav_Entity_Attribute_Source_Customeroptions

Add below functions in that class

public function getAllOptions()
{
    if (is_null($this->_options)) {
        $this->_options = array(

            array(
                "label" => Mage::helper("eav")->__("First"),
                "value" =>  1
            ),

            array(
                "label" => Mage::helper("eav")->__("Second"),
                "value" =>  2
            ),

        );
    }
    return $this->_options;
}

/**
 * Retrieve option array
 *
 * @return array
 */
public function getOptionArray()
{
    $_options = array();
    foreach ($this->getAllOptions() as $option) {
        $_options[$option["value"]] = $option["label"];
    }
    return $_options;
}

/**
 * Get a text for option value
 *
 * @param string|integer $value
 * @return string
 */
public function getOptionText($value)
{
    $options = $this->getAllOptions();
    foreach ($options as $option) {
        if ($option["value"] == $value) {
            return $option["label"];
        }
    }
    return false;
}

Your setup class should be Mage_Customer_Model_Entity_Setup in your config.xml

<customerattribute_setup>
        <setup>
          <module>Namespace_Module</module>
          <class>Mage_Customer_Model_Entity_Setup</class>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
</customerattribute_setup>
Related Topic