Drupal:如何在与表单相同的页面上呈现表单结果
如何在与表单本身相同的页面上打印表单提交的结果?
相关的hook_menu:
$items['admin/content/ncbi_subsites/paths'] = array(
'title' => 'Paths',
'description' => 'Paths for a particular subsite',
'page callback' => 'ncbi_subsites_show_path_page',
'access arguments' => array( 'administer site configuration' ),
'type' => MENU_LOCAL_TASK,
);
页面回调:
function ncbi_subsites_show_path_page() {
$f = drupal_get_form('_ncbi_subsites_show_paths_form');
return $f;
}
表单构建功能:
function _ncbi_subsites_show_paths_form() {
// bunch of code here
$form['subsite'] = array(
'#title' => t('Subsites'),
'#type' => 'select',
'#description' => 'Choose a subsite to get its paths',
'#default_value' => 'Choose a subsite',
'#options'=> $tmp,
);
$form['showthem'] = array(
'#type' => 'submit',
'#value' => 'Show paths',
'#submit' => array( 'ncbi_subsites_show_paths_submit'),
);
return $form;
}
提交功能(为简洁起见,跳过验证功能)
function ncbi_subsites_show_paths_submit( &$form, &$form_state ) {
//dpm ( $form_state );
$subsite_name = $form_state['values']['subsite'];
$subsite = new Subsite( $subsite_name ); //y own class that I use internally in this module
$paths = $subsite->normalized_paths;
// build list
$list = theme_item_list( $paths );
}
如果我打印该$list变量,它正是我想要的,但我不确定如何将其放入原始表单的页面中从“ncbi_subsites_show_path_page”构建的页面。非常感谢任何帮助!
How would I print the results of a form submission on the same page as the form itself?
Relevant hook_menu:
$items['admin/content/ncbi_subsites/paths'] = array(
'title' => 'Paths',
'description' => 'Paths for a particular subsite',
'page callback' => 'ncbi_subsites_show_path_page',
'access arguments' => array( 'administer site configuration' ),
'type' => MENU_LOCAL_TASK,
);
page callback:
function ncbi_subsites_show_path_page() {
$f = drupal_get_form('_ncbi_subsites_show_paths_form');
return $f;
}
Form building function:
function _ncbi_subsites_show_paths_form() {
// bunch of code here
$form['subsite'] = array(
'#title' => t('Subsites'),
'#type' => 'select',
'#description' => 'Choose a subsite to get its paths',
'#default_value' => 'Choose a subsite',
'#options'=> $tmp,
);
$form['showthem'] = array(
'#type' => 'submit',
'#value' => 'Show paths',
'#submit' => array( 'ncbi_subsites_show_paths_submit'),
);
return $form;
}
Submit function (skipped validate function for brevity)
function ncbi_subsites_show_paths_submit( &$form, &$form_state ) {
//dpm ( $form_state );
$subsite_name = $form_state['values']['subsite'];
$subsite = new Subsite( $subsite_name ); //y own class that I use internally in this module
$paths = $subsite->normalized_paths;
// build list
$list = theme_item_list( $paths );
}
If I print that $list variable, it is exactly what I want, but I am not sure how to get it into the page with the original form page built from 'ncbi_subsites_show_path_page'. Any help is much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Nikit 发布的链接中的关键信息是 $form_state['rebuild']。以下是 Drupal 7 文档中的一些信息,我相信这些信息也适用于 Drupal 6...
The key information in the link Nikit posted is $form_state['rebuild']. Here's some info from Drupal 7 documentation that I believe applies the same for Drupal 6...
这是页面和同一页面上的列表的完整工作示例
This is a full working example of a page and a list on the same page
Drupal6的node.module和dblog.module通过提供一个页面回调来为admin/content/node和admin/reports/dblog做到这一点,该回调在其输出中包含呈现的表单。
在表单提交中,更新的过滤器设置存储在 $_SESSION 中。
在页面回调中,它根据 $_SESSION 中存储的过滤器设置呈现结果。
$_SESSION 只是这里的另一个全局变量(尽管是持久的)。
Drupal6 node.module and dblog.module do this for admin/content/node and admin/reports/dblog by providing a page callback which includes the rendered form in its output.
In form submit, updated filter settings are stored in $_SESSION.
In the page callback it renders the results based on the filter settings stored in $_SESSION.
$_SESSION is just another global here (albeit a persistent one).
对于 Drupal7,我发现如果您使用
$form_state['rebuild']
,那么可以最好从 PHP 超级全局变量$_POST
(或 <代码>$_REQUEST)。但是,如果您使用$form_state['redirect']
,则使用$_SESSION
的解决方案会更好(而不是使用$_GET
或$_REQUEST
)。我发现这个问题即使对于专家来说也很棘手。也许Drupal有一些我们不知道的更简单直观的方法。
For Drupal7 I find that if you use
$form_state['rebuild']
, then the form variables can be best accessed from the PHP super-global variable$_POST
(or$_REQUEST
). However, if you use$form_state['redirect']
, the solution with$_SESSION
is better (instead of using$_GET
or$_REQUEST
).I find this issue quite tricky even for experts. Maybe Drupal has some more easy and intuitive way that we don't know.
对于 Drupal 8,如果您有一个实现
FormBase
的表单,我发现我需要设置要重建的表单,以允许在成功提交表单后在渲染表单期间使用表单状态:默认情况下, form 将提交并处理表单,然后重定向,然后再次构建表单,此时您没有表单状态(除非您已将表单状态作为重定向中查询参数的一部分传递)。
For Drupal 8, if you have a form implementing
FormBase
I found I needed to set the form to be rebuilt to allow using the form state during render of the form after a successful form submission:By default, the form will submit and process the form, then redirect, then build the form again and at that point you don't have the form state (unless you've passed the form state as part of a query parameter in the redirect).