0

On the customer registration page, I need to change the page title depending on the condition. In my custom module I try extend \Magento\Customer\Block\Form\Register method protected function _prepareLayout(). My code is:

namespace Dv\RegistrationNotify\Block\Customer\Block\Form;
class Register extends \Magento\Customer\Block\Form\Register
{
    protected function _prepareLayout()
    {
        if(my condition){
            $this->pageConfig->getTitle()->set(__('Custom title Create New Customer Account'));
        } else {
            $this->pageConfig->getTitle()->set(__('Create New Customer Account'));
        }
        return parent::_prepareLayout();
    }
}

in di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Block\Form\Register" type="Dv\RegistrationNotify\Block\Customer\Block\Form\Register" />
</config>

But in result title doesn't change and page is empty enter image description here

How can I change the title depending on the condition ?

2

The fact that you're calling the following code after your modification will overwrite your change:

return parent::_prepareLayout();

As it will basically do the following:

    if(my condition){
        $this->pageConfig->getTitle()->set(__('Custom title Create New Customer Account'));
    } else {
        $this->pageConfig->getTitle()->set(__('Create New Customer Account'));
    }
    $this->pageConfig->getTitle()->set(__('Create New Customer Account'));
    return $this;

To fix that replace your code with:

parent::_prepareLayout();
if(my condition){
    $this->pageConfig->getTitle()->set(__('Custom title Create New Customer Account'));
} else {
    $this->pageConfig->getTitle()->set(__('Create New Customer Account'));
}
return $this;
  • thanks, title changes but page is empty – omelandr Mar 17 '17 at 10:34
  • @omelandr and if you disable the module it comes back fine ? which version are you using exactly ? – Raphael at Digital Pianism Mar 17 '17 at 10:43
  • yes! magento 2.1.5! – omelandr Mar 17 '17 at 10:50
  • I'm out of ideas here, that's annoying – Raphael at Digital Pianism Mar 17 '17 at 10:59
1

Rafael's answer is correct but it needs one thing added.

The template which is being used is form/register.phtml. This is defined in app/code/Magento/Customer/view/frontend/layout/customer_account_create.xml with this line:

<block class="Magento\Customer\Block\Form\Register" name="customer_form_register" template="form/register.phtml">

Since the template does not define the module it is being used in, such as "Magento_Customer::form/register.phtml", Magento assumes the template is in the same module as its block. When you change which block is being used, you also change the template location.

To fix this, add a customer_account_create.xml file in your module and add a line like this:

<referenceBlock name="customer_form_register" template="Magento_Customer::form/register.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.