Bhaveshpp

Professional Magento Developr - Healp eachother to grow

Magento 223: add font awsom on specific page

18 Dec 2021 » magento2, php

add event

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="layout_load_before">
        <observer name="fontawesome5_add_assets" instance="Td\StoreSeller\Observer\AddPageAssets" />
    </event>
</config>

Add observer

<?php

namespace Td\StoreSeller\Observer;

use Magento\Framework\App;
class AddPageAssets implements \Magento\Framework\Event\ObserverInterface
{
    /**
     * @var \Magento\Framework\View\Page\Config
     */
    protected $pageConfig;

    /**
     * @var \Swissup\FontAwesome5\Helper\Data
     */
    protected $helper;

    /**
     * @var \Magento\Framework\App\Request\Http
     */
    protected $request;

    
    public function __construct(
        \Magento\Framework\View\Page\Config $pageConfig,
        App\Request\Http $request
    ) {
        $this->request = $request;
        $this->pageConfig = $pageConfig;
    }

    /**
     * Add FontAwesome assets according to module config
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        if ($observer->getData('full_action_name') == 'cms_page_view') 
        {
            if ($this->request->getRequestString() == '/evaluanos') 
            {
                $asset = new \Magento\Framework\DataObject();
                $asset->addData([
                    'properties' => [],
                    'name' => 'Td_StoreSeller',
                    'url'  => 'Td_StoreSeller::fontawesome-free-5.3.1-web/css/all.min.css',
                    'type' => 'css'
                ]);
                $this->addLocalAsset($asset);
            }
        }
    }

    protected function addLocalAsset($asset)
    {
        $this->pageConfig->addPageAsset(
            $asset->getUrl(),
            $asset->getProperties(),
            $asset->getName()
        );
    }
}

add css file to app/code/vendor/module/view/frontend/web/ directory

Thanks.