Magento – How to add custom frontend input for “catalog input type for store owner”

catalogproduct-attribute

I want to add custom frontend_input for “Catalog Input Type for Store Owner” at the creation of an product attribute.

Like at present magento is providing Text Field,Text Area,Date,Yes/No,Multiple Select,Dropdown,Price,Media Image.
In this list I want to add new attribute like : multiselect_calender.

So it is possible to add new option ? How ?

Just check screen shot for better idea about my query

enter image description here

Best Answer

Reference class Mage_Catalog_Model_Product_Attribute_Source_Inputtype extends Mage_Eav_Model_Adminhtml_System_Config_Source_Inputtype, to add multiselect_calender as a dropdown option, you can add an observer to the event adminhtml_product_attribute_types. Then, you need to listen to the event catalog_entity_attribute_save_before to insert your backend_model.

As an example from Mage_Weee_Model_Observer:

/**
 * Add new attribute type to manage attributes interface
 *
 * @param   Varien_Event_Observer $observer
 * @return  Mage_Weee_Model_Observer
 */
public function addWeeeTaxAttributeType(Varien_Event_Observer $observer)
{
    // adminhtml_product_attribute_types

    $response = $observer->getEvent()->getResponse();
    $types = $response->getTypes();
    $types[] = array(
        'value' => 'weee',
        'label' => Mage::helper('weee')->__('Fixed Product Tax'),
        'hide_fields' => array(
            'is_unique',
            'is_required',
            'frontend_class',
            'is_configurable',

            '_scope',
            '_default_value',
            '_front_fieldset',
        ),
        'disabled_types' => array(
            Mage_Catalog_Model_Product_Type::TYPE_GROUPED,
        )
    );

    $response->setTypes($types);

    return $this;
}

/**
 * Automaticaly assign backend model to weee attributes
 *
 * @param   Varien_Event_Observer $observer
 * @return  Mage_Weee_Model_Observer
 */
public function assignBackendModelToAttribute(Varien_Event_Observer $observer)
{
    $backendModel = Mage_Weee_Model_Attribute_Backend_Weee_Tax::getBackendModelName();
    /** @var $object Mage_Eav_Model_Entity_Attribute_Abstract */
    $object = $observer->getEvent()->getAttribute();
    if ($object->getFrontendInput() == 'weee') {
        $object->setBackendModel($backendModel);
        if (!$object->getApplyTo()) {
            $applyTo = array();
            foreach (Mage_Catalog_Model_Product_Type::getOptions() as $option) {
                if ($option['value'] == Mage_Catalog_Model_Product_Type::TYPE_GROUPED) {
                    continue;
                }
                $applyTo[] = $option['value'];
            }
            $object->setApplyTo($applyTo);
        }
    }

    return $this;
}

You also need to add this in your config.xml:

<global>   
    <events>
        <catalog_prepare_price_select>
            <observers>
                <weee>
                    <type>model</type>
                    <class>weee/observer</class>
                    <method>prepareCatalogIndexSelect</method>
                </weee>
            </observers>
        </catalog_prepare_price_select>
        <catalog_entity_attribute_save_before>
            <observers>
                <weee>
                    <type>model</type>
                    <class>weee/observer</class>
                    <method>assignBackendModelToAttribute</method>
                </weee>
            </observers>
        </catalog_entity_attribute_save_before>
    </event>
</global>   
<default>
    <general>
        <validator_data>
            <input_types>
                <weee>weee</weee>
            </input_types>
        </validator_data>
    </general>
</default>

For the validator, see Mage_Adminhtml_Catalog_Product_AttributeController::saveAction() and Mage_Eav_Model_Adminhtml_System_Config_Source_Inputtype_Validator

Related Topic