Hi,

I need to insert a CCK node based on an automatic script that executes every X minutes, this script should insert a CCK node with values taken from other source.

Is there a CCK API or something to insert these values from plain PHP file?

Or do you know a better solution?

Thank you!

Leandro

Comments

chrisv-1’s picture

This one definitely needs some documentation!

The following code inside of a module seems to work for CCK textfields (but I don't know whether it is really the "right" approach):

	$node = array();
	$node['type'] = 'blog';
	$node['title'] = 'test_node!';
	$node['body'] = '<h1><center>this is the body</center></h1>';
	$node['format'] = '3'; // 3 = unfiltered HTML
	$node['comment_settings'] = '2'; // 0=off 1=readonly 2=allowed
	$node['field_topic'] = array(array('value' => 'test topic'));
	$node['field_subtitle'] = array(array( 'value' => 'test subtitle'));
	$node['field_author'] = array(array( 'value' => 'test author' ));
	$node['path'] = 'this/is/a/test.html';
	$node = node_submit($node);
	node_save($node);

In this example, there are three CCK textfields named "topic", "subtitle" and "author"; content type is "blog". Note that the double array around the CCK values is absolutely necessary, otherwise only the first character of the string will be stored (even if the fields are single-value only).

elwieneko’s picture

This works fine for me, but once I try to create a nodereference or userreference it doesn't anymore. The data in these fields is not saved and worse no error is shown!

I used the following code


global $user;
[…]
$node['field_uref'] = array (0 => array('uid' => $user->uid));
$node = node_submit($node);
node_save($node);

Any ideas or suggestions?

elwieneko’s picture

Ok, maybe somebody might find this relevant in the future:
I looked at the POST request that Drupal receives via the wonderful Trace module. It turns out that the format of references differs from other content types.
This line worked for me:

global $user;
[…]
$node['field_uref'] = array('uids' => $user->uid); 
$node = node_submit($node);
node_save($node);

The main differences are that

  • there is only one array
  • the key is a plural form
  • Happy coding,
    Lars

    karens’s picture

    Status: Active » Closed (won't fix)

    Closing old issues. None of us is using the D5 version any more, so hard to provide any support. Sorry.