{"id":7393,"date":"2019-10-04T12:31:08","date_gmt":"2019-10-04T07:01:08","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7393"},"modified":"2019-10-04T15:43:43","modified_gmt":"2019-10-04T10:13:43","slug":"magento-2-add-date-picker-field-in-system-configuration","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-add-date-picker-field-in-system-configuration\/","title":{"rendered":"Magento 2 &#8211; How to Add Date Picker Field in System Configuration"},"content":{"rendered":"<p>In this post I am going to explain how to add date picker field in magento 2 system.xml file or you can say in magento 2 system configuration.<\/p>\n<p>Here, I am using my custom module \u2013 Ht_Mymodule<\/p>\n<p>First, open your module&#8217;s system.xml file and add below code in it.<\/p>\n<p>File Path: app\/code\/Ht\/Mymodule\/etc\/adminhtml\/system.xml<\/p>\n<pre class=\"lang:default decode:true \">&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Store:etc\/config.xsd\"&gt;\r\n    &lt;system&gt;\r\n        &lt;tab id=\"mymoduletab\" translate=\"label\" sortOrder=\"50\"&gt;\r\n            &lt;label&gt;Ht Mymodule&lt;\/label&gt;\r\n        &lt;\/tab&gt;\r\n        &lt;section id=\"mymodulesection\" translate=\"label\" type=\"text\" sortOrder=\"10\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\"&gt;\r\n            &lt;label&gt;Configuration&lt;\/label&gt;\r\n            &lt;tab&gt;mymoduletab&lt;\/tab&gt;\r\n            &lt;resource&gt;Ht_Mymodule::configuration&lt;\/resource&gt;\r\n            &lt;group id=\"general\" translate=\"label\" type=\"text\" sortOrder=\"10\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\"&gt;\r\n                &lt;label&gt;General&lt;\/label&gt;\r\n                &lt;field id=\"date_field\" translate=\"label comment\" sortOrder=\"1\" type=\"text\" showInDefault=\"1\" showInStore=\"1\" &gt;\r\n                    &lt;label&gt;Date&lt;\/label&gt;\r\n                    &lt;frontend_model&gt;Ht\\Mymodule\\Block\\DatePicker&lt;\/frontend_model&gt;\r\n                &lt;\/field&gt;\r\n            &lt;\/group&gt;\r\n        &lt;\/section&gt;\r\n    &lt;\/system&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>Now we have added &#8220;Ht\\Mymodule\\Block\\DatePicker&#8221; as frontend model. So now we are going to create DatePicker class in file DatePicker.php<\/p>\n<p>File Path: app\/code\/Ht\/Mymodule\/Block\/DatePicker.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Ht\\Mymodule\\Block;\r\n\r\nclass DatePicker 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, \r\n        \\Magento\\Framework\\Registry $coreRegistry, \r\n        array $data = []\r\n    )\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    {\r\n        $html = $element-&gt;getElementHtml();\r\n        if (!$this-&gt;_coreRegistry-&gt;registry('datepicker_loaded')) {\r\n            $this-&gt;_coreRegistry-&gt;registry('datepicker_loaded', 1);\r\n        }\r\n        $html .= '&lt;button type=\"button\" class=\"custom-ui-datepicker-trigger\"&gt;&lt;span&gt;Select Date&lt;\/span&gt;&lt;\/button&gt;';\r\n        \r\n        $html .= '&lt;script type=\"text\/javascript\"&gt;\r\n            require([\"jquery\", \"jquery\/ui\"], function (jq) {\r\n                jq(document).ready(function () {\r\n                    jq(\"#' . $element-&gt;getHtmlId() . '\").datepicker( { dateFormat: \"dd\/mm\/yy\" } );\r\n                    jq(\".custom-ui-datepicker-trigger\").removeAttr(\"style\");\r\n                    jq(\".custom-ui-datepicker-trigger\").click(function(){\r\n                        jq(\"#' . $element-&gt;getHtmlId() . '\").focus();\r\n                    });\r\n                });\r\n            });\r\n            &lt;\/script&gt;';\r\n        $html .= '&lt;style&gt;\r\n            .custom-ui-datepicker-trigger {\r\n                background-image: none;\r\n                background: 0 0;\r\n                -moz-box-sizing: content-box;\r\n                border: 0;\r\n                box-shadow: none;\r\n                line-height: inherit;\r\n                margin: 0;\r\n                padding: 0;\r\n                text-shadow: none;\r\n                font-weight: 400;\r\n                text-decoration: none;\r\n                display: inline-block;\r\n                vertical-align: middle;\r\n                position: relative;\r\n            }\r\n            .custom-ui-datepicker-trigger::after {\r\n                -webkit-font-smoothing: antialiased;\r\n                -moz-osx-font-smoothing: grayscale;\r\n                font-size: 42px;\r\n                line-height: 30px;\r\n                color: #514943;\r\n                content: \"\\e612\";\r\n                font-family: \"icons-blank-theme\";\r\n                vertical-align: middle;\r\n                display: inline-block;\r\n                font-weight: 400;\r\n                overflow: hidden;\r\n                speak: none;\r\n                text-align: center;\r\n                position: absolute;\r\n                top: -17px;\r\n                right: 0;\r\n            }\r\n            .custom-ui-datepicker-trigger &gt; span {\r\n                border: 0;\r\n                clip: rect(0,0,0,0);\r\n                height: 1px;\r\n                margin: -1px;\r\n                overflow: hidden;\r\n                padding: 0;\r\n                position: absolute;\r\n                width: 1px;\r\n            }\r\n            &lt;\/style&gt;';\r\n        return $html;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to add date picker field in magento 2 system.xml file or you can say in magento 2 system configuration. Here, I am using my custom module \u2013 Ht_Mymodule First, open your module&#8217;s system.xml file and add below code in it. File Path: app\/code\/Ht\/Mymodule\/etc\/adminhtml\/system.xml &lt;config xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=&#8221;urn:magento:module:Magento_Store:etc\/config.xsd&#8221;&gt;&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-add-date-picker-field-in-system-configuration\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7411,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7393"}],"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=7393"}],"version-history":[{"count":2,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7393\/revisions"}],"predecessor-version":[{"id":7412,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7393\/revisions\/7412"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7411"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}