Magento – Magento 2 – Incorrect title showing for some pages

advanced-searchcontact-usmagento-2.1magento2page-title

Some of the pages show incorrect title and heading : it is showing "Customer Login" as page heading and title for Contact page and Advanced Search page.

The point is, I included customer login form on every page including these above, but anywhere else I can edit the page title successfully…

How to edit these pages title and heading ?

Best Answer

Solved it. The problem was that I had a layout using Magento\Customer\Block\Form\Login, like this:

<block class="Magento\Customer\Block\Form\Login" name="login.sidebar" template="form/login_sidebar.phtml">
    <container name="form.additional.info" as="form_additional_info"/>
</block>

I saw that Magento\Customer\Block\Form\Login class was overriding _prepareLayout() function like this:

/**
 * @return $this
 */
protected function _prepareLayout()
{
    $this->pageConfig->getTitle()->set(__('Customer Login'));
    return parent::_prepareLayout();
}

So I created a custom Block in app/code/MyVendor/MyModule/Block/Form/Login extending Magento\Customer\Block\Form\Loginand overriding _prepareLayout() function like this:

<?php

namespace MyVendor\MyModule\Block\Form;

use Magento\Customer\Block\Form\Login as BaseLogin;

class Login extends BaseLogin
{
    /**
     * @return $this
     */
    protected function _prepareLayout()
    {
        return $this;
    }
}

And then I replaced in my custom layout this:

<block class="Magento\Customer\Block\Form\Login" name="login.sidebar" template="form/login_sidebar.phtml">
    <container name="form.additional.info" as="form_additional_info"/>
</block>

by that:

<block class="MyVendor\MyModule\Block\Form\Login" name="login.sidebar" template="Magento_Customer::form/login_sidebar.phtml">
    <container name="form.additional.info" as="form_additional_info"/>
</block>

And the issue was solved!

Related Topic