//在一个地方调用表单 可以在模板处调用 也可以在模块处调用
<?php print drupal_get_form('food_order_form', $node); ?>
<?php
function food_order_form(&$form_state, $node)
{
$form['order'] = array(
'#type' => 'fieldset',
'#title' => '订餐:' . $node->title,
);
$form['order']['order_qty'] = array(
'#type' => 'textfield',
'#title' => '数量',
'#maxlength' => 3,
'#size' => 10,
'#default_value' => 1,
);
$form['order']['ship_address'] = array(
'#type' => 'textfield',
'#title' => '送餐地址',
);
$form['order']['ship_phone'] = array(
'#type' => 'textfield',
'#title' => '联系电话',
);
$form['order']['memo'] = array(
'#type' => 'textarea',
'#title' => '备注',
);
$form['order']['submit'] = array('#type' => 'submit', '#value' => '下订单');
$form['food_nid'] = array('#type' => 'value', '#value' => $node->nid);
return $form;
}
function food_order_form_validate($form, &$form_state) {
if (!is_numeric($form_state['values']['order_qty'])) { // 如果用户输入了非数字,则报错
form_set_error('order_qty', '请输入整数!');
}
if ($form_state['values']['order_qty'] <= 0)
{
form_set_error('order_qty', '订餐数量不能小于0!'); // 如果用户输入的数量小于或等于零,则报错
}
}
function food_order_form_submit($form, &$form_state) {
$node = (object)$node; //创建空节点对象
$node->uid = 1; // 创建订单者统一设为超级管理员
$node->type = 'food_order'; //节点类型为“订单”
$node->title = time(); // 订单号为当前时间戳
$node->body = $form_state['values']['memo']; // 记录客户的特殊要求
$node->field_food_nid[0]['nid'] = $form_state['values']['food_nid']; //记录客户所订餐的编号
$node->field_qty[0]['value'] = $form_state['values']['order_qty']; //记录客户订餐数量
$node->field_phone[0]['value'] = $form_state['values']['ship_phone']; //联系电话
$node->field_ship_address[0]['value'] = $form_state['values']['ship_address']; //送餐地址
node_save($node);
if ($node->nid)
{
drupal_set_message('订餐成功!您的订单号是:' . l($node->title, 'node/' . $node->nid) . ',点击查看');
}
else
{
drupal_set_message('订餐失败!请重新下订单。', 'error');
}
}