1

I want to insert a custom block into the $block->getChildHtml('customer.form.register.extra') placeholder in the customer registration form - called in the Magento_Customer::form/register.phtml template file.

I have a custom block in my module that I'd like to insert into this placeholder to avoid overriding the .phtml file.

I've copied the customer_account_create.xml layout file into my module and replaced the code with the following:

<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="customer.form.register.extra">
            <block class="Magento\Framework\View\Element\Template"
                   name="custom.block.name"
                   template="Vendor_ModuleName::my_custom_template.phtml"/>
        </referenceContainer>
    </body>
</page>

I've also tried referencing by referenceBlock instead of referenceContainer but still my custom html is not appearing in the form.

Does anyone have any ideas how I can get this to work? Any help would be appreciated.

Thanks

2 Answers 2

1

Actually, the customer.form.register.extra is called in the phtml file, but never created by Magento in the customer_account_create.xml layout.

You have 2 solutions here.


The first one, which I recommend, would be to add your custom block to the customer.form.register.fields.before container instead (which is called by its alias form_fields_before in the template file).

In the template file, this container is called just before the customer.form.register.extra element, with no code in between. So there should be no difference for you to switch to this container.

You would end up with a layout file like this :

<?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>
        <referenceContainer name="customer.form.register.fields.before">
            <block class="Magento\Framework\View\Element\Template"
                   name="custom.block.name"
                   template="Vendor_ModuleName::my_custom_template.phtml"/>
        </referenceContainer>
    </body>
</page>

Note that it is not necessary to repeat the layout="1column" instruction on the page node of your XML file.


Another solution, if you really need to use the customer.form.register.extra element first, would be to create it first in your layout file as a container, and add your custom block as its child.

You would end up with a code like this :

<?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">
            <container name="customer.form.register.extra">
                <block class="Magento\Framework\View\Element\Template"
                       name="custom.block.name"
                       template="Vendor_ModuleName::my_custom_template.phtml"/>
            </container>
        </referenceBlock>
    </body>
</page>

The reason why I would not recommend this method, is because if in the future Magento or another module decides to create the customer.form.register.extra element, you would end up with this element declaration duplicated.

2
0

You need to use Magento Plugin Functionality.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Block\Form\Register">
        <plugin name="Your_Module::Custom_Block"
                type="Your\Module\Plugin\Custom"/>
    </type>
</config>

Add your layout here with the below condition

<?php
declare(strict_types=1);

namespace Your\Module\Plugin;

use Magento\Framework\View\Element\AbstractBlock;

class Custom
{
    public function afterGetChildHtml(\Magento\Customer\Block\Form\Register $subject, , string $result, $alias = '', $useCache = true): string
    {
        if ($alias == 'customer.form.register.extra') { 
            $block = $subject->getLayout()
        ->createBlock('\Your\Module\Block\Custom')
        ->setTemplate('Your_Module::custom.phtml')
        ->toHtml();
            $result = $result . $block;
        }

        return $result;
    }
}
2
  • When possible, I would avoid using plugins to add custom blocks when it can be done in the layout files. It adds more complexity when you want to look at the code or extend it, and is less readable than the usual XML instruction. If using a plugin was necessary, I would recommend using the after method instead of around (which should be avoided when not required).
    – Cladiuss
    May 23 at 16:56
  • I've replaced around to after, now you can use this code. May 24 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.