I'm trying to load the user registration form into the front page (for some cool jQuery stuff) in a Drupal 7 installation. In Drupal 6, the code was as simple as this....

<?php
    print drupal_get_form('user_register');
?>

However, in Drupal 7 it prints out an array of the form, and while I tried using render() on it and other attempts, it won't print that form out. The D7 API docs aren't real helpful as it shows no examples (unlike PHP / jQuery docs), and the linked form generation document doesn't really apply here as I'm not trying to do this in a module. Also as D7 is not well documented yet, Google hasn't been much help either. (Can't wait until the D7 Pro Development book is available.)

Does anyone know how to make this work in Drupal 7?

Comments

WebMaster’s picture

Came across something interesting... http://coder1.com/articles/drupal-6-to-7-first-glimpse

Unfortunately, I am also trying print drupal_render(drupal_get_form('user_register')); as that page suggested on the page.tpl.php and that is not working as well. Don't understand it...

WebMaster’s picture

Oh, for crying out loud, I sometimes hate change for the sake of change. Solved it, use this...

<?php 
    print drupal_render(drupal_get_form('user_register_form')); 
?>

I should add that's just for the registration form, putting in any form ID should work fine with the above code (the D6/D7 rename threw me off).

Wappie08’s picture

hmm, then why doesn't this work if I put it in a block (works for user registration but not fot page):

 print drupal_render(drupal_get_form('page_node_form')); 

Get lots of php errors..

Greets Wappie

bhupendra_kanojiya’s picture

Use this, it works

print drupal_render(node_add('form_name'));

also look
http://api.drupal.org/api/drupal/modules--node--node.pages.inc/function/...

norman.lol’s picture

$form = drupal_get_form('user_register_form');
print drupal_render($form);
johnlaine’s picture

Works perfectly, Thanks!

muhammad.tanweer’s picture

This way works.

Thanks WebMaster.

luthien’s picture

how do you display a profile2 form in a node using php? what form id do I use for profile2 registration forms?

pyxio’s picture

This works. But for me it is throwing a warning Strict warning: Only variables should be passed by reference in include() (line 45 of /var/www/foo/sites/all/themes/foo/templates/page.tpl.php). any ideas what is causing that? cheers, kevin

markpape’s picture

Hi Kevin, If you separate the functions into two lines, as he did

$form = drupal_get_form('user_register_form');
print drupal_render($form);
Krait’s picture

Mini tutorial to use D7 config page, query a table, display the results in a table, with a pager, drupal_get_form and renderable arrays all rolled up into one neat little package.

I wanted to weigh in on this, even though it is [SOLVED], because this page does show up high in google results.

My code may not be perfect, or the best way to do this, but hopefully it saves people some time.

I hacked out a lot of code specific to my project, but here is working(after some tweaks) code:

	$items['admin/config/mymodule'] = array(
		'title' => t('My Module'),
		'page callback' => 'system_admin_menu_block_page',
		'description' => 'Configure My Module.',
		'access arguments' => array('my permission'),
		'position' => 'right',
		'weight' => 10,
		'file' => 'system.admin.inc',
		'file path' => drupal_get_path('module', 'system'),
	);
	$items['admin/config/mymodule/cool_beans'] = array(
		'title' => 'Cool Beans',
		'description' => 'This are some Cool Beans.',
		'page callback' => 'mymodule_admin',
		'access arguments' => array('my permission'),
	);

function mymodule_admin() {
	$build = array();
	$rows = array();
	$header = array('Column One', 'Column Two', 'Column Three',);
	
	$query = db_select('mymodule_mytable')
		->extend('PagerDefault')
		->extend('TableSort');
	$query->fields('mymodule_mytable', array('field_1', 'field_2', 'field_3',))
		->condition('in_db', 0)
		->orderBy('field_1', 'ASC')
		->limit(10);
	$results = $query->execute();
	
	foreach ($results as $line) {
		$rows[] = array(
			$line->field_1,
			$line->field_2,
			$line->field_3,
		);
	}
	
	$variables = array('header' => $header, 'rows' => $rows);
	$content = theme('table', $variables, array());
	
	$build['content'] = array(
		'this_does_not_matter_too_much1' => array(
			'#markup' => $content,
		),
		'this_does_not_matter_too_much2' => drupal_get_form('mymodule_sample_form'),
	);
	
	$build['pager'] = array(
		'#theme' => 'pager',
		'#weight' => 5,
	);
  
  return $build;
}

function mymodule_sample_form($form, &$form_state) {
	$form = array();
	$form['submit'] = array (
		'#type' => 'submit',
		'#value' => 'Do Cool Things',
	);
	
	return $form;
}

function mymodule_sample_form_validate($form, &$form_state) {
}

function mymodule_sample_form_submit($form, &$form_state) {
}
naimul.k’s picture

You are my hero, I've been searching forever on how to place drupal_get_form within a page render array with other markup and your solution does just that. Thanks.

johnlaine’s picture

Works perfectly, Thanks!

muthusaravanan’s picture

But If we return the same rendered form instead of print, It will display as normal page with theme.

return drupal_get_form('form_name');
bmango’s picture

If you are using form validation this will not work if you are printing the form directly in the template. To get the validation to work, assign the form to a variable in a preprocess function and then print that variable out in the template (see Jaypan's comment at form_set_error() should only). So for example in your template.php file you can put: 

function my_theme_preprocess_page(&$variables) {
  $variables['code'] = drupal_get_form('form_id');
}

And then in your template, eg page.tpl.php you can then put: 

<?php print drupal_render($code); ?>
sano’s picture

@bmango, thanks. This info helped me a lot.

jkingsnorth’s picture

Argh! Yes! This solved a really weird problem I was having where the form was only having ajax applied and working for anonymous users, but not when logged in. I'm not sure how this fixes it - but it does...