Creating Drupal CCK content programmatically/ via API

cckdrupal

I am working with a Drupal 6.x system to create exercise / personal training programmes, and am using the CCK with content types of Exercise and Programme, where Programme contains a few header fields and a list of node references to the exercises it consists of. This works great and I can manually create programmes which work fine. I now wish to create a module which can generate these programs automatically based on a number of algorithms I have developed, the process will look like:

  1. Load all exercises into array
  2. Load users personal info (entered previously)
  3. Establish best suited exercises
  4. Create new programme content type
  5. Save programme

An Exercise has a number of related attributes and although I could do all of the above using SQL directly into the tables it would be quite complex and doesn't feel right. I would like in step 1 to load the exercises as an array of Exercise objects (node_load?), and then create a programme object and save. Is this OO type approach possible or do I have to resort to manipulating the data directly?

Best Answer

The best way to tackle this problem would be to write your own module to do this.

Step 1 you can do node_load($nid) on all the excercies Step 2 you can use user_load($uid) Step 3 you'll need to iterate through the user object and match up to the appropriate excercies. Step 4/5 I'd create a new $node = stdClass(); object and populate the attributes with the correct data then perfrom a node_save($node); this will assign it a $node->id etc.

If your unsure on what attributes are in your training program node, then do a print_r($node); on one you've created already.

Phil

Related Topic