{"id":51023,"date":"2016-06-07T06:08:48","date_gmt":"2016-06-07T06:08:48","guid":{"rendered":"http:\/\/webkul.com\/blog\/?p=51023"},"modified":"2024-02-22T14:26:58","modified_gmt":"2024-02-22T14:26:58","slug":"add-color-picker-magento2-system-config","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/","title":{"rendered":"How To Add ColorPicker In Magento2 System Config"},"content":{"rendered":"\n<p>As an extension developer sometimes you have to give admin the permission to control the ui of particular pages, it can include colors too, like background color, font color etc. Magento 2 uses jQuery colorpicker for that. Today we will see how we can add colorpicker in system configuration fields.<\/p>\n\n\n\n<p>I am assuming you already know how to create module in magento2, so I am &nbsp;going to tell you what changes you have to make in your system configuration field to add colorpicker in that .<\/p>\n\n\n\n<p>I&#8217;m assuming that the company name is Webkul and module name is MyColorPicker.<\/p>\n\n\n\n<p>First in your system config file located at :<\/p>\n\n\n\n<p>app\/code\/Webkul\/MyColorPicker\/etc\/adminhtml\/system.xml<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;config xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:module:Magento_Config:etc\/system_file.xsd&quot;&gt;\n    &lt;system&gt;\n        &lt;tab id=&quot;webkul&quot; translate=&quot;label&quot; sortOrder=&quot;10&quot;&gt;\n            &lt;label&gt;Webkul&lt;\/label&gt;\n        &lt;\/tab&gt;\n        &lt;section id=&quot;mycolorpicker&quot; translate=&quot;label&quot; type=&quot;text&quot; sortOrder=&quot;300&quot; showInDefault=&quot;1&quot; showInWebsite=&quot;1&quot; showInStore=&quot;1&quot;&gt;\n            &lt;label&gt;My Color Picker Demo&lt;\/label&gt;\n            &lt;!-- Assign section to tab --&gt;\n            &lt;tab&gt;webkul&lt;\/tab&gt;\n            &lt;resource&gt;Webkul_MyColorPicker::config_facebookwallpost&lt;\/resource&gt;\n            &lt;!-- create group for fields in section --&gt;\n           &lt;group id=&quot;parameter&quot; translate=&quot;label&quot; type=&quot;text&quot; sortOrder=&quot;1&quot; showInDefault=&quot;1&quot; showInWebsite=&quot;1&quot; showInStore=&quot;1&quot;&gt;\n\n                &lt;label&gt;Colorpicker Settings&lt;\/label&gt;\n                &lt;field id=&quot;wall_color&quot; translate=&quot;label comment&quot;  sortOrder=&quot;116&quot; type=&quot;text&quot; showInDefault=&quot;1&quot; showInWebsite=&quot;1&quot; showInStore=&quot;1&quot;&gt;\n                    &lt;label&gt;Wall Color&lt;\/label&gt;\n                    &lt;frontend_model&gt;Webkul\\MyColorPicker\\Block\\ColorPicker&lt;\/frontend_model&gt;\n                &lt;\/field&gt;\n            &lt;\/group&gt;\n        &lt;\/section&gt;\n    &lt;\/system&gt;\n&lt;\/config&gt;<\/pre>\n\n\n\n<p>In the above code I have created a field in system configuration that will allow you to choose any color using jquery colorpicker. In the code you can see the &lt;frontend _model&gt; it is required to add colorpicker in the field.<\/p>\n\n\n\n<p>Now create a layout file named &#8220;adminhtml_system_config_edit.xml&#8221; located in app\/code\/Webkul\/MyColorPicker\/view\/adminhtml\/layout\/<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\"&gt;\n    &lt;head&gt;\n        &lt;css src=\"jquery\/colorpicker\/css\/colorpicker.css\"\/&gt;\n    &lt;\/head&gt;\n&lt;\/page&gt;<\/pre>\n\n\n\n<p>In the above code, we&#8217;re adding css file for colorpicker.<\/p>\n\n\n\n<p>So now we will create this block class :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Webkul\\MyColorPicker\\Block;\n\nclass ColorPicker extends \\Magento\\Config\\Block\\System\\Config\\Form\\Field\n{\n    \/**\n     * Add color picker in admin configuration fields\n     * @param  \\Magento\\Framework\\Data\\Form\\Element\\AbstractElement $element\n     * @return string script\n     *\/\n    protected function _getElementHtml(\\Magento\\Framework\\Data\\Form\\Element\\AbstractElement $element)\n    {\n        $html = $element-&gt;getElementHtml();\n        $value = $element-&gt;getData('value');\n\n        $html .= '&lt;script type=\"text\/javascript\"&gt;\n            require([\"jquery\",\"jquery\/colorpicker\/js\/colorpicker\"], function ($) {\n                $(document).ready(function () {\n                    var thisElement = $(\"#' . $element-&gt;getHtmlId() . '\");\n                    thisElement.css(\"backgroundColor\", \"'. $value .'\");\n                    thisElement.ColorPicker({\n                        color: \"'. $value .'\",\n                        onChange: function (hsb, hex, rgb) {\n                            thisElement.css(\"backgroundColor\", \"#\" + hex).val(\"#\" + hex);\n                        }\n                    });\n                });\n            });\n            &lt;\/script&gt;';\n        return $html;\n    }\n}<\/pre>\n\n\n\n<p>In the above script you can see that we have returned the javascript from the &#8216;_getElementHtml&#8217; method and form field it will simply add colorpicker on the field on change event .<\/p>\n\n\n\n<p>That&#8217;s all for this article, try this it will work for sure and if you \u00a0have any issues please comment below.<\/p>\n\n\n\n<p>Thanks \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As an extension developer sometimes you have to give admin the permission to control the ui of particular pages, it can include colors too, like background color, font color etc. Magento 2 uses jQuery colorpicker for that. Today we will see how we can add colorpicker in system configuration fields. I am assuming you already <a href=\"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":33,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-51023","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How To Add ColorPicker on Magento2 Configuration<\/title>\n<meta name=\"description\" content=\"In this article , we will see how we can add jQuery colorpicker in the system config fields in Magento 2 .\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Add ColorPicker on Magento2 Configuration\" \/>\n<meta property=\"og:description\" content=\"In this article , we will see how we can add jQuery colorpicker in the system config fields in Magento 2 .\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2016-06-07T06:08:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-22T14:26:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ashutosh Srivastava\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashutosh Srivastava\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/\"},\"author\":{\"name\":\"Ashutosh Srivastava\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970\"},\"headline\":\"How To Add ColorPicker In Magento2 System Config\",\"datePublished\":\"2016-06-07T06:08:48+00:00\",\"dateModified\":\"2024-02-22T14:26:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/\"},\"wordCount\":257,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/\",\"url\":\"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/\",\"name\":\"How To Add ColorPicker on Magento2 Configuration\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2016-06-07T06:08:48+00:00\",\"dateModified\":\"2024-02-22T14:26:58+00:00\",\"description\":\"In this article , we will see how we can add jQuery colorpicker in the system config fields in Magento 2 .\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To Add ColorPicker In Magento2 System Config\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970\",\"name\":\"Ashutosh Srivastava\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Ashutosh Srivastava\"},\"sameAs\":[\"http:\/\/webkul.com\"],\"url\":\"https:\/\/webkul.com\/blog\/author\/ashutosh\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Add ColorPicker on Magento2 Configuration","description":"In this article , we will see how we can add jQuery colorpicker in the system config fields in Magento 2 .","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/","og_locale":"en_US","og_type":"article","og_title":"How To Add ColorPicker on Magento2 Configuration","og_description":"In this article , we will see how we can add jQuery colorpicker in the system config fields in Magento 2 .","og_url":"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2016-06-07T06:08:48+00:00","article_modified_time":"2024-02-22T14:26:58+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Ashutosh Srivastava","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Ashutosh Srivastava","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/"},"author":{"name":"Ashutosh Srivastava","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970"},"headline":"How To Add ColorPicker In Magento2 System Config","datePublished":"2016-06-07T06:08:48+00:00","dateModified":"2024-02-22T14:26:58+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/"},"wordCount":257,"commentCount":3,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/","url":"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/","name":"How To Add ColorPicker on Magento2 Configuration","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2016-06-07T06:08:48+00:00","dateModified":"2024-02-22T14:26:58+00:00","description":"In this article , we will see how we can add jQuery colorpicker in the system config fields in Magento 2 .","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/add-color-picker-magento2-system-config\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How To Add ColorPicker In Magento2 System Config"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970","name":"Ashutosh Srivastava","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Ashutosh Srivastava"},"sameAs":["http:\/\/webkul.com"],"url":"https:\/\/webkul.com\/blog\/author\/ashutosh\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/51023","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=51023"}],"version-history":[{"count":10,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/51023\/revisions"}],"predecessor-version":[{"id":423918,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/51023\/revisions\/423918"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=51023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=51023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=51023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}