我将如何在与表单本身相同的页面上打印表单提交的结果?
相关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'构建的原始表单页面的页面。非常感谢您的帮助!
Nikit发布的链接中的关键信息是$ form_state ['rebuild']。这是Drupal 7文档中的一些信息,我认为这些信息也适用于Drupal 6 ...
$ form_state ['rebuild']:通常,在整个 表格处理完成并 提交处理程序运行,表格是 考虑完成 drupal_redirect_form()将重定向 用户使用GET进入新页面 请求(因此浏览器刷新不会 重新提交表格)。但是,如果 'rebuild'已设置为TRUE,然后 表格的新副本会立即显示 构建并发送到浏览器;代替 重定向。这用于 多步骤表单,例如向导和 确认表格。另外,如果是表格 验证处理程序已设置“重建” 为TRUE和验证错误 发生,然后重建表格 返回之前,启用表格 适当更改的元素 特定的验证错误。
这是页面和同一页面上的列表的完整工作示例
<?php
/*
* Implements hook_mennu()
*/
function test_menu() {
$items['test'] = array(
'title' => t('Test'),
'page callback' => 'test_search_page',
'access callback' => True,
);
return $items;
}
function test_search_page(){
$form = drupal_get_form('test_search_form');
return $form;
}
function test_search_form($form, &$form_state){
$header = array(t('id'), t('name'), t('firstname'));
$rows = Null;
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#required' => True,
'#default_value' => isset($_GET['name']) ? $_GET['name'] : Null
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('submit'),
);
if (isset($_GET['name'])){
$rows = get_data();
}
$form['table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('Aucun résultat.')
);
$form['pager'] = array('#markup' => theme('pager'));
/*
if (isset($form_state['table'])) {
$form['table'] = $form_state['table'];
}
$form['pager'] = array('#markup' => theme('pager'));
*/
return $form;
}
function test_search_form_submit($form, &$form_state){
$form_state['redirect'] = array(
// $path
'test',
// $options
array('query' => array('name' => $form_state['values']['name'])),
// $http_response_code
302,
);
}
//$header = array(t('id'), t('name'), t('firstname'));
function get_data(){
$data = array(
0 => array(
'id' => '0',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
1 => array(
'id' => '1',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
2 => array(
'id' => '2',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
3 => array(
'id' => '3',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
4 => array(
'id' => '4',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
5 => array(
'id' => '5',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
6 => array(
'id' => '6',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
7 => array(
'id' => '7',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
8 => array(
'id' => '8',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
9 => array(
'id' => '9',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
10 => array(
'id' => '10',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
),
11 => array(
'id' => '11',
'name' => 'pokpokpok',
'firstname' => 'pokpokpok',
)
);
$paging = pager_array_splice($data, 2);
return $paging;
}
/*
$header = array(t('id'), t('name'), t('firstname'));
$form_state['table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $paging,
'#empty' => t('Aucun r?sultat.')
);
$form_state['rebuild'] = True;*/
function pager_array_splice($data, $limit = 9, $element = 0) {
global $pager_page_array, $pager_total, $pager_total_items;
$page = isset($_GET['page']) ? $_GET['page'] : '';
// Convert comma-separated $page to an array, used by other functions.
$pager_page_array = explode(',', $page);
// We calculate the total of pages as ceil(items / limit).
$pager_total_items[$element] = count($data);
$pager_total[$element] = ceil($pager_total_items[$element] / $limit);
$pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
return array_slice($data, $pager_page_array[$element] * $limit, $limit, TRUE);
}
Drupal6 node.module和dblog.module通过提供页面回调在其输出中包含呈现的表单来为admin / content / node和admin / reports / dblog进行此操作。
modules/dblog/dblog.admin.inc
dblog_overview()
modules/node/node.admin.inc
node_admin_nodes()
在表单提交中,更新的过滤器设置存储在$ _SESSION中。
在页面回调中,它基于$ _SESSION中存储的过滤器设置来呈现结果。
$$ SESSION只是这里的另一个全局变量(尽管是一个持久变量)。
对于Drupal7,我发现如果您使用$form_state['rebuild']
,那么最好从PHP超全局变量$_POST
(或$_REQUEST
)访问表单变量。但是,如果使用$form_state['redirect']
,则使用$_SESSION
的解决方案更好(而不是使用$_GET
或$_REQUEST
)。
即使对于专家,我也发现这个问题非常棘手。也许Drupal有一些我们不知道的更简单直观的方法。
对于Drupal 8,如果您有一个实现FormBase
的表单,我发现我需要设置要重新构建的表单,以允许在成功提交表单后呈现表单期间使用表单状态:
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state->setRebuild(TRUE);
}
默认情况下,表单将提交并处理该表单,然后重定向,然后再次构建该表单,此时您将没有表单状态(除非您已经将表单状态作为查询参数的一部分传递给了重定向)。