i am using Magento 2 , In my case i want to change programmatically the instance's logo and favicon , but i can't find the right file to change them ,
can anyone help me do that please
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityFor set Logo
app\code\VendoreName\ModuleName\view\frontend\layout
default.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">
<action method="setLogoFile">
<argument name="logo_file" xsi:type="string">VendoreName_ModuleName::images/logo.png</argument>
</action>
</referenceBlock>
</body>
</page>
Put your logo image to this path app\code\VendoreName\ModuleName\view\frontend\web\images here my image file name is logo.png.
For set fevicon icon
- Add your icons in the <your_theme_dir>/Magento_Theme/web/ directory.
- In the <your_theme_dir>/Magento_Theme/layout/default_head_blocks.xml layout file specify the paths to the icons and their sizes. For example, if you added a favicon-32x32.png icon and want it to be used as a 32px x 32px favicon, your default_head_blocks.xml would be like following:
<?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">
<head>
<link src="Magento_Theme::images/logo.png" rel="icon" sizes="32x32" />
</head>
</page>
- Put your fevicon image into <your_theme_dir>/Magento_Theme/web/images/logo.png
*Note:- For your changes to be applied, clear the browser cache, and the following directories on the server (do not delete the .htaccess file!): - pub/static - all directories under var
I Hope This Helps You.*
Just inject the class \Magento\Config\Model\ResourceModel\Config $_resourceConfig
and set value for web/default/cms_home_page to desired page ‘youridentifier’.
public function __construct(\Magento\Config\Model\ResourceModel\Config $_resourceConfig)
{
$this->_resourceConfig = $_resourceConfig;
}
$this->_resourceConfig->saveConfig('web/default/cms_home_page', 'youridentifier', 'default', 0);
You can create a DataPatch to update the path to both the logo and the favicon:
<?php declare(strict_types=1);
namespace VENDOR\DataPatches\Setup\Patch\Data;
use Magento\Config\Model\ResourceModel\Config;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Store\Model\ScopeInterface;
class UpdateFavicon implements DataPatchInterface
{
/**
* @var Config
*/
protected $config;
public function __construct(
Config $config
) {
$this->config = $config;
}
public function apply()
{
$config = $this->config;
$path = 'PATHTOFILE';
$config->saveConfig(
'design/head/shortcut_icon',
$path,
ScopeInterface::SCOPE_WEBSITES,
0
);
}
public static function getDependencies()
{
return [];
}
public function getAliases()
{
return [];
}
}
Above is an example for changing the favicon. You can also do this for Logo by using design/header/logo_src
.
However, in this example it is assumed there's already a logo / favicon in your server.