使用 drupal_get_form() 传递参数

发布于 2024-09-26 18:26:03 字数 2342 浏览 0 评论 0原文

这是我使用钩子的自定义模块,

假设如果我想将参数传递给 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

饮湿 2024-10-03 18:26:03

如果您想通过 URL 传递参数,请使用 arg()< /a>:

function custom1_default_form() {
  // Assuming the URL is http://example.com/admin/content/types:
  $arg1 = arg(1); // $arg1 = 'content'
  $arg2 = arg(2); // $arg2 = 'types'
  // ...
}

如果您只想通过 drupal_get_form() 调用将参数传递给表单,只需将参数作为附加参数添加到 drupal_get_form()

$block['content'] = drupal_get_form('custom1_default_form', $arg1, $arg2);

// ...

function custom1_default_form($form_state, $arg1, $arg2) {
  // ...
}

If you want to pass an argument via the URL, use arg():

function custom1_default_form() {
  // Assuming the URL is http://example.com/admin/content/types:
  $arg1 = arg(1); // $arg1 = 'content'
  $arg2 = arg(2); // $arg2 = 'types'
  // ...
}

If you just want to pass an argument to the form via the drupal_get_form() call, just add the arguments as additional parameters to drupal_get_form():

$block['content'] = drupal_get_form('custom1_default_form', $arg1, $arg2);

// ...

function custom1_default_form($form_state, $arg1, $arg2) {
  // ...
}
烦人精 2024-10-03 18:26:03

我发现在 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
// ...
}

风柔一江水 2024-10-03 18:26:03

尽可能避免使用 arg() 函数:

尽可能避免使用此函数,因为生成的代码很难
阅读。在菜单回调函数中,尝试使用命名参数。
有关如何构造回调的信息,请参阅 menu.inc 中的说明
采取论点。当尝试使用此函数加载
当前路径中的元素,例如在节点页面上加载节点,
请改用 menu_get_object()。

avoid the using of arg() functions when possible:

Avoid use of this function where possible, as resulting code is hard
to read. In menu callback functions, attempt to use named arguments.
See the explanation in menu.inc for how to construct callbacks that
take arguments. When attempting to use this function to load an
element from the current path, e.g. loading the node on a node page,
use menu_get_object() instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文