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 Answers 2

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;
5
  • thanks, title changes but page is empty
    – omelandr
    Commented Mar 17, 2017 at 10:34
  • @omelandr and if you disable the module it comes back fine ? which version are you using exactly ? Commented Mar 17, 2017 at 10:43
  • yes! magento 2.1.5!
    – omelandr
    Commented Mar 17, 2017 at 10:50
  • I'm out of ideas here, that's annoying Commented Mar 17, 2017 at 10:59
  • @RaphaelatDigitalPianism I suggest that you hide the .page-title-wrapper via CSS (visibility:hidden or display:none) then you create your own page title and add some stylings to it. This can be achieved by creating a static block then calling that static block via phtml file (register.phtml) apply a CSS design to it. You will have more freedom/control on your new page title. Commented Nov 20, 2020 at 15: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 and acknowledge you have read our privacy policy.

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