我正在为我的组织编写一个模块,将XML提要缓存到静态文件到我们的our服务器上的任意位置。我是Drupal开发公司的新手,我想知道我是否以正确的方式来处理这个问题。
基本上我:
这是菜单钩子:
function ncbi_cache_files_menu() {
$items = array();
$items['admin/content/ncbi_cache_files'] = array(
'title' => 'NCBI Cache File Module',
'description' => 'Cache Guide static content to files',
'page callback' => 'drupal_get_form',
'page arguments' => array( 'ncbi_cache_files_show_submit'),
'access arguments' => array( 'administer site configuration' ),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
我生成的表单如下:
function ncbi_cache_files_show_submit() {
$DEFAULT_OUT = 'http://myorg/foo';
$form[ 'ncbi_cache_files' ] = array(
'#type' => 'textfield',
'#title' => t('Output Directory'),
'#description' => t('Where you want the static files to be dumped.
This should be a directory that www has write access to, and
should be accessible from the foo server'),
'#default_value' => t( $DEFAULT_OUT ),
'#size' => strlen( $DEFAULT_OUT ) + 5,
);
$form['dump'] = array(
'#type' => 'submit',
'#value' => 'Dump',
'#submit' => array( 'ncbi_cache_files_dump'),
);
return system_settings_form( $form );
}
然后,功能在回调中:
function ncbi_cache_files_dump( $p, $q) {
//dpm( get_defined_vars() );
$outdir = $p['ncbi_cache_files']['#post']['ncbi_cache_files'];
drupal_set_message('outdir: ' . $outdir );
}
问题是:在Drupal中,这是一种处理任意形式的好方法吗?我真的不需要听任何drupal钩子,因为我基本上只是做一些URL和文件处理。
我在回调($q)中得到的论点是什么?这就是我猜的带有post值的表单数组吗?这是获取表单参数的最佳方法吗?
谢谢你的建议。
发布于 2010-04-20 10:20:05
您可以分两个阶段处理表单,验证和提交。
当您想要验证某个用户提供的用户时,如果某个用户输入无效(比如无效的url或电子邮件地址),则会引发表单错误。
如果表单通过了所有的验证,就会调用使用的Submit,因此如果您进行了适当的验证,您就会知道用户提供的数据是可以的。
您的提交函数应该如下所示:
function ncbi_cache_files_dump(&$form, &$form_state) {
// $form: an array containing the form data
// $form_state: data about the form, like the data inputted in the form etc.
// code...
}
发布于 2010-04-20 10:55:50
我想你需要两份不同的表格
另外,将先前保存的路径发布为设置表单中的默认值(而不是硬编码路径)似乎是合乎逻辑的。
通常,您应该检查submit回调的第二个参数中的表单输入数据:
function ncbi_cache_files_dump(&$form, &$form_state) {
$outdir = $form_state['values']['ncbi_cache_files'];
// ...
}
https://stackoverflow.com/questions/2670443
复制相似问题