Magento 2 set Page title while extending customer blocks

Here is the Page Title solution for M2 while you are using ‘Magento\Customer\Block\Form\Register’ or ‘Magento\Customer\Block\Form\Login’ class to extend in your custom block.
I have found that when we are using this to extend our custom block they make changes in page titles in some of the pages like order success page.

After struggeling for sometime I have found a patch we can use. Hopefully useful for someone

    /**
     * @return $this
     * @author Karan Popat <krn.ppt@gmail.com>
     */
    protected function _prepareLayout()
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $request = $objectManager->get('\Magento\Framework\App\Request\Http');
        if($request->getFullActionName() == 'customer_account_create') {
            $this->pageConfig->getTitle()->set(__('Create New Customer Account'));  
        }
    }

Here you can replace ‘customer_account_create’ with relevant action name for the block you have extended and set title accordingly. i.e.

Magento\Customer\Block\Form\Register => customer_account_create
Magento\Customer\Block\Form\Login => customer_account_login

2 thoughts on “Magento 2 set Page title while extending customer blocks

Post your Queries or Suggesions

This site uses Akismet to reduce spam. Learn how your comment data is processed.