I have given configuration to upload Light and dark logo, you can see here:-
System.xml:-
<field id="logo_homepage" translate="label" type="image" sortOrder="20" showInDefault="1" showInWebsite="1" >
<label>Logo For Homepage</label>
<backend_model>YourNamespace\YourModule\Model\Config\Backend\Image</backend_model>
<base_url type="media" scope_info="1">logo</base_url>
</field>
<field id="logo_otherthan_homepage" translate="label" type="image" sortOrder="20" showInDefault="1" showInWebsite="1" >
<label>Logo For Other Pages</label>
<backend_model>YourNamespace\YourModule\Model\Config\Backend\Image</backend_model>
<base_url type="media" scope_info="1">logo</base_url>
<comment>Logo except homepage.</comment>
</field>
And then overrided logo.phtml file into my theme. used helper into this .phtml file to set uploaded dark and light logo of the configuration into the src of logo.phtml.
YourNamespace\YourModule\Model\Config\Backend\Image.php:-
<?php
namespace YourNamespace\YourModule\Model\Config\Backend;
class Image extends \Magento\Config\Model\Config\Backend\Image
{
/**
* The tail part of directory path for uploading
*
*/
const UPLOAD_DIR = 'logo'; // Folder save image
/**
* Return path to directory for upload file
*
* @return string
* @throw \Magento\Framework\Exception\LocalizedException
*/
protected function _getUploadDir()
{
return $this->_mediaDirectory->getAbsolutePath($this->_appendScopeInfo(self::UPLOAD_DIR));
}
/**
* Makes a decision about whether to add info about the scope.
*
* @return boolean
*/
protected function _addWhetherScopeInfo()
{
return true;
}
/**
* Getter for allowed extensions of uploaded files.
*
* @return string[]
*/
protected function _getAllowedExtensions()
{
return ['jpg', 'jpeg', 'gif', 'png', 'svg'];
}
}
YourNamespace\YourModule\Helper\Data.php:-
<?php
namespace YourNamespace\YourModule\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
/**
* Created by Carl Owens ([email protected])
* Company: PartFire Ltd (www.partfire.co.uk)
**/
class Data extends AbstractHelper
{
/**
* @var \Magento\Framework\App\Http\Context
*/
private $httpContext;
/**
* Scope config
*
* @var ScopeConfigInterface
*/
protected $scopeConfig;
const LOGO_HOMEPAGE = 'section/group/logo_homepage';
const LOGO_OTHERTHAN_HOMEPAGE = 'section/group/logo_otherthan_homepage';
const LOGO_FOLDER_NAME = 'logo';
public function __construct(
\Magento\Framework\App\Helper\Context $context,
ScopeConfigInterface $scopeConfig,
\Magento\Framework\App\Request\Http $request
) {
parent::__construct($context);
$this->scopeConfig = $scopeConfig;
$this->_request = $request;
}
public function getLogo()
{
$scope = ScopeInterface::SCOPE_STORE;
$folderName = self::LOGO_FOLDER_NAME;
$logoUrl = '';
if ($this->_request->getFullActionName() == 'cms_index_index') {
$logo = $this->scopeConfig->getValue(self::LOGO_HOMEPAGE, $scope);
} else {
$logo = $this->scopeConfig->getValue(self::LOGO_OTHERTHAN_HOMEPAGE, $scope);
}
if (!empty($logo)) {
$storeLogoPath = $logo;
$path = $folderName . '/' . $storeLogoPath;
$logoUrl = $this->_urlBuilder
->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_MEDIA]) . $path;
}
return $logoUrl;
}
}
app\design\frontend\ThemnNamespace\Themname\Magento_Theme\templates\html\header\logo.phtml:-
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
/**
* @var \Magento\Theme\Block\Html\Header\Logo $block
*/
?>
<?php
$helper = $this->helper('YourNamespace\YourModule\Helper\Data');
$logoSrc = $helper->getLogo()!='' ? $helper->getLogo() : $block->getLogoSrc() ;
?>
<?php $storeName = $block->getThemeName() ? $block->getThemeName() : $block->getLogoAlt();?>
<span data-action="toggle-nav" class="action nav-toggle"><span><?= /* @escapeNotVerified */ __('Toggle Nav') ?></span></span>
<a
class="logo"
href="<?= $block->getUrl('') ?>"
title="<?= /* @escapeNotVerified */ $storeName ?>"
aria-label="store logo">
<img src="<?= /* @escapeNotVerified */ $logoSrc ?>"
title="<?= $block->escapeHtmlAttr($block->getLogoAlt()) ?>"
alt="<?= $block->escapeHtmlAttr($block->getLogoAlt()) ?>"
<?= $block->getLogoWidth() ? 'width="' . $block->getLogoWidth() . '"' : '' ?>
<?= $block->getLogoHeight() ? 'height="' . $block->getLogoHeight() . '"' : '' ?>
/>
</a>