2011-11-25 53 views
4

我尝试使用下面的代码从外部脚本添加一个新的节点:如何在Drupal 7中为节点设置自定义字段值?

define('DRUPAL_ROOT', getcwd()); 
    include_once './includes/bootstrap.inc'; 
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); 
    $node = new stdClass(); 
    $node->title = "Your node title"; 
    $node->body = "The body text of your node."; 
    $node->type = 'rasskazi'; 
    $node->created = time(); 
    $node->changed = $node->created; 
    $node->status = 1; // Published? 
    $node->promote = 0; // Display on front page? 
    $node->sticky = 0; // Display top of page? 
    $node->format = 1; // Filtered HTML? 
    $node->uid = 1; // Content owner uid (author)? 
    $node->language = 'en'; 
    node_submit($node); 
    node_save($node); 

但是如何设置自定义字段值(“SUP_ID”整数为例)?

回答

7

像这样:

$node->field_sup_id[LANGUAGE_NONE] = array(
    0 => array('value' => $the_id) 
); 

如果你的字段有多个基数您可以添加额外的项目是这样的:

$node->field_sup_id[LANGUAGE_NONE] = array(
    0 => array('value' => $the_id), 
    1 => array('value' => $other_id) 
); 

而且你可以使用数组的language元素来定义该用什么语言特定字段值属于:

$lang = $node->language; // Or 'en', 'fr', etc. 
$node->field_sup_id[$lang] = array(
    0 => array('value' => $the_id), 
    1 => array('value' => $other_id) 
); 

广告d在您致电node_save()之前,这些字段内容将在您调用它时添加/更新。

+0

Upvote for Clive! –

0

克莱夫是现货。你也可以使用数组简写语法,这是更好的表示法。

$node->field_sup_id[LANGUAGE_NONE][0]['value'] = "2499"; 
相关问题