Automatic Nodetitles 使用nid做标题
Automatic Nodetitles可以让你在没有输入标题的情况下自动添加一个标题, 但是由于其内在机制的原因, 当你设置使用nid为标题的时候, 无法创建, 因为Automatic Nodetitles是在文章创建之前就添加了这个标题, 而此时文章还未写入数据库, 也就不存在nid了. 不过有人给出了解决方法.
打开auto_nodetitle.module, 在74行添加如下代码:
/**
* Implementation of hook_nodeapi().
* Needed for newly saved nodes
*/
function auto_nodetitle_nodeapi(&$node, $op, $teaser, $page) {
// First check if this nodetype is auto_nodetitle enabled
$setting = auto_nodetitle_get_setting($node->type);
if ($setting == AUTO_NODETITLE_ENABLED || ($setting == AUTO_NODETITLE_OPTIONAL && empty($form_state['values']['title']))) {
switch ($op) {
// Now works for insert and update. Title is stored in database with db_query instead of node_save
case 'insert':
case 'update':
$types = node_get_types();
$pattern = variable_get('ant_pattern_'. $node->type, '');
if (trim($pattern)) {
$node->changed = time();
$node->title = _auto_nodetitle_patternprocessor($pattern, $node);
}
else if ($node->nid) {
$node->title = t('@type @node-id', array('@type' => $types[$node->type]->name, '@node-id' => $node->nid));
}
else {
$node->title = t('@type', array('@type' => $types[$node->type]->name));
}
drupal_set_message($node->title . t(' has been generated. Node id is ') . $node->nid);
db_query("UPDATE {node} SET title = '%s' WHERE nid = %d", $node->title, $node->nid);
db_query("UPDATE {node_revisions} SET title = '%s' WHERE nid = %d AND vid = %d", $node->title, $node->nid, $node->vid);
break;
default:
break;
}
}
}这个方法我在auto_nodetitle 6.x-1.0版本测试通过, 据说也可用于5.x版本. 经过测试, 在5.x中, 这段代码也可以正常工作哦, 不过需要在大概47行, 替换掉
<?php
/**
* Implementation of hook_nodeapi().
*/
function auto_nodetitle_nodeapi(&$node, $op, $form = NULL, $a4 = NULL) {
if ($op == 'validate') {
if ((auto_nodetitle_get_setting($node->type) == AUTO_NODETITLE_ENABLED) || ((auto_nodetitle_get_setting($node->type) == AUTO_NODETITLE_OPTIONAL) && empty($node->title))) {
/*
* Sets the node title.
* We need to do this on validation because of two points:
* It's early engouh to have node previews working and
* it's late enough as CCK has already gone through the 'process form values' step
*
* As changes to $node are not persistent during validation, we use form_set_value()!
*/
auto_nodetitle_set_title($node);
form_set_value($form['title'], $node->title);
}
}
}
?>另外, 使用这个方法有一个缺点, 就是必须使用自动创建的标题而不能手动输入一个标题.
评论
发表新评论