Magento 2 : Alternate Logo for Page Layout
The better way to do this will be using helper function.
in your Magento_Theme/layout/default.xml
you can pass argument for logo like this-
<argument name="logo_file" xsi:type="helper" helper="Namespace\ModuleName\Helper\Data::getLogoImage"></argument>
and then in your helper file you can return logo based on current url, Your helper file-
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\App\Request\Http $objectManager
)
{
$this->_objectManager = $objectManager;
}
public function getLogoImage()
{
$fullAction = $this->_objectManager->getFullActionName();;
if($fullAction == 'cms_index_index')
{
$logo = 'images/logo_light_sm.png';
}
else
{
$logo = 'images/logo_dark_sm.png';
}
return $logo;
}
Create helper to change logo dynamically
Override default.xml in your theme and add helper in logo_file argument
Magento_Theme/layout/default.xml
<argument name="logo_file" xsi:type="helper" helper="Vendor\Module\Helper\Data::getLogo"></argument>
In your helper, you can get logo based on current url like this.
<?php
namespace Vendor\Module\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $_request;
public function __construct
(
\Magento\Framework\App\Request\Http $request
) {
$this->_request = $request;
}
public function getLogo()
{
if ($this->_request->getFullActionName() == 'cms_index_index') {
$logo = 'images/logo_light_sm.png';
} else {
$logo = 'images/logo_dark_sm.png';
}
return $logo;
}
}
I tried, this is working for me very well.
Magento_Cms/layout/cms_index_index.xml:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="logo">
<arguments>
<argument name="logo_file" xsi:type="string">images/logo_light_sm.png</argument>
</arguments>
</referenceBlock>
</body>
</page>
This solution is no better than creating helper class?