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

3

我在Drupal 7中使用安装文件中的hook_node_info方法创建了自定义节点类型:

// 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和正文字段没有保存。你有什么想法我在做错了什么吗?

1个回答

2

经过大量的挖掘,我发现在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”,只是因为当通过表单输入数据时,我看到它进入数据库。

谢谢!那对我非常有效。我相信只要保持相同的结构,摘要和格式字段可以留空。例如,我使用 $node->field_id[LANGUAGE_NONE][0]['value'] = 'some id value']; 来设置我的自定义 field_id 字段,这很有效。 - Sam King

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接