开发者

How do I save custom node types in Drupal 7

I've created a custom node type in Drupal 7, using the hook_node_info method in the install file:

// 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

and I'm trying to save that type in the module file using the following code:

// INSERT the stuff
node_save(node_submit((object)array(
  'type'    => 'foo',
    'is_new'  => true,
    'uid'     => 1,
    'title'   =&g开发者_运维百科t; 'Title, blah blah blah',
    'url'     => 'url here, just pretend',
    'body'    => '<p>test</p>',
)));

My issue, is that the url, and body fields aren't saving. Any idea what I'm doing wrong?


So, after a ton of digging, it turns out that the way I was entering the custom fields in the node_save was wrong. The node_save needs to look like the following:

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,
    ))),
)));

Notice that for the custom fields, the array structure has to match what was previously going on with CCK (pretty much exactly). The first key in the array describing the field value is the language for the content.

I've used 'und' here only because that's what I saw going into the database when entering the data through a form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜