0

It seems simple enough to create a form in Drupal 7 with the Form API, however, everything I find shows that you have to use hook_menu() to add a page and then generate the form that way.

I would like to create a standalone form, then just display the form using drupal_get_form() and drupal_render() to render the form.

How do I create a custom form without creating it's own page?

  • Have you tried to simply omit the hook_menu() step? How it failed you? – Mołot Feb 4 '14 at 10:36
5

It's very simple...add a function to your module:

function MYMODULE_some_form($form, &$form_state) {
  // Build and return your form.
}

Call drupal_get_form() from whatever context makes sense to your use case, and render the result:

$form = drupal_get_form('MYMODULE_some_form');
$rendered_form = drupal_render($form);

Then you can use $rendered_form as the output wherever it makes sense to do so (usually a template file).

  • Fantastic, thanks! I knew it would be quite simple just couldn't find a good example. Cheers. – VanD Feb 4 '14 at 10:45

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.