【问题标题】:How to set custom field value for node in Drupal 7?如何在 Drupal 7 中为节点设置自定义字段值?
【发布时间】:2023-03-28 18:35:01
【问题描述】:

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

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

【问题讨论】:

    标签: php drupal drupal-7


    【解决方案1】:

    像这样:

    $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() 之前添加这些内容,并且在您致电时将添加/更新字段内容。

    【讨论】:

      【解决方案2】:

      克莱夫是正确的。您还可以使用数组速记语法,恕我直言,这是更好的表示法...例如

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

      【讨论】:

        猜你喜欢
        • 2015-10-10
        • 1970-01-01
        • 1970-01-01
        • 2012-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多