In Drupal 7, I have a preprocess function retrieving a form like so:

$contact = drupal_get_form('privatemsg_new');

$contact is then a fully populated form array with the right fields and elements. After that, I tried drupal_render and render, but neither one show a themed output in the node. What do I have to do to display the form?


The correct answer is to use drupal_render, which is used for render arrays like the form is. I tested this with a different form, and got the desired result so what you're doing should be right. Most likely you have a different error some where in your code causing this.

$html = drupal_render(drupal_get_form('form_id', $args)));
  • Doh! I just realized I am in a preprocess function, so it should have been $vars['contact'] with the render. Thanks for helping. – Kevin Jul 29 '11 at 17:30
  • if use above method, 'form' tag is not rendered – Guru Jul 22 '15 at 12:14
  • @Guru If the form tag is not rendered, then you have a different issue. I've done this many times and it have worked fine every time. – googletorp Jul 23 '15 at 12:26
  • Thanks @googletorp it is working fine. I've also faced another problem. ie, '#ajax' is not triggered when i rendered form by above method. Is any other solutions for this? – Guru Jul 28 '15 at 4:44

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.