0

I try to override the following template which is under www.example.net/newsletter/manage with my own

/vendor/magento/module-customer/view/frontend/templates/form/newsletter.phtml

Block class: Magento\Customer\Block\Newsletter

I already asked the question Override template and block from a module?, and I understood it. But I struggle to find out, how to override this one.

enter image description here

I added my custom template file newsletter.phtml to my module:

app\code\Black\Newsletter2Go\view\frontend\templates\form\newsletter.phtml

What step is required next? I guess I need to add a layout file too, but how do I know how it has to be named?

I need a walkthrough for this one please so that I can understand how this all works together.

3

If @Rising answer not work try this. In newsletter_manage_index.xml try this code.

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="customer_account"/>
    <body>
       <referenceBlock name="customer_newsletter">
            <arguments>
                <argument name="template" xsi:type="string">Vendor_Module::form/newsletter.phtml</argument>
            </arguments>
       </referenceBlock>
    </body>
</page>

OR

<referenceBlock name="customer_newsletter" template="Vendor_Module::form/newsletter.phtml"/>
  • check updated answer and if its not work then replace <referenceBlock> parth with another solution which I have added – Rakesh Varma Oct 30 at 14:07
  • I edited the answer to fix an error. You have to use Vendor_Module::form/newsletter.phtml instead of Vendor_Module::newsletter.phtml. Then it works! :) But how do you find out that you need to reference the block customer_newsletter ? – Black Oct 30 at 14:12
  • the arguments > argument name="template" can be done via di.xml in some block classes. but this is not a way you would use when working with layout xml. Setting the template directly on the referenceblock would work in most cases, but im pretty sure it wont in this specific case. – Ekk4rd Oct 30 at 14:13
  • @Ekk4rd, it worked though. I tried it. – Black Oct 30 at 14:14
  • 1
    ok nice, good to know... about the customer_newsletter block: take a look into the newsletter_manage_index.xml where the block is defined. Using a layout.xml with the same name, you are able to overwrite a xml node by using the same name attribute. – Ekk4rd Oct 30 at 14:17
1

Please try to override the following file mentioned below either using theme or by using your custom module.

/Magento/Customer/view/frontend/templates/form/newsletter.phtml file.

If you are trying to override using theme then please create the folder structure mentioned below.

app/design/frontend/Namespace/<Your Theme Name>/Magento_Customer/templates/form/newsletter.phtml

If you are trying to override using Module.

app/code/<Namespace>/<Module Name>/view/frontend/templates/form/newsletter.phtml

and please add the below line in newsletter_manage_index.xml which you need to create in your layout folder.

<referenceBlock name="newsletter.custom">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Namespace_Modulename::form/newsletter.phtml</argument>
        </action>
    </referenceBlock>

After clear the cache and see the results.

  • Your xml is not complete? Don't I have to use <page ...><body>... ? – Black Oct 30 at 13:58
1

Actually in this specific case, overwriting the template by using a module, is a bit more tough then the most other templates in Magento2. Because this one is not set by layout.xml, but directly in the specific Block-Class.
See module-customer/Block/Newsletter.php for more information.

The most easy way to overwrite this template would be by using a Magento2 Theme instead of an module. If you would create a theme, you could just place the new templte inside. app/deisgn/frontend/Yourtheme/yourtheme/Module_Customer/templates/form/newsletter.phtml.
How do setup a Magento2 Theme: https://devdocs.magento.com/guides/v2.3/frontend-dev-guide/themes/theme-inherit.html

If you still want to change the template by using a module, i guess you won't be able to do this in this module, by just updating a block inside layout.xml, maybe you even need to write a preference for Magento\Customer\Block\Newsletter

*edit:
Utilizing the <action method="setTemplate">... should also do it like @Rising mentioned below.

0

you can place this template file into your theme:

app/design/frontend/VendorName/ThemeName/Magento_Customer/templates/form/newsletter.phtml

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.