最近在学Drupal,但是由于英语实在不怎么样,只能看Drupal专业开发指南的中文版了,
不过有个问题是他上面的例子都是Drupal6的,和Drupal7有些不同,找了一晚上资料实在不知道怎么办了,只好来这试着问问了,希望明天能有好消息~
function annotate_node_view($node, $view_mode, $langcode) {
global $user;
// The 'view' operation means the node is about to be displayed.
if ($user->uid == 0 || ($view_mode != 'full' && node_is_page($node)) ) {
return;
}
// Find out which node types we should annotate.
$types_to_annotate = variable_get('annotate_node_types', array('page'));
// Abort if this node is not one of the types we should annotate.
if (!in_array($node->type, $types_to_annotate)) {
return;
}
// Add our form as a content item.
$node->content['annotation_form'] = array(
'#value' => drupal_get_form('annotate_entry_form', $node),
'#weight' => 10
);
//由于Drupal6的annotate_nodeapi的$node是引用的,而7的node_view却是传值的,所以不知道是不是用node_save($node)来保存$node值.
node_save($node);
//用了node_save后却什么事情都没发生,按道理在查看页面会显示我自定义的表单的~
}
/**
* Define the form for entering an annotation.
*/
function annotate_entry_form($form,$form_state, $node) {
// Define a fieldset.
$form['annotate'] = array(
'#type' => 'fieldset',
'#title' => t('Annotations'),
);
// Define a textarea inside the fieldset.
$form['annotate']['note'] = array(
'#type' => 'textarea',
'#title' => t('Notes'),
'#default_value' => isset($node->annotation) ? $node->annotation : '',
'#description' => t('Make your personal annotations about this content here. Only you (and the site administrator) will be able to see them.')
);
// For convenience, save the node ID.
$form['annotate']['nid'] = array(
'#type' => 'value',
'#value' => $node->nid,
);
// Define a submit function.
$form['annotate']['submit'] = array(
'#type' => 'submit',
'#value' => t('Update'),
);
return $form;
}