Drupal:如何在表格的相同页面上渲染表单的结果 [英] Drupal: How to Render Results of Form on Same Page as Form

查看:111
本文介绍了Drupal:如何在表格的相同页面上渲染表单的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



相关的hook_menu:


$ b如何在表单本身的同一页面上打印表单提交的结果? $ b

  $ items ['admin / content / ncbi_subsites / paths'] = array(
'title'=>'Paths',
'description '=>'特定子网站的路径',
'页面回调'=>'ncbi_subsites_show_path_page',
'访问参数'=>数组('管理站点配置'),
'type'=> MENU_LOCAL_TASK,
);

页面回调:

 函数ncbi_subsites_show_path_page(){
$ f = drupal_get_form('_ nc​​bi_subsites_show_paths_form');
return $ f;
}

表单建立功能:

 函数_ncbi_subsites_show_paths_form(){
//这里的一堆代码

$ form ['subsite'] = array(
' #title'=> t('Subsites'),
'#type'=>'select',
'#description'=>'选择子网站以获取路径',
'#default_value'=>'选择子网站',
'#options'=> $ tmp,
);

$ form ['showthem'] = array(
'#type'=>'submit',
'#value'=>'显示路径',
'#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); //我在本模块内部使用的类
$ paths = $ subsite-> normalized_pa​​ths;

//构建列表
$ list = theme_item_list($ paths);
}

如果我打印$ list变量,那就是我想要的,但是我不知道如何使用从ncbi_subsites_show_path_page构建的原始表单页面进入页面。任何帮助非常感谢!

解决方案

Nikit发布的链接中的关键信息是$ form_state ['rebuild']。以下是Drupal 7文档中的一些信息,我相信对于Drupal 6应用相同的方法。


$ form_state ['rebuild']:通常整个
表单处理完成后,
提交处理程序运行,一个表单是
,被认为是要完成的,
drupal_redirect_form()将把
重定向到一个新页面使用GET
请求(因此浏览器刷新不会
重新提交表单)。但是,如果
'rebuild'设置为TRUE,则
新的表单副本立即被
内置并发送到浏览器;而是
的重定向。这用于
多步骤表单,例如向导和
确认表单。另外,如果一个表单
验证处理程序将'rebuild'
设置为TRUE,并且发生验证错误
,那么该表单在被返回之前被重新生成
,使能窗体
要更改的元素,适当的
到特定的验证错误。



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!

解决方案

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...

$form_state['rebuild']: Normally, after the entire form processing is completed and submit handlers ran, a form is considered to be done and drupal_redirect_form() will redirect the user to a new page using a GET request (so a browser refresh does not re-submit the form). However, if 'rebuild' has been set to TRUE, then a new copy of the form is immediately built and sent to the browser; instead of a redirect. This is used for multi-step forms, such as wizards and confirmation forms. Also, if a form validation handler has set 'rebuild' to TRUE and a validation error occurred, then the form is rebuilt prior to being returned, enabling form elements to be altered, as appropriate to the particular validation error.

这篇关于Drupal:如何在表格的相同页面上渲染表单的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆