如何保存自定义节点类型在Drupal 7

问题描述:

我在Drupal 7中创建一个自定义的节点类型,使用hook_node_info方法是安装文件:如何保存自定义节点类型在Drupal 7

// declare the new node type 
function foo_node_info () { 
    return array(
    'foo' => array(
     'name' => t('Foo entry'), 
     'base' => 'node_content', 
     'description' => t('For use to store foo entries.'), 
)); 
} // END function foo_node_info 

,我试图挽救该类型在模块文件中使用以下代码:

// INSERT the stuff 
node_save(node_submit((object)array(
    'type' => 'foo', 
    'is_new' => true, 
    'uid'  => 1, 
    'title' => 'Title, blah blah blah', 
    'url'  => 'url here, just pretend', 
    'body' => '<p>test</p>', 
))); 

我的问题是,url和body字段没有保存。任何想法我做错了什么?

因此,在挖掘之后,事实证明,我在node_save中输入自定义字段的方式是错误的。该node_save需要如下所示:

node_save(node_submit((object)array(
    'type' => 'foo', 
    'is_new' => true, 
    'uid'  => 1, 
    'title' => 'the title', 
    'url'  => array(
     'und' => array(array(
     'summary' => '', 
     'value' => 'url value', 
     'format' => 2, 
    ))), 
    'body' => array(
     'und' => array(array(
     'summary' => '', 
     'value' => 'the body goes here', 
     'format' => 2, 
    ))), 
))); 

注意,自定义字段,该阵列结构具有匹配的内容以前发生的事情与CCK(几乎完全一致)。数组中描述字段值的第一个键是内容的语言。

我在这里只用了'und',因为这就是我在通过表单输入数据时看到的数据库。

+0

谢谢!这对我来说非常合适。我相信,只要保持相同的结构,摘要和格式字段可以留空。例如,我做了$ node-> field_id [LANGUAGE_NONE] [0] ['value'] ='某个id值'];并努力设置我的自定义field_id字段。 – 2012-01-30 08:30:29