How to Add Color Picker in Magento 2 Admin Configuration
Sometimes while creating an extension, you may need to provide admin the complete control of the frontend UI. Changing UI includes the change of background color, font colors which arises the need of adding color picker in Magento 2 Admin Configuration.
Generally, the settings in a Magento 2 extension resides under Stores –> Configuration and here you need to add the color picker to allow change the color in frontend UI. Here, I’ll show to add color picker in Magento 2 Admin Configuration in just three steps!
Steps to Add Color Picker in Magento 2 Admin Configuration:
- Add color picker to textbox through system.xml file located at app\code\Vendor\Module\etc\adminhtml
123456789101112131415<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"><system><section><group id="my_color_group" ...><field id="my_color_option" translate="label" type="text" sortOrder="10" showInDefault="1"showInWebsite="1" showInStore="1"><label>Background Color</label><comment><![CDATA[Background color]]></comment><frontend_model>Vendor\Module\Block\Color</frontend_model></field></group></section></system></config> - Create one Color.php file at below location under the hood of extension app\code\Vendor\Module\Block
123456789101112131415161718192021222324252627282930<?phpnamespace Vendor\Module\Block;use Magento\Config\Block\System\Config\Form\Field;use Magento\Backend\Block\Template\Context;use Magento\Framework\Registry;use Magento\Framework\Data\Form\Element\AbstractElement;class Color extends Field{protected $_coreRegistry;public function __construct($context, Registry $coreRegistry, array $data = []){$this->_coreRegistry = $coreRegistry;parent::__construct($context, $data);}protected function _getElementHtml(AbstractElement $element){$html = $element->getElementHtml();$cpPath = $this->getViewFileUrl('Vendor_Module::js');if (!$this->_coreRegistry->registry('colorpicker_loaded')) {$html .= '<script type="text/javascript" src="' . $cpPath . '/' . 'jscolor.js"></script>';$this->_coreRegistry->registry('colorpicker_loaded', 1);}$html .= '<script type="text/javascript">var el = document.getElementById("' . $element->getHtmlId() . '");el.className = el.className + " jscolor{hash:true}";</script>';return $html;}}?> - Add the JS file at Vendor_Module/view/adminhtml/web/js/jscolor.js, click here to copy jscolor.js
Clear the cache and navigate to stores configuration. The color picker option will be displayed. Use this color picker anywhere in Magento 2 with your own customized code when needed!
Thanks for reading. You may post your doubts in the comments which I’ll be happy to solve.
Hit 5 stars if you like my article ?
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
Here is the Error in system.xml file Vendor/Module/Block/Color
This is not an Correct Line.
The Proper Way Of Written this line is Vendor\Module\Block\Color
There is issue about the slashes ‘ / ‘ this type of slash is not accept by the system.xml file you have to use ‘ \ ‘ this slash
My bad! Thank you, Vishal for drawing my attention. I’ve corrected it in the post.