Skip to content

Instantly share code, notes, and snippets.

@and1truong
Created March 30, 2012 09:14
Show Gist options
  • Select an option

  • Save and1truong/2250220 to your computer and use it in GitHub Desktop.

Select an option

Save and1truong/2250220 to your computer and use it in GitHub Desktop.
cck/modules/content_migrate/modules/content_migrate.filefield.inc
<?php
/**
* Implements hook_content_migrate_data_record_alter().
*
* Tweaks individual records in a field.
*/
function content_migrate_filefield_data_record_alter(&$record, $field, $instance) {
switch($field['type']) {
case 'image':
// Map D6 imagefield field columns to D7 image field columns.
if (!empty($record[$field['field_name'] . '_data']) && ($data = unserialize($record[$field['field_name'] . '_data']))) {
$record[$field['field_name'] . '_alt'] = $data['alt'];
$record[$field['field_name'] . '_title'] = $data['title'];
}
else {
unset($record[$field['field_name'] . '_alt']);
unset($record[$field['field_name'] . '_title']);
}
// Fall through.
case 'file':
// Map D6 filefield field columns to D7 file field columns.
if (!empty($record[$field['field_name'] . '_data']) && ($data = unserialize($record[$field['field_name'] . '_data'])) && isset($data['description'])) {
$record[$field['field_name'] . '_description'] = $data['description'];
}
else {
unset($record[$field['field_name'] . '_description']);
}
if (isset($record[$field['field_name'] . '_list'])) {
$record[$field['field_name'] . '_display'] = $record[$field['field_name'] . '_list'];
unset($record[$field['field_name'] . '_list']);
}
// Copies imagefield data from the old 'files' table into 'files_managed' and sets file_usage
// Mostly copied from system_update_7061, which does the same for the D6 core 'upload' module
$nid = $record['entity_id'];
$fid = $record[$field['field_name'] . '_fid'];
if (empty($fid)) {
$record = NULL;
return;
};
$file = db_select('files', 'f')->fields('f', array('fid', 'uid', 'filename', 'filepath', 'filemime', 'filesize', 'timestamp', 'status'))
->condition('fid', $fid)
->execute()
->fetchObject();
$basename = variable_get('file_directory_path', conf_path() . '/files');
$scheme = file_default_scheme() . '://';
// We will convert filepaths to uri using the default scheme
// and stripping off the existing file directory path.
$file->uri = $scheme . str_replace($basename, '', $file->filepath);
$file->uri = file_stream_wrapper_uri_normalize($file->uri);
unset($file->filepath);
// Insert into the file_managed table.
// Each fid should only be stored once in file_managed.
try {
db_merge('file_managed')
->key(array(
'fid' => $file->fid,
))
->fields(array(
'uid' => $file->uid,
'filename' => $file->filename,
'uri' => $file->uri,
'filemime' => $file->filemime,
'filesize' => $file->filesize,
'status' => $file->status,
'timestamp' => $file->timestamp,
))
->execute();
}
catch (Exception $e) {
}
// Add the usage entry for the file.
file_usage_add($file, 'file', 'node', $nid);
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment