簡體   English   中英

如何在Drupal 7中為節點設置自定義字段值?

[英]How to set custom field value for node in 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)
);

您可以使用數組的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()之前添加這些,並在調用時添加/更新字段內容。

克萊夫是現貨。 你也可以使用數組速記語法,這是更好的符號恕我直言......例如

$node->field_sup_id[LANGUAGE_NONE][0]['value'] = "2499";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM