How to save custom node types in Drupal 7

I created my own node type in Drupal 7 using the method hook_node_infoin the installation 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 this type in a 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 problem is that url and body fields are not saved. Any idea what I'm doing wrong?

+3
source share
1 answer

So, after a ton of digging, it turns out that the way to enter custom fields in node_save was wrong. The node_save node should look like this:

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' , , , .

+2

Source: https://habr.com/ru/post/1758614/


All Articles