Probably ripped off from somewhere on drupal.org...
import xmlrpclib s = xmlrpclib.ServerProxy('http://mmatienzo/services/xmlrpc') class DrupalNode: def __init__(self, title, body, path, ntype='page', uid=1, username='mmatienzo'): self.title = title self.body = body self.path = path self.type = ntype self.uid = uid self.nid = 67 self.name = username self.promote = True self.taxonomy = {'3': '3'} #how do i create new taxonomy terms??? try: sessid, user = s.system.connect() n = DrupalNode('ZA WARUDO!', 'toki wo tomare', 'saworhhhjjjdss') s.node.save('roadroallerdawryyy', n) except xmlrpclib.Fault, err: print "A fault occurred" print "Fault code: %d" % err.faultCode print "Fault string: %s" % err.faultString
This works similarly to the Taxonomy Defaults module, but has no UI to set this up - this is for programmatic, backend stuff. It tests to see if somethings a particular content type and then checks for the value of a given CCK field to determine the appropriate term.
<?php function custom_taxonomy_defaults_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { if ($op == 'presave') { $taxonomy = $node->taxonomy; switch ($node->type) { case 'online_project_old': $vid = '15'; $projtype = $node->field_external_resource_type[0]['value']; switch ($projtype) { case '1': # online exhibition case '2': # special project $taxonomy[$vid] = '902'; # special projects break; case '3': # index $taxonomy[$vid] = '901'; # research tools break; case '4': # ebooks/digital collection $taxonomy[$vid] = '895'; # collections break; } break; } if (isset($taxonomy)) { $node->taxonomy = $taxonomy; } } } ?>
The long and the short of it, for Drupal 6 - use content_insert(), which should be fired anyway but isn't. See
node_save() with CCK fields for more details. drupal_execute() is too robust for me - I just needed to import a SQL Server table with 4500+ rows quickly. Sample code after the jump, using parsecsv for PHP.
<?php include_once('./includes/bootstrap.inc'); include_once('./includes/form.inc'); include_once('./modules/node/content_types.inc'); drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); bootstrap_invoke_all('init'); ini_set('memory_limit', '512M'); user_authenticate('user','password'); require_once('/home/mmatienzo/parsecsv.lib.php'); $csv = new parseCSV(); $csv->auto('/home/mmatienzo/manu_collections.uniq.csv'); module_load_include('inc', 'node', 'node.pages'); $i = 0; //print_r($csv->data); foreach ($csv->data as $row) { $node = new stdClass(); #$node = array('type'=>'amatimport'); $form_state = array(); $node->type = 'amatimport'; $node->title = $row['ManuscriptTitle']; //$node->name = 'mmatienzo'; $node->language = 'en'; //$node->uid = '1'; $node->field_arms_amat_id[0]['value'] = $row['Id']; $node->field_arms_findingaids[0]['value'] = $row['FindingAids']; $node->field_arms_amat_printfindingaid[0]['value'] = $row['PrintFindingAid']; $node->field_arms_catnyp_old[0]['value'] = $row['CatnypLink']; $node->field_arms_amat_project_link[0]['value'] = $row['ProjectLink']; $node->field_arms_amat_seealso[0]['value'] = $row['SeeAlso']; $node->field_arms_closed['value'] = $row['Closed']; $node->field_arms_amat_location[0]['value'] = $row['Location']; $node->field_arms_sgmcatnyp[0]['value'] = $row['SgmCatnyp']; $node->field_arms_sgmead[0]['value'] = $row['SgmEad']; $node->field_arms_seeref[0]['value'] = $row['SeeRef']; $node->field_arms_creator[0]['value'] = $row['ManuscriptAuthor']; $node->field_arms_mssdbid[0]['value'] = $row['MSS_ID']; $node->op = t('Save'); if ($row['Wilson'] = 'TRUE') { $wilson = 1; } else { $wilson = 0; } $node->field_arms_amat_wilson['value'] = $wilson; content_presave($node); node_save($node); content_insert($node); $i++; print $i . '<br/>'; #break; ?>
Similar to the CCK migration thing I posted before I found a peculiarity dealing with filefield and nodeapi - for some reason, it didn't actually update the filefield until I ran a separate node_save()...
<?php function archivalcollection_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { if ($op == 'presave') { switch ($node->type) { case 'archivalcollection': if ((empty($node->field_arms_pdffile[0]['fid']) || $node->field_arms_pdffile[0]['fid'] == 0) && !empty($node->field_arms_amat_printfindingaid[0]['value'])) { $pdfurl = $node->field_arms_amat_printfindingaid[0]['value']; $pdfpath = file_directory_path() .'/archivalcollections/pdf'; $tmppath = file_directory_temp(); $tmpfile = $tmppath.'/'.basename($pdfurl); if (!$pdfdata = file_get_contents($pdfurl)) { watchdog('AMAT Migration', "nid $node->nid - Could not read $pdfurl", NULL, WATCHDOG_ERROR); } elseif (!$tmppdf = file_save_data($pdfdata, $tmpfile)) { watchdog('AMAT Migration', "nid $node->nid - Could not write to $tmpfile", NULL, WATCHDOG_ERROR); } elseif (!$file = field_file_save_file($tmpfile, array(), $pdfpath)) { watchdog('AMAT Migration', "nid $node->nid - Could not create file object for file $tmpfile", NULL, WATCHDOG_ERROR); } else { $fc = $file; $fid = $fc['fid']; $file = field_file_save($node, $file); $node->field_arms_pdffile = array( 0 => array( 'fid' => $fc['fid'], 'title' => basename($fc['filename']), 'filename' => $fc['filename'], 'filepath' => $fc['filepath'], 'filesize' => $fc['filesize'], 'mimetype' => $fc['filemime'], 'data' => array('description' => ''), 'list' => 1, ), ); node_save($node); } } // } break; } } }
A sample chunk of CCK computed field code for use with my Shrew module for Drupal. Shrew is a PHP library to interact with Innovative Interface online library catalog systems. You can see how this code snippet evolved over time in the revisions.
<?php if (!$node->nid) { node_save($node); } $delta = 0; $node_field = array(); if ($bnum = $node->field_iii_single[0]['value']) { $record = shrew_record_get($bnum); if ($fields = shrew_field_get($record[$bnum]->bibliographicRecord, '520')) { foreach ($fields as $field) { $node_field[$delta]['value'] = $field; $delta++; } } else $node_field[0]['value'] = NULL; }
This code snippet autopopulates a Computed Field CCK field for Drupal using the the functionality provided by the UUID module. Note that this is for the 6.x-1.x-dev version of the module and requires the patch attached to this comment. It assumes that you only want to generate UUIDs once. It probably should be abstracted into another CCK module, but I'm too lazy to do that at the moment.
<?php if (empty($node_field[0]['value'])) { $node_field[0]['value'] = uuid_uuid(); }