Create custom field programmatically I'm trying to re-create a field in my custom form with a date-picker. A dpm of my already existing form learned me the form looks like this:



Is it correctly to assume you can create the field with following code? Because I get an empty result...

$form['my_field_field_periods'] = array(
    '#title' =&gt; t('Periode 1'),
    '#type' =&gt; 'container',
    '#attributes' =&gt; array(
      'class' =&gt; array(
        'field-type-datetime', 'field-name-field-periods','field-widget-date-popup')
    ),
    - and so on -
  );
You can add a regular select box style date field with the form api like this:

$form['date1'] = array(
  '#type' =&gt; 'date', 
  '#title' =&gt; t('Date 1'), 
  '#description' =&gt; t('Date 1 description'), 
);


Drupal core doesn't have a date popup FAPI element, but you can add jQuery UI datepicker to a regular text field as well

$form['date1'] = array(
  '#type' =&gt; 'textfield', 
  '#title' =&gt; t('Date 1'), 
  '#description' =&gt; t('Date 1 description'), 
  '#prefix' =&gt; '&lt;div class="datepicker-wrapper"&gt;',
  '#suffix' =&gt; '&lt;/div&gt;'
);

// Attach JS datepicker library and init js for textfield
$form['#attached']['library'][] = array('system', 'ui.datepicker');
$form['#attached']['js'] = array(
  drupal_get_path('theme', 'my_theme') . '/js/datepicker-init.js',
);


Then in your theme /js/datepicker-init.js

(function ($) {
  // Add calendar popup to date textfield
  Drupal.behaviors.setDatePicker = {
    attach: function(context, settings) {
      var options = {
        'dateFormat':'yy-mm-dd',
        'changeYear':true,
        'changeMonth':true
       };
      $(".datepicker-wrapper input").once('datepicker').datepicker(options);
    }
  };

})(jQuery);


The result is something like this: