繁体   English   中英

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

[英]How do I save custom node types in Drupal 7

我在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和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'只是因为这是我通过表单输入数据时进入数据库的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM