drupal Form

1, 首先在 hook_menu 中定义path, 例如下面定义的path为user/register。
$items['user/register'] = array(
'title' => 'Create new account',
'page callback' => 'drupal_get_form', //一般都是使用'drupal_get_form'函数
'page arguments' => array('register_user_form'), //'register_user_form'是指构造$form数组的函数,同时该字符串还会作为form id。
'access callback' => 'user_register_access',
);

2,定义'register_user_form'函数


function register_user_form()
{
$period = drupal_map_assoc(array(3600,10800,21600,32400,43200,86400),'format_interval');
$form['email'] = array('#type' => 'textfield','#size' => '45', '#default_value'=>'jerry jia cecy');
$form['fullname'] = array('#type' => 'textfield','#size' => '32');
$form['month'] = array('#type' => 'select','#options' => $period);
$form['day'] = array('#type' => 'select','#options' => $period);
$form['year'] = array('#type' => 'select','#options' => $period);
//$form['mydiv'] = array('#type' => 'markup', '#value' => $_table);
$form['submit'] = array('#type' => 'submit', '#value' => t('Save form'));

$form['#theme'] = 'simplygreen_register_user_form'; // 'simplygreen_register_user_form'是关键,和hook_theme()中的
$form['#submit'] = array('register_user_form_submit');

return $form;
}


3, 使用hook_theme() 注册模板文件

function user_theme() {

return array(
'user_admin_new_role' => array(
'arguments' => array('form' => NULL),
'file' => 'user.admin.inc',
),
'user_admin_account' => array(
'arguments' => array('form' => NULL),
'file' => 'user.admin.inc',
),
'user_filter_form' => array(
'arguments' => array('form' => NULL),
'file' => 'user.admin.inc',
),
'user_filters' => array(
'arguments' => array('form' => NULL),
'file' => 'user.admin.inc',
),
'user_signature' => array(
'arguments' => array('signature' => NULL),
),
'simplygreen_register_user_form' => array(
'arguments' => array('form' => NULL), //form很关键,在模板文件中使用的变量有该字符串决定。form => $form
'template' => 'theme_register_user_form', //指定模板tpl.php文件的文件名,不带扩展名。该文件应放在模块所在的目录下面。
),
);

}


4,定义simplygreen_register_user_form.tpl.php文件(位于modules\user文件夹下)

<table cellspacing="4" cellpadding="4" border="0">
<tbody><tr>
<td align="right"><nobr>Email Address2:</nobr> </td>
<td width="100" colspan="2">
<?php print drupal_render($form['email']); ?>
</td>
</tr>
<tr>
<td align="right"><nobr>Full Name2:</nobr> </td>
<td width="100" colspan="2">
<?php print drupal_render($form['fullname']); ?>
</td>
</tr>

<tr>
<td align="right"><nobr>Date of Birth:</nobr> </td>
<td colspan="2">
<?php print drupal_render($form['month']); ?>
<?php print drupal_render($form['day']); ?>
<?php print drupal_render($form['year']); ?>
</td>
</tr>
</tbody></table>



当用户访问base + user/register网址时,由'page callback'指向的函数来生成页面表单。 这里是drupal_get_form().

传给drupal_get_form的参数是'page arguments'指向的数组array('register_user_form')。

drupal_get_form()函数

posted on 2009-11-26 18:29  jerry data  阅读(354)  评论(0编辑  收藏  举报