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....我正在尝试将用户注册表单加载到 Drupal 7 安装中的首页(用于一些很酷的 jQuery 内容)。在 Drupal 6 中,代码就这么简单......

<?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.)然而,在 Drupal 7 中,它打印出表单的数组,虽然我尝试在其上使用 render() 和其他尝试,但它不会打印该表单。 D7 API 文档并没有真正的帮助,因为它没有显示任何示例(与 PHP / jQuery 文档不同),并且链接的表单生成文档在这里并不真正适用,因为我没有尝试在模块中执行此操作。此外,由于 D7 还没有很好的文档记录,Google 也没有提供太多帮助。 (等不及 D7 Pro 开发书上市了。)

Does anyone know how to make this work in Drupal 7?有谁知道如何在 Drupal 7 中实现此功能?

Comments 评论 

WebMaster’s picture

Came across something interesting... http://coder1.com/articles/drupal-6-to-7-first-glimpse遇到一些有趣的事情......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...不幸的是,我也在尝试  print drupal_render(drupal_get_form('user_register'));  作为 page.tpl.php 上建议的页面,但这也不起作用。没看懂啊...

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).我应该补充一点,这只是针对注册表单,输入任何表单 ID 都应该可以与上面的代码配合使用(D6/D7 重命名让我失望)。

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):嗯,那么如果我把它放在一个块中,为什么它不起作用(适用于用户注册,但不适用于 fot 页面):

 print drupal_render(drupal_get_form('page_node_form')); 

Get lots of php errors..得到很多 php 错误..

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/...http://api.drupal.org/api/drupal/modules--node--node.pages.inc/function/...

leymannx’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?如何使用 php 在节点中显示 profile2 表单?我在 profile2 注册表中使用什么表单 ID?

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这有效。但对我来说,它抛出警告严格警告:只有变量应该在 include() 中通过引用传递(/var/www/foo/sites/all/themes/foo/templates/page.tpl.php 的第 45 行)。任何想法是什么造成的?干杯,凯文

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.使用 D7 配置页面、查询表格、在表格中显示结果的迷你教程,其中包含分页器、drupal_get_form 和可渲染数组,所有这些都汇总到一个整洁的小包中。

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.你是我的英雄,我一直在寻找如何将 drupal_get_form 与其他标记一起放置在页面渲染数组中,而你的解决方案就是这样做的。谢谢。

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: 如果您正在使用表单验证,并且直接在模板中打印表单,则这将不起作用。要使验证生效,请将表单分配给预处理函数中的变量,然后在模板中打印该变量(仅应参见 Jaypan 在 form_set_error() 处的评论)。例如,在 template.php 文件中,您可以输入:

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: 然后在您的模板中,例如 page.tpl.php 您可以输入:

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

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