tags:

views:

24

answers:

2

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'   => '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?

A: 

Did you try naming them something else?

tom
yes tom, Geez. :/I've tried foo_url, field_url, field_foo_url, and a few others.
Cory Collier
+1  A: 

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.

Cory Collier