Skip to content

Instantly share code, notes, and snippets.

@anarchivist
Created December 24, 2009 17:30
Show Gist options
  • Select an option

  • Save anarchivist/263277 to your computer and use it in GitHub Desktop.

Select an option

Save anarchivist/263277 to your computer and use it in GitHub Desktop.
Programmatic file migration in Drupal for existing nodes over HTTP using CCK filefield and nodeapi
<?php
/* 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().. */
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment