You are here

function filefield_migrate_prepare_node in Migrate Extras 6

Implementation of hook_migrate_prepare_node().

File

./filefield.migrate.inc, line 11
Hooks for handling FileFields in destination nodes.

Code

function filefield_migrate_prepare_node(&$node, $tblinfo, $row) {
  $errors = array();
  module_load_include('inc', 'filefield', 'field_file');
  foreach (filefield_migrate_fields_node($node->type) as $source_key => $label) {
    if (!empty($node->{$source_key})) {
      $source_file = $node->{$source_key};
      $field_name = preg_replace('/_sourcefile$/', '', $source_key);
      $field = content_fields($field_name, $tblinfo->desttype);
      $validators = filefield_widget_upload_validators($field);
      if ($field['widget']['type'] == 'imagefield_widget') {
        $validators = array_merge($validators, imagefield_widget_upload_validators($field));
      }
      $files_path = _migrate_widget_files_directory($field);
      if ($file = field_file_save_file($source_file, $validators, $files_path)) {
        if (!isset($node->{$field_name})) {
          $node->{$field_name} = array();
        }
        array_push($node->{$field_name}, $file);
      }
      else {
        $errors[] = migrate_message(t('File save failed for %file', array(
          '%file' => $source_file,
        )));
      }
    }
  }
  return $errors;
}