cancel
Showing results for 
Search instead for 
Did you mean: 

Overwrite the Block file and .phtml files

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Overwrite the Block file and .phtml files



I would like to overwrite the the Block files and .phtml files of the magento modules.Here is example
-Magento Newsletter Module

I want to extended the Block file '\Magento\Newsletter\Block\Subscribe' to the custom Module as mentioned in the below code
<?php
namespace Sample\Test\Block;

class Customsubscribe extends \Magento\Newsletter\Block\Subscribe
{

/**
 * Retrieve form action url and set "secure" param to avoid confirm
 * message when we submit form from secure page to unsecure
 *
 * @return string
 */
public function getFormActionUrl()
{
    return $this->getUrl('newsletter/subscriber/new1231', ['_secure' => true]);
}

}
?>

Add tried to override the subscriber.phtml for Magento newsletter Module to My sample Module 'Test'

In the custom modules folder i add the below code

app\code\Sample\Test\view\frontend\layout\default.xml

    <!--remove name ='form.subscribe'/-->
    <referenceContainer name="footer">
        <block class="Training\Test\Block\Customsubscribe" name="form.subscribe" as="subscribe" before="-" template="subscribe.phtml"/>
    </referenceContainer>
</body>

After Doing that i run the CLI : php bin/magento setup:upgrade, but still the file is loading from the Magento_Newsletter Module .

Can any one please help on the this issues.

3 REPLIES 3

Re: Overwrite the Block file and .phtml files

One of your errors is you have specified a wrong namespace: you use Sample\Test\Block in the class declaration but Training\Test\Block is the layout default.xml file.

Re: Overwrite the Block file and .phtml files

Hi Dmitry_Fedyuk,

I have done correction and tried ,but is not working:

 

<referenceContainer name="footer">
        <block class="Sample\Test\Block\Customsubscribe" name="form.subscribe" as="subscribe" before="-" template="subscribe.phtml"/>
    </referenceContainer>

Re: Overwrite the Block file and .phtml files

I think you need to remove block 'form.subscribe' and then redeclare it.

<referenceBlock  name="form.subscribe" remove="true"></referenceBlock>
    <referenceContainer name="footer">
        <block class="Training\Test\Block\Customsubscribe" name="form.subscribe" as="subscribe" before="-" template="subscribe.phtml"/>
    </referenceContainer>
</body>