1

Naming the custom module to override the logo.phtml file found at the path seen in the screenshot below a bit confusing to me as the module name is too long to decide on the naming. The vendor of the custom theme being used is Swissup.

I know the module name should begin with Swissup_(What comes here) with me not being able to figure out how to complete the module name after Swissup_. Any advice or help, please.

enter image description here

1

You can override the logo.phtml file under your custom module by making preference for Magento\Theme\Block\Html\Header\Logo class.

Add di.xml file under app/code/Vendor/Module/etc/frontend/di.xml with below content.

<?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\Theme\Block\Html\Header\Logo" type="Vendor\Module\Block\Rewrite\Html\Header\Logo"/>
</config>

After that, create Logo.php under app/code/Vendor/Module/Block/Rewrite/Html/Header with below content.

<?php 

namespace Vendor\Module\Block\Rewrite\Html\Header;

class Logo extends \Magento\Theme\Block\Html\Header\Logo
{
    /**
     * Current template name
     *
     * @var string
     */
    protected $_template = 'Vendor_Module::html/header/logo.phtml';
}

Copy the logo.phtml file from your theme and placed under app/code/Vendor/Module/view/frontend/templates/html/header.

Run the deployment commands and check your changes.

Hope it helps!!!

| improve this answer | |
  • thanks for your time and the help. Could the module name be anything? Will it do if I name it as Swissup_Logo or Mycompanyname_Logo? – CodeForGood Apr 28 at 6:17
  • Yes, you can change the name of the module whatever you want. Also, please add all required files to create a module Registration.php and module.xml. – Sumit Apr 28 at 6:19
  • I am trying your solution now. In type="Vendor\Module\Block\Rewrite\Html\Header\Logo" do I need to replace the Vendor with my company name and Module with module name? – CodeForGood Apr 28 at 10:07
  • Yes, you're right. You can replace it with your names. Please make sure you can change the names in the Logo.php file as well. – Sumit Apr 28 at 10:37
  • Followed all the instructions by you, but the logo part itself is now missing from the homepage. I wonder what must have gone wrong. – CodeForGood Apr 28 at 10:42
0

You can manage via after plugin on getTemplate() .

Create di.xml at app/code/StackExchange/Theme/etc/frontend

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Theme\Block\Html\Header\Logo">
        <plugin disabled="false" name="StackExchange_Theme_Plugin_Magento_Theme_Block_Html_Header_Logo" sortOrder="10" type="StackExchange\Theme\Plugin\Magento\Theme\Block\Html\Header\Logo"/>
    </type>
</config>

Then on plugin class StackExchange\Theme\Plugin\Magento\Theme\Block\Html\Header\Logo.php and

from afterGetTemplate() changes template from here.

<?php  declare(strict_types=1);

namespace StackExchange\Theme\Plugin\Magento\Theme\Block\Html\Header;

class Logo
{

    public function afterGetTemplate(
        \Magento\Theme\Block\Html\Header\Logo $subject,
        $result
    ) {

        return 'StackExchange_Theme::html/header/logo.phtml';
    }
}
| improve this answer | |
  • thanks a lot for the help.I presume I must replace app/code/StackExchange/Theme/etc/frontend with app/code/MyCompanyName/Swissup/etc/frontend. Is this correct? – CodeForGood Apr 28 at 7:44
  • yes.. app/code/StackExchange/Theme/etc/frontend – Amit Bera Apr 28 at 8:58

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.