4

I have been scouring the forums for the right way of changing the page title for a specific page or layout in Magento 2 but many answers rely on action methods which are now deprecated.

Essentially, I have set the store up so the suffix for each page title is "- CompanyName" but I would like to know how to create an exception for this for the home page (and other specific landing pages). I have changed the page title in a custom layout file successfully but the suffix is still appended to the end. Is there a sensible way of stopping this from happening on a specific page, like the home page?

Thank you.

3

You need to overwrite Magento\Framework\View\Page\Title::addConfigValues function.

etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="YourVendor_YourModule" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Framework"/>
        </sequence>
    </module>
</config>

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Framework\View\Page\Title" type="YourVendor\YourModule\Model\View\Page\Title" />
</config>

YourVendor/YourModule/Model/View/Page/Title.php

<?php
namespace YourVendor\YourModule\Model\View\Page;

/**
* Page title
*
* @api
* @since 100.0.2
*/
class Title extends \Magento\Framework\View\Page\Title
{
    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * @var \Magento\Framework\App\Request\Http
     */
    private $request;

    /**
     * Title constructor.
     * @param \Magento\Framework\App\Request\Http $request
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     */
    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->request = $request;
        $this->scopeConfig = $scopeConfig;

        parent::__construct($scopeConfig);
    }

    /**
     * @param string $title
     * @return string
     */
    protected function addConfigValues($title)
    {
        $preparedTitle = $this->scopeConfig->getValue(
                'design/head/title_prefix',
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE
            ) . ' ' . $title;
        $postfix = ' ' . $this->scopeConfig->getValue(
                'design/head/title_suffix',
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE
            );
        if ($this->request->getFullActionName() == 'cms_index_index') {
            $postfix = '';
        }
        return trim($preparedTitle . $postfix);
    }
}

Replace YourVendor_YourModule with your own vendor and module names.

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.