Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save custom node types in Drupal 7

Tags:

php

drupal

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?

like image 598
Cory Collier Avatar asked Mar 02 '26 20:03

Cory Collier


1 Answers

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.

like image 196
Cory Collier Avatar answered Mar 05 '26 11:03

Cory Collier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!