tags:

views:

746

answers:

4

Hiya,

I currently have a datasource from a client in the form of XML, this XML has content for all the pages that the website we're making will contain. Now after parsing and preparing all this content, does anyone know how we can then (using) PHP automate the creation of a drupal node (including all related fields for that node i.e. CCK fields, paths).

Ideally a function we can send all the content to and the nodes get created. Now i don't mind putting it straight into the db, but i'm not quite sure what db tables get updated (as the drupal setup has a gazillion tables).

I've searched through google and the drupal docs, but i can't really find something for this (Which i assumed would be a simple and often used function by web developers on drupal..)

Your input will be very much appreciated!

Thanks in advance,

Shadi

+1  A: 

You should be able to use node_save() for that purpose. I haven't tried with cck fields, but if you add the fields they should be saved through the hooks run. However, if you don't create the nodetype with fields through code, the function will be dependent on the setup you do in the admin interface.

googletorp
+3  A: 

You can use Drupal's node_save function (as googletorp mentioned) something like this:

require 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$node = new stdClass();

$node->title = "My imported node";
$node->body = "The body of my imported node.";
$node->type = 'story'; 
$node->created = time();
$node->changed = $node->created;
$node->status = 1;
$node->promote = 1;
$node->sticky = 0;
$node->format = 1;
$node->uid = 1;

node_save($node);

See this article on the Acquia site for more (including CCK fields, if you're using pathauto that should create paths on node_save): http://acquia.com/blog/migrating-drupal-way-part-i-creating-node

Richard M
Perfect i think this looks like what i'm after :) cheers!
Shadi Almosri
A: 

node_save works for cck if you set the right type and format fields well in the array structure.

Geshan
A: 

How could I deal with Image attach field. I mean if I wan't the node_save function to import the images to the server and save them in the database. Also create thumbs of them??

I have a client who wants to run a script of all his new items and sleep :)

Please advise, Mohannad

Mohannad