老葛的Drupal培训班 Think in Drupal
到目前为止,你已经为你的新节点类型定义了元数,还有定义了访问控制权限。接着,你需要构建节点表单,这样用户就可以输入笑话了。你可以通过实现hook_form()来完成这一工作:
/**
* Implementation of hook_form().
*/
function joke_form($node) {
// Get metadata for this node type
// (we use it for labeling title and body fields).
// We defined this in joke_node_info().
$type = node_get_types('type', $node);
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5,
'#maxlength' => 255,
);
$form['body_filter']['body'] = array(
'#type' => 'textarea',
'#title' => check_plain($type->body_label),
'#default_value' => $node->body,
'#rows' => 7,
'#required' => TRUE,
);
$form['body_filter']['filter'] = filter_form($node->format);
$form['punchline'] = array(
'#type' => 'textfield',
'#title' => t('Punchline'),
'#required' => TRUE,
'#default_value' => isset($node->punchline) ? $node->punchline : '',
'#weight' => 5
);
return $form;
}
注意:如果你不熟悉表单API的话,请参看第10章。
作为站点管理员,如果你已经启用了你的模块,现在你就可以导航到“创建内容➤笑话”来查看新创建的表单了。在前面函数中,第一行代码返回了该节点类型的元数据信息。node_get_types()将检查$node->type以判定要返回哪种节点类型的元数据(在我们的例子中,$node->type的值将为“joke”)。这里再强调一遍,在钩子hook_node_info()中设置节点元数据,你已经在前面的joke_node_info()中设置了它。
图 7-3.笑话的提交表单