Magento 2: Custom module to override the logo.phtml file of the custom theme
-
13-04-2021 - |
Вопрос
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.
Решение
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!!!
Другие советы
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';
}
}