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.

2
  • Did you solve this? Dec 18, 2018 at 4:16
  • @themanwhoknowstheman see below :)
    – Mageinn
    Dec 20, 2018 at 10:06

1 Answer 1

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.

1
  • Feel like a plugin should be able to more simply handle this but haven't figured one out yet.
    – addMitt
    Apr 16, 2021 at 19:46

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.