Question

In one Magento 2.2.6 instance something very odd occurs: the meta page title shows

<title>Customer Login</title>

and the page title in the content also shows Customer Login - even though the route has nothing to to with the customer login!

The module in question is omikron/magento2-factfinder and the route in question is factfinder/result/index. Currently the extension does not set its own page title (I've already created a PR for that), but even when you add

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Search Results</title>
        …
    </head>
    <body>
        …
    </body>
</page>

the page still shows <title>Customer Login</title> in the meta tags - however the page title in the <body> will indeed show Search Results.

In a second Magento 2.2.6 instance this problem does not occur.

How can this be? What could be the reason why the page title is overridden in such a manner for a particular route?

Was it helpful?

Solution

This is something I've seen on Magento 1 before.
I have a hunch that you have a login form displayed in every page.
Something like a popup that appears when you click on the 'login' button, or something like that.
If this is the case, the problem is cause by that login form.
I assume the block class that displays the mini-form is Magento\Customer\Block\Form\Login or a class that extends it.
This class has a method called _prepareLayout which is called automatically for each block before the block is rendered.

For the login class, the method looks like this:

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

This means, that every page that contains this block will get the meta title Customer Login.

You have 2 options here (there may be more):
Option 1: the clean way.

Create a class that extends Magento\Customer\Block\Form\Login. Your new class should contain a single method

/**
 * @return $this
 */
protected function _prepareLayout()
{
    return $this;
}

Then, use this new class in the layout for the mini-login form.

Option 2: quick and dirty.
Same as in option 1, create the same class, then in the di.xml file of your module set a preference for the default login block to your new class

<preference for="Magento\Customer\Block\Form\Login" type="Your\NewClass\Name\Here" />

This should work also, but you might lose the meta title on the actual login page.

As a note: this is a magento inconsistency. the login class knows and does too much. It knows details about the context that it should not know.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top