I've got a strange problem. I've been working with the Drupal 7 form API which was going swimmingly. For some reason or other I cleared the Drupal cache, and now the form simply says 'array' - it was working fine before. No matter what I change I cannot seem to make any form using the API display. I have used the code used in the Drupal Form API quickstart guide, which still says 'array'. I am calling it using print render(drupal_get_form('my_form')), but I have also tried drupal_render. If I print_r the drupal_get_form part it shows the array, so the form is definately there. I'm at a loss, please tell what is wrong!

function urban_form($form, &$form_state) {

  $form = array();
  $form['customization'] = array(
      '#type' => 'fieldset',
      '#title' => 'ur:customization',
      '#collapsible' => TRUE
  );
  $form['customization']['check_in'] = array(
    '#title' => t("Check in"),
    '#type' => 'select',
    '#options' => array(
      'one' => 'Option 1',
      'two' => 'Option 2',
      'three' => 'Option 3',
    )
  );
  $form['customization']['length'] = array(
    '#title' => t("Please select length of stay"),
    '#type' => 'select',
    '#options' => array(
      'one' => 'Option 1',
      'two' => 'Option 2',
      'three' => 'Option 3',
    )
  );


  return $form;
}

print drupal_render(drupal_get_form('urban_form'));

Comments

John Carbone’s picture

I don't see anything immediately wrong with your form array or the calls, but there's a chance that there's some funky namespace collisions in there. If your module is called urban then a function called urban_form will get picked up by hook_form which is used by node modules. Check out http://api.drupal.org/api/drupal/modules%21node%21node.api.php/function/... I don't know that that's definitely the cause of your problem but nothing else really comes to mind. What happens if you rename your function to something like "urban_form_builder" or something like that? Anything?

Also, make sure you've flushed your caches a couple of times. It could just be a caching issue. Hope this helps.

mrchristoph’s picture

Before I read your reply I managed to fix it by changing the name of the form, so you you were right! Strange that it was working before I cleared the cache though...

rpjanaka’s picture

For me it was ... I have missed the 'drupal_render' :)

elchubakabre’s picture

I saw this problem too.

function same_func() {
  return '<div id="same-id">' . drupal_get_form('user_login_block') . '</div>';
}

this code does't work.

John Carbone’s picture

I'm not sure what context you're using this in but try changing

return '<div id="same-id">' . drupal_get_form('user_login_block') . '</div>';

to:

return '<div id="same-id">' . drupal_render(drupal_get_form('user_login_block')) . '</div>';

and see if that helps any. In D7 drupal_get_form returns a renderable array, not HTML.

nehajn’s picture

''.drupal_render(drupal_get_form('test_form',$ap_code)).'';

In D7 drupal_get_form returns a renderable array, not HTML.

espurnes’s picture

I don't know why but

drupal_render(drupal_get_form('form_id'));

does not work in my case to render a custom form in a block or node using PHP code text format.

Instead it works the following code

print render(drupal_get_form('form_id'));