0

I'm making a custom form and I would like to render a field that is just like the form that gets rendered for a Date with a jQuery Popup when editing a node.

enter image description here

I can't figure out how to build the array so that it will get rendered by drupal properly. I've tried a ton of different attempts, but this is what I've got currently:

$form['birthdate'] = array(
    '#title' => t("Offspring Birth Date"),
    '#id' => 'edit-field-filter-birthdate',
    '#type' => 'date_popup',
    '#default_value' => '2015-01-01 10:30:00',
    '#date_type' => DATE_DATETIME,
    '#date_timezone' => date_default_timezone(),
    '#date_format' => 'm-d-Y H:i',
    '#date_increment' => 1,
    '#date_year_range' => '-3:+3',
);

$html = drupal_render($form);

The README.txt in the date/date_popup module says:

To include a popup calendar in a form, use the type 'date_popup':

$form['date'] = array( '#type' => 'date_popup': '#title => t('My Date'), .... );

Set the #type to date_popup and fill the element #default_value with a date adjusted to the proper local timezone, or leave it blank.

However, all that ever gets rendered is just the '#title'.

What am I doing wrong?

  • And the rest of the form elements render fine? – montaigne Mar 13 '15 at 2:45
  • The rest of the function this is in generates the html directly (not using a render array). It wasn't written by me, just dealing with existing code. I'm trying to build an array just for this field and have drupal render it to html inside my function. Then I'll include that html with the rest of the html returned from this module function. That being said, yes, the rest of the page renders fine.. but it's not using a render array. – Kenny Wyland Mar 13 '15 at 2:54
1

You need to initialize the form with drupal_get_form():

$form = drupal_get_form('MYMODULE_birthdate_form');
$html = drupal_render($form);

function MYMODULE_birthdate_form($form, &$form_state) {
  $form['birthdate'] = array(
    '#type' => 'date_popup',
    // ...
  );
  return $form
}
  • Perfect! You're the king. – Kenny Wyland Mar 13 '15 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.