使用 drupal_get_form() 传递参数
这是我使用钩子的自定义模块,
假设如果我想将参数传递给 custom1_default_form 函数调用,我应该如何传递参数?
<?php
function custom1_block($op,$delta=0){
if($op=='list'){
$block = array();
$block[0]['info']=t('hello world');
return $block;
}else if($op=='view'){
$block_content = '<p>THIS IS MY FIRST BLOCK</p>';
$block['subject'] = 'HELLO WORLD';
$block['content'] =drupal_get_form('custom1_default_form');
return $block;
}
}
function custom1_default_form () {
$form = array();
$form['nusoap_urls']['txt_name'] =
array('#type' => 'textfield',
'#title' => t('Please enter your name'),
'#default_value' => variable_get('webservice_user_url',''),
'#maxlength' => '40',
'#size' => '20',
// '#description' => t('<br />Root directory used to present the filebrowser user interface.')
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Details'),
);
return $form;
}
function custom1_default_form_validate (&$form, &$form_state) {
if(($form_state['values']['txt_name']) == '') {
form_set_error('user_webservice', t('Enter a name'));
}
}
function custom1_default_form_submit ($form_id, $form_values) {
// drupal_set_message( print_r($_POST));
// $message = 'You have submitted the ' . $form_id . ' form which contains the following data:<pre>' . print_r($form_state['values'],true) . '</pre>';
//drupal_set_message(t($message));
//drupal_set_message(t($form_values['values']['txt_name']));
// print_r($form_values['values']);
$GET_TXT_FIELD_VALUE = $form_values['values']['txt_name'];
$INSERT_QUERY = "INSERT INTO sample (test_name) VALUES ('$GET_TXT_FIELD_VALUE')";
if (db_result(db_query("SELECT COUNT(*) FROM {sample} WHERE test_name = '%s';", $GET_TXT_FIELD_VALUE))) {
// User doesn't exist
drupal_set_message(t('ALREADY EXIST.....'));
}else{
db_query($INSERT_QUERY)or die('Execution Failed');
if(db_affected_rows()==1){
drupal_set_message(t('VALUE INSERTED SUCCESSFULLY'));
}else{
drupal_set_message(t('VALUE INSERTED FAILED'));
}
}
}
Here is my custom module using hook,
Assume if I want to pass argument to custom1_default_form function call, how should i pass the argument?
<?php
function custom1_block($op,$delta=0){
if($op=='list'){
$block = array();
$block[0]['info']=t('hello world');
return $block;
}else if($op=='view'){
$block_content = '<p>THIS IS MY FIRST BLOCK</p>';
$block['subject'] = 'HELLO WORLD';
$block['content'] =drupal_get_form('custom1_default_form');
return $block;
}
}
function custom1_default_form () {
$form = array();
$form['nusoap_urls']['txt_name'] =
array('#type' => 'textfield',
'#title' => t('Please enter your name'),
'#default_value' => variable_get('webservice_user_url',''),
'#maxlength' => '40',
'#size' => '20',
// '#description' => t('<br />Root directory used to present the filebrowser user interface.')
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Details'),
);
return $form;
}
function custom1_default_form_validate (&$form, &$form_state) {
if(($form_state['values']['txt_name']) == '') {
form_set_error('user_webservice', t('Enter a name'));
}
}
function custom1_default_form_submit ($form_id, $form_values) {
// drupal_set_message( print_r($_POST));
// $message = 'You have submitted the ' . $form_id . ' form which contains the following data:<pre>' . print_r($form_state['values'],true) . '</pre>';
//drupal_set_message(t($message));
//drupal_set_message(t($form_values['values']['txt_name']));
// print_r($form_values['values']);
$GET_TXT_FIELD_VALUE = $form_values['values']['txt_name'];
$INSERT_QUERY = "INSERT INTO sample (test_name) VALUES ('$GET_TXT_FIELD_VALUE')";
if (db_result(db_query("SELECT COUNT(*) FROM {sample} WHERE test_name = '%s';", $GET_TXT_FIELD_VALUE))) {
// User doesn't exist
drupal_set_message(t('ALREADY EXIST.....'));
}else{
db_query($INSERT_QUERY)or die('Execution Failed');
if(db_affected_rows()==1){
drupal_set_message(t('VALUE INSERTED SUCCESSFULLY'));
}else{
drupal_set_message(t('VALUE INSERTED FAILED'));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想通过 URL 传递参数,请使用
arg()
< /a>:如果您只想通过
drupal_get_form()
调用将参数传递给表单,只需将参数作为附加参数添加到drupal_get_form()
:If you want to pass an argument via the URL, use
arg()
:If you just want to pass an argument to the form via the
drupal_get_form()
call, just add the arguments as additional parameters todrupal_get_form()
:我发现在 Drupal 6.20 中,您应该在回调函数定义中添加一个虚拟参数:
$block['content'] = drupal_get_form('custom1_default_form', $arg1, $arg2);
// ...
function custom1_default_form($dummy, $arg1, $arg2) { // 查看 $dummy 中存储的内容
// ...
}
I have found that in Drupal 6.20 you should add a dummy argument to the callback function definition:
$block['content'] = drupal_get_form('custom1_default_form', $arg1, $arg2);
// ...
function custom1_default_form($dummy, $arg1, $arg2) { // look at what gets stored in $dummy
// ...
}
尽可能避免使用 arg() 函数:
avoid the using of arg() functions when possible: