2

Magento2 inserts two favicons into the head: <link rel="icon" type="image/x-icon" href="favicon.ico"> and <link rel="shortcut icon" type="image/x-icon" href="favicon.ico">.

Since I will add custom icons by myself in default_head_blocks.xml, I would like to remove the default two. I tried removing them via <remove src="Magento_Theme::favicon.ico" />, but this didn't work. I also tried to override the two icons by adding them to default_head_blocks.xml and using another file, but this didn't work either.

2
  • i think you can change favicon from admin panel.why through xml? May 30, 2016 at 10:44
  • I don't want to change the favicons from the backend because you can only add two. I want to include several favicons to have an optimised favicon for a broad set of devices.
    – Stef
    May 30, 2016 at 11:54

3 Answers 3

1

You cannot remove the default favicons, but you can override them in a theme by adding your favicon.ico in the <your_theme_dir>/Magento_Theme/web/ directory. Other icons to be used as favicons must be also added here, and additionally specified in default_head_blocks.xml. Example: <link src="Magento_Theme::favicon-32x32.png" rel="icon" sizes="32x32" />

1
  • 2
    I'll guess that would be the best solution then. Still, it's odd we're forced to keep using those two icons and don't have the possibility to remove them.
    – Stef
    Jun 7, 2016 at 15:16
0

Favicons links being added by the method prepareFavicon in the class Magento\Framework\View\Page\Config\Renderer I also was requested to remove them so I came with this solution.

Solution:

In your customer module, create an after plugin on the method prepareFavicon in this class Magento\Framework\View\Page\Config\Renderer

Then get an instance from Magento\Framework\View\Asset\GroupedCollection and call the remove method on both favicon keys ('icon', 'shortcut-icon').

Steps:

  1. Create your own custom module (Vendor_Module for example).
  2. Add the plugin into the app/code/Vendor/Module/etc/frontend/di.xml file.
<?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\Framework\View\Page\Config\Renderer">
        <plugin name="remove_default_favicon" type="Vendor\Module\Plugin\Renderer"/>
    </type>
</config>
  1. Create the plugin class as explained below.
<?php

namespace Vendor\Module\Plugin;

use Magento\Framework\View\Page\Config\Renderer as BaseRenderer;
use Magento\Framework\View\Asset\GroupedCollection;

class Renderer
{
    /** @var GroupedCollection */
    private $pageAssets;

    public function __construct(GroupedCollection $pageAssets)
    {
        $this->pageAssets = $pageAssets;
    }

    public function afterPrepareFavicon(BaseRenderer $subject, $result)
    {
        $this->pageAssets->remove('icon');
        $this->pageAssets->remove('shortcut-icon');
    }
}
  1. Run:
bin/magento setup:upgrade
bin/magento setup:di:compile

And that's it, favicons links should disappear from the HTML page.

0

Easily you can comment the public function prepareFavicon() parameters in Renderer.php Should look this way:

public function prepareFavicon()
    {
       /** if ($this->pageConfig->getFaviconFile()) {
            $this->pageConfig->addRemotePageAsset(
                $this->pageConfig->getFaviconFile(),
                Generator\Head::VIRTUAL_CONTENT_TYPE_LINK,
                ['attributes' => ['rel' => 'icon', 'type' => 'image/png']],
                'icon'
            );
            $this->pageConfig->addRemotePageAsset(
                $this->pageConfig->getFaviconFile(),
                Generator\Head::VIRTUAL_CONTENT_TYPE_LINK,
                ['attributes' => ['rel' => 'shortcut icon', 'type' => 'image/png']],
                'shortcut-icon'
            );
        } else {
            $this->pageConfig->addPageAsset(
                $this->pageConfig->getDefaultFavicon(),
                ['attributes' => ['rel' => 'icon', 'type' => 'image/png']],
                'icon'
            );
            $this->pageConfig->addPageAsset(
                $this->pageConfig->getDefaultFavicon(),
                ['attributes' => ['rel' => 'shortcut icon', 'type' => 'image/png']],
                'shortcut-icon'
            );
        } */
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.