{"id":2853,"date":"2016-06-06T22:27:29","date_gmt":"2016-06-06T16:57:29","guid":{"rendered":"http:\/\/www.magewallet.com\/?p=217"},"modified":"2020-03-13T16:07:26","modified_gmt":"2020-03-13T10:37:26","slug":"add-color-picker-system-configuration-magento-2","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/add-color-picker-system-configuration-magento-2\/","title":{"rendered":"How to add color picker on system configuration in Magento 2?"},"content":{"rendered":"<p>Recently I have found one solution to add color picker on system configuration options in Magento 2. Lets discuss in detail.<\/p>\n<p>In order to add color picker on system configuration options, we have to add one system field and require one Block file.<\/p>\n<p>Use below code to add system field.<\/p>\n<pre class=\"lang:default decode:true \">&lt;field id=\"color\" translate=\"label\" type=\"text\" sortOrder=\"10\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\"&gt;\r\n                    &lt;label&gt;Text Color&lt;\/label&gt;\r\n                    &lt;frontend_model&gt;Hiddentechies\\DigitalProduct\\Block\\Color&lt;\/frontend_model&gt;\r\n                &lt;\/field&gt;<\/pre>\n<p>Now lets create Block file &#8211; Color.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Hiddentechies\\DigitalProduct\\Block;\r\n\r\nuse Magento\\Framework\\Registry;\r\n\r\nclass Color extends \\Magento\\Config\\Block\\System\\Config\\Form\\Field {\r\n\r\n    protected $_coreRegistry;\r\n\r\n    public function __construct(\r\n    \\Magento\\Backend\\Block\\Template\\Context $context, Registry $coreRegistry, array $data = []\r\n    ) {\r\n        $this-&gt;_coreRegistry = $coreRegistry;\r\n        parent::__construct($context, $data);\r\n    }\r\n\r\n    protected function _getElementHtml(\\Magento\\Framework\\Data\\Form\\Element\\AbstractElement $element) {\r\n        $base = $this-&gt;getBaseUrl();\r\n        $html = $element-&gt;getElementHtml();\r\n        $cpPath = $base . 'pub\/media\/js\/';\r\n        if (!$this-&gt;_coreRegistry-&gt;registry('colorpicker_loaded')) {\r\n            $html .= '&lt;script type=\"text\/javascript\" src=\"' . $cpPath . 'jscolor.js\"&gt;&lt;\/script&gt;&lt;style type=\"text\/css\"&gt;input.jscolor { background-image: url(' . $cpPath . 'color.png) !important; background-position: calc(100% - 8px) center; background-repeat: no-repeat;} input.jscolor.disabled,input.jscolor[disabled] { pointer-events: none; }&lt;\/style&gt;';\r\n            $this-&gt;_coreRegistry-&gt;registry('colorpicker_loaded', 1);\r\n        }\r\n        $html .= '&lt;script type=\"text\/javascript\"&gt;\r\n                var el = document.getElementById(\"' . $element-&gt;getHtmlId() . '\");\r\n                el.className = el.className + \" jscolor\";\r\n            &lt;\/script&gt;';\r\n        return $html;\r\n    }\r\n\r\n}<\/pre>\n<p>Lets discuss some terms used in this block.<br \/>\n<strong>Hiddentechies<\/strong> \u2013 Vendor Name<br \/>\n<strong>DigitalProduct<\/strong>\u2013 Module Name<br \/>\n<strong>color.png<\/strong> \u2013 add color icon image at pub\/media\/js\/ directory<br \/>\n<strong>jscolor.js<\/strong> &#8211; add color picker js at pub\/media\/js\/ directory<\/p>\n<p>You can download the color picker Js from below URL.<br \/>\n<strong>http:\/\/jscolor.com\/download\/<\/strong><\/p>\n<p>Thats it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I have found one solution to add color picker on system configuration options in Magento 2. Lets discuss in detail. In order to add color picker on system configuration options, we have to add one system field and require one Block file. Use below code to add system field. &lt;field id=&#8221;color&#8221; translate=&#8221;label&#8221; type=&#8221;text&#8221; sortOrder=&#8221;10&#8243;&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/add-color-picker-system-configuration-magento-2\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":4815,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[201,10,45,202],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2853"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=2853"}],"version-history":[{"count":4,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2853\/revisions"}],"predecessor-version":[{"id":10517,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2853\/revisions\/10517"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/4815"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=2853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=2853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=2853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}