2010-10-08 144 views
7

這裏是我的自定義模塊使用鉤子,使用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')); 
     } 
    }  
} 

回答

11

如果你想通過URL來傳遞參數,使用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' 
    // ... 
} 

如果你只是想通過drupal_get_form()調用傳遞一個參數的形式,只需添加參數作爲附加參數drupal_get_form()

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

// ... 

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

我發現,在Drupal 6.20你應該添加一個虛擬參數回調函數的定義:

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

// ...

功能custom1_default_form($假,$ ARG1,ARG2 $){//看看是什麼獲取存儲在$虛擬 // ... }

1

避免在可能的情況下使用arg()函數:

在可能的情況下避免使用此函數,因爲結果代碼難以讀取 。在菜單回調函數中,嘗試使用命名參數。 請參閱menu.inc中的說明以瞭解如何構造 接受參數的回調。當試圖使用該功能從當前路徑加載一個 元素時,例如加載節點頁面上的節點, 改爲使用menu_get_object()。