function file_import_alter in Default Content 7
Same name and namespace in other branches
- 7.2 plugins/file.inc \file_import_alter()
Prepares file field for import
Create the file and attached the new file object in the file field
File
- plugins/
file.inc, line 42
Code
function file_import_alter(&$node) {
$fields = get_file_fields();
foreach ($fields as $field_name => $field) {
if (isset($node->{$field_name})) {
//unset($node->{$field_name});
}
if (isset($node->{$field_name})) {
foreach ($node->{$field_name} as $lang => $items) {
foreach ($items as $key => $item) {
$file = (object) $item;
$query = new EntityFieldQuery();
$fids = db_select('file_managed', 'f')
->condition('uri', $file->uri)
->fields('f', array(
'fid',
))
->execute()
->fetchCol();
if (!empty($fids) && isset($fids[0]) && is_object($fids[0])) {
file_delete($fid[0]);
}
$file = file_save_data($item['image'], $item['uri']);
// Decode image data. Older versions exported binary data
// so if this fails we asume that is the reason.
$data = base64_decode($item['image']);
if (!$data) {
$data = $item['image'];
}
$file = file_save_data($data, $item['uri']);
if ($field['type'] == 'image') {
$file->alt = $item['alt'];
$file->title = $item['title'];
}
elseif ($field['type'] == 'file') {
$file->display = $item['display'];
$file->description = $item['description'];
}
file_save($file);
$node->{$field_name}[$lang][$key] = (array) $file;
//unset($node->{$field_name}[$lang][$key]);
}
}
}
}
}