2010-10-08 13 views
7

Dưới đây là mô-đun tùy chỉnh của tôi sử dụng móc,Đi qua lập luận sử dụng drupal_get_form()

Giả sử nếu tôi muốn vượt qua lập luận để custom1_default_form chức năng gọi điện, làm thế nào tôi nên vượt qua đối số?

<?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')); 
     } 
    }  
} 

Trả lời

11

Nếu bạn muốn vượt qua một cuộc tranh cãi thông qua URL, sử dụng 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' 
    // ... 
} 

Nếu bạn chỉ muốn vượt qua một cuộc tranh cãi với hình thức thông qua drupal_get_form() cuộc gọi, chỉ cần thêm các đối số như thêm tham số đến drupal_get_form():

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

// ... 

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

Tôi đã tìm thấy rằng trong Drupal 6.20 bạn nên thêm đối số giả vào định nghĩa hàm gọi lại:

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

// ...

chức năng custom1_default_form ($ giả, $ arg1, arg2 $) {// nhìn vào những gì được lưu trữ trong $ giả // ... }

1

tránh sử dụng các hàm arg() khi có thể:

Tránh sử dụng chức năng này nếu có thể, do đó mã khó là để đọc. Trong các hàm gọi lại của menu, hãy cố sử dụng các đối số được đặt tên. Xem phần giải thích trong menu.inc để biết cách xây dựng các cuộc gọi lại lấy các đối số. Khi cố gắng sử dụng hàm này để tải phần tử từ đường dẫn hiện tại, ví dụ: tải nút trên trang nút, sử dụng menu_get_object() thay thế.