gpt4 book ai didi

php - 如何在 Drupal 7 中为节点设置自定义字段值?

转载 作者:可可西里 更新时间:2023-10-31 22:14:47 24 4
gpt4 key购买 nike

我正在尝试使用下一个代码从外部脚本添加一个新节点:

    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'整数)?

最佳答案

像这样:

$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)
);

在调用 node_save() 之前添加这些内容,当您调用它时,字段内容将被添加/更新。

关于php - 如何在 Drupal 7 中为节点设置自定义字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8274815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com