Adding Custom JS and CSS Files to Admin Product Grid Using XML
As a senior developer specializing in e-commerce platforms, I often receive queries about customizing admin interfaces using XML. In this post, we’ll walk through the process of adding JavaScript (JS) and Cascading Style Sheets (CSS) files to your Magento 2 admin product grid using XML configuration.
Why Use XML for Adding JS/CSS Files?
Using XML allows you to keep your code clean and maintainable, separating business logic from presentation. It also enables easy updates and modifications without touching the core PHP file.
Step-by-Step Guide
1. Create Your Custom JS File
Assuming you have already developed lightbox.js and cat_filter.js, place them in your custom module’s directory:
/app/code/Vendor/Extension/view/adminhtml/web/js/
2. Define XML Configuration
Create a new XML file, e.g., custom_grid.xml, inside the following path:
/app/code/Vendor/Extension/etc/adminhtml/di.xml
Add the following configuration to include your JS and CSS files:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Backend\Block\Widget\Grid\Container">
<arguments>
<argument name="jsComponentType" xsi:type="string">Vendor_Extension/js/container</argument>
</arguments>
</type>
<virtualType name="Vendor_Extension/js/container" type="Magento\Framework\View\Element\Template">
<arguments>
<argument name="template" xsi:type="string">Vendor_Extension/grid/container.phtml</argument>
<argument name="defaultTemplate" xsi:type="string">Magento_Catalog::widget/grid/container.phtml</argument>
</arguments>
</virtualType>
<type name="Magento\Catalog\Block\Adminhtml\Product">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="grid-container" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/container</item>
<item name="template" xsi:type="string">Vendor_Extension/grid/container.phtml</item>
</item>
</item>
</argument>
</arguments>
</type>
</config>
Replace Vendor with your actual vendor name.
3. Create the Custom PHTML Template
Create a new file named container.phtml inside:
/app/code/Vendor/Extension/view/adminhtml/templates/grid/
Add the following content to include your JS and CSS files:
<div class="admin__main">
<div id="product_grid">
<?php echo $block->getGridHtml() ?>
</div>
</div>
<?= $block->place('head.js') ?>
<js src="<?= $block->getViewFileUrl('Vendor_Extension/js/lightbox.js') ?>" />
<js src="<?= $block->getViewFileUrl('Vendor_Extension/js/cat_filter.js') ?>" />
<?= $block->place('end_head') ?>
<?= $block->place('head.css') ?>
<link rel="stylesheet" href="<?= $block->getViewFileUrl('Vendor_Extension/css/style.css') ?>">
<?= $block->place('end_head') ?>
4. Clear Cache
Finally, clear your Magento cache to apply the changes:
php bin/magento c:f
Conclusion
Adding custom JavaScript and CSS files to the admin product grid using XML configuration allows for a clean separation of concerns and easy maintenance. By following this step-by-step guide, you can enhance your admin interface with minimal core file modifications.
Remember that proper XML syntax and correct file placement are crucial for successful implementation. If you encounter any issues or have further questions about Magento development best practices, feel free to explore our Laravel Company blog for more in-depth tutorials and tips from experienced developers like me!