15

I'm trying to override the register.phtml in my module, but it seems there are some config problem that make Magento unable to use my custom template to override the one in core module. I've checked the code but can't find the problem. Anyone can help? Thanks in advance. module.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Namespace_ModuleName" setup_version="0.0.2">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

customer_account_create.xml under Namespace\ModuleName\view\frontend\layout:

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <referenceBlock name="customer_form_register">
        <arguments>

            <argument name="template" xsi:type="string">Namespace_ModuleName::form/register.phtml</argument>

        </arguments>
    </referenceBlock>
</layout>

composer.json:

{
  "name": "namespace/module-modulename",
  "description": "modulename",
  "type": "magento2-module",
  "version": "0.0.2",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "require": {
    "php": "~5.5.0|~5.6.0",
    "magento/framework": "~0.42",
    "magento/magento-composer-installer": "*",
    "magento/module-customer": "*"
  },
  "extra": {
    "map": [
      [
        "*",
        "Namespace/ModuleName"
      ]
    ]
  }
}
18

Just reference necessary block and set template as attribute of referenceBlock node:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_form_register" template="VendorName_ModuleName::form/register.phtml"/>
    </body>
</page>

Alternative syntax:

<referenceBlock name="customer_form_register">
    <action method="setTemplate">
        <argument name="template" xsi:type="string">VendorName_ModuleName::form/register.phtml</argument>
    </action>
</referenceBlock>

Also if you are using Enterprise Edition, make sure to put Magento_CustomerCustomAttributes to sequence in your module.xml because it also overrides this template and may override your changes.

  • 1
    Thanks Alex, but it does not work for me. I'm wondering am I missed some important details e.g. config file or got typo. – Ricky.C Dec 10 '15 at 10:13
  • Just tried to put such layout handle to Captcha module (it has dependency on Customer module so is loaded after it, like your custom module) and it worked for me. Is your module recognized by Magento at all? And of course remember to clear cache before refreshing the page. Try this with Captcha, just to understand if the problem is with your module or layout declaration. – Alex Paliarush Dec 10 '15 at 10:17
  • @Ricky.C see updated answer, especially regarding EE. – Alex Paliarush Dec 10 '15 at 11:04
  • I've tried the alternative syntax, it works on my latest Community Magento 2.0 . Thanks a lot. – Ricky.C Dec 11 '15 at 1:15
1

Use this code in your layout file instead:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_form_register">
            <arguments>
                <argument name="template" xsi:type="string">Namespace_ModuleName::form/register.phtml</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

It replaces "layout" with "page" and adds the body tag

EDIT: 2nd alternative is to remove the original block and add a new one with the desired template

<referenceBlock name="customer_form_register" remove="true"/>
<referenceContainer name="content">
    <block class="Magento\Customer\Block\Form\Register" name="my_customer_form_register" template="Namespace_ModuleName::form/register.phtml">
        <container name="form.additional.info" as="form_additional_info"/>
        <container name="customer.form.register.fields.before" as="form_fields_before" label="Form Fields Before" htmlTag="div" htmlClass="customer-form-before"/>
    </block>
</referenceContainer>
  • Thanks for your answer, I've tried it but still fail – Ricky.C Dec 10 '15 at 9:13
  • Have a try with the the 2nd solution. By the way, is your template file in Namespace/ModuleName/view/frontend/templates/form/register.phtml ? – csmarvz Dec 10 '15 at 9:46
  • This still failed for me, I think because I'm working with an adminhtml template. So I tried an even more alternative method: Copy the template to my own module and then override the Block to invoke the setTemplate method, which then finds the template in the same module. Very dirty solution but the only thing that worked for me! – Jacco Amersfoort Nov 2 '17 at 14:53
0

You missing < body > tag after < page > tag, please try:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="customer_form_register">
        <arguments>

            <argument name="template" xsi:type="string">Namespace_ModuleName::form/register.phtml</argument>

        </arguments>
    </referenceBlock>
</body>
</page>
  • 1
    Thanks for your answer, it's also not work for me. – Ricky.C Dec 10 '15 at 9:41
  • Did you change < layout > tag to < page > tag ? – Magev2 Dec 10 '15 at 9:57
  • Yes, I just copied your code and changed the Namespace_ModuleName – Ricky.C Dec 10 '15 at 10:10
0

Overriding the setTemplate methods works the best for me.

/**
 * Sales Order items name column renderer
 */
class Name 
extends \Magento\Sales\Block\Adminhtml\Items\Column\Name
{

    public function setTemplate($template){
        return parent::setTemplate('Magento_Sales::'.$template);
    }

    /**
     * Add line breaks and truncate value
     *
     * @param string $value
     * @return array
     */
    public function getFormattedOption($value)
    {
        $remainder = '';
        $value = $this->truncateString(
                 str_replace('; ',';
',$value), 1024,'', $remainder ); return ['value' => nl2br($value), 'remainder' => nl2br($remainder)]; } }
0

I know this is old, but if it helps someone,

this github issue magento2/issues/3356 states that referenceBlock and arguments method (mentionned in the question) suffers from a bug as opposed to the deprecated and working method:

<referenceBlock name="customer_form_login">
    <action method="setTemplate">
        <argument name="template" xsi:type="string">
            VendorName_ModuleName::form/login.phtml
        </argument>
    </action>
</referenceBlock>

the documentation (Set a block template) doesn't reflect the actual state of things.

  • 1
    yes, the new method doesn't always work, so gotta use deprecated one – TheKitMurkit May 2 at 8:50

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.