php – 如何在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’整数)?

解决方法:

像这样:

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

您可以使用数组的语言元素来定义此特定字段值所属的语言:

$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()之前添加这些,并在调用添加/更新字段内容.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...