2012-07-21 66 views
4

我在網上搜索一個教程來創建一個頁面顯示的表單, 當我們使用模塊和塊顯示內容時,我們應該在模塊內顯示一個表單嗎? 由於我是drupal的新手,我對drupal表單並不瞭解。 我下載並安裝了示例表單模塊。但我不知道這個表單會顯示在哪裏。 我從這裏下載了 http://drupal.org/node/1121110如何在Drupal模塊中創建表單?

回答

11

即使你是drupal的新手,這並不複雜。我在這個例子中所要做的只是使用hook_menu()並且知道drupal form api reference的可用表單項。

下面是你要做的一個例子。

/** 
* Implementation of hook_menu() 
*/ 
function mymodule_menu() 
{ 
    $items = array(); 

    $items['my-custom-page-path'] = array(
     'title'    => 'My Page Title', 
     'description'  => t(''), 
     'access callback' => 'user_access', 
     'access arguments' => array('access content'), 
     'page callback'  => 'drupal_get_form', 
     'page arguments' => array('mymodule_form_id'), 
    ); 

    return $items; 
} 

function mymodule_form_id($form, &$form_state) 
{ 
    $form = array(); 

    $form['my_textfield'] = array(
     '#type'   => 'textfield', 
     '#title'  => t('Text Field'), 
     '#description' => t(''), 
     '#weight'  => 20, 
     '#required'  => TRUE, 
     '#size'   => 5, 
     '#maxlength' => 5, 
    ); 

    $form['submit'] = array(
     '#type'   => 'submit', 
     '#value'  => t('Save settings'), 
     '#weight'  => 10000, 
    ); 

    return $form; 
} 

/** 
* Form validation callback 
*/ 
function mymodule_form_id_validate($form, &$form_state) 
{ 
    // notice adding "_validate" to the form id 
} 

/** 
* Form submission callback 
*/ 
function mymodule_form_id_submit($form, &$form_state) 
{ 
    // notice adding "_submit" to the form id 
} 
3
#Here is the simple code for creating form in module# 

=============================================================== 

/*..firstly create a menu in module by copying this code..*/ 
function form_test_menu() { 
    $items['formtest'] = array(
         'title' => 'Form Test', 
         'page callback' => 'drupal_get_form', 
         'page arguments' => array('form_test_form'), 
         'access callback' => TRUE, 
         ); 
    return $items; 
} 

/*...Now create fields like below...*/ 
function form_test_form($form,&$form_submit) { 
    $form['firstname'] = array(
          '#title' => t('Firstname'), 
          '#type' => 'textfield', 
          '#required' => TRUE, 
          ); 
    $form['lastname'] = array(
          '#title' => t('Lastname'), 
          '#type' => 'textfield', 
          ); 
    $form['submit'] = array(
         '#value' => 'Submit', 
         '#type' => 'submit', 
         ); 
    return $form; 
}