0

Just installed Magento CE 1.9.3.1 into my local system.

Then I tried to override the default text input field of Varien class just to add "placeholder" attribute.

etc/config.xml of my module:

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_PlaceholderAttribute>
            <version>1.0.0</version>
        </Custom_PlaceholderAttribute>
    </modules>
    <global>
        <blocks>
            <placeholderattr>
                <rewrite>
                    <text>Varien_Data_Form_Element_Text</text>
                </rewrite>
            </placeholderattr>
        </blocks>
    </global>
</config>

Block class file of module:

<?php
class Custom_PlaceholderAttribute_Block_Adminhtml_Text extends Varien_Data_Form_Element_Text {
    public function getHtmlAttributes() {
        $attributes = parent::getHtmlAttributes();
        $attributes[] = 'placeholder';
        return $attributes;
    }
}

Now when I try to change fieldset type, in any block-form file, by adding below line, it is giving me Fatal Error.

$fieldset->addType('text', Mage::getConfig()->getBlockClassName('placeholderattr/adminhtml_text'));

The Fatal error I am getting is:

Fatal error: Class 'Mage_Placeholderattr_Text_Block_Adminhtml_Text' not found in C:\UwAmp\www\testmage1931\lib\Varien\Data\Form\Abstract.php on line 146

But if I try this, rewrite works fine:

$fieldset->addType('text','Custom_PlaceholderAttribute_Block_Adminhtml_Text');

Why block type doesn't work in getBlockClassName(), can somebody clarify/explain the reason for this ?

1

Add following code inside global tag

<blocks>
    <placeholderattr>
        <class>Custom_PlaceholderAttribute_Block_Block</class>
    </placeholderattr>
</blocks>

So your code looks like:

<config>
    <modules>
        <Custom_PlaceholderAttribute>
            <version>1.0.0</version>
        </Custom_PlaceholderAttribute>
    </modules>
    <global>
        <blocks>
            <placeholderattr>
                <class>Custom_PlaceholderAttribute_Block_Block</class>
            </placeholderattr>
        </blocks>
    </global>
</config>

Clear cache.

0

It is not a regular block you are trying to rewrite. You try to rewrite a lib.

Therefore you have to copy this class into the local codepool to do a rewrite. Have a look at this answer from Alan Storm: https://stackoverflow.com/a/6077165

In general, this guide will help you with all the rewrite types you might need: http://inchoo.net/magento/overriding-magento-blocks-models-helpers-and-controllers/

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.