Migrate module: file handling

So you’re migrating pages and users from another CMS to Drupal using the Migrate module, but how do you handle all those file attachments? This example will cover taking the input filename and adding it to an imagefield on a Drupal content type. However, this should work in exactly the same way for filefields too.

The first thing you need to do is to take all files to be migrated and place them in your site’s files directory. If you plan on using the migrate module’s “clear imported records” feature, so as to do multiple test runs, you should retain a backup of these files as when the nodes are cleared / deleted, so are the files.

Create the CCK content type and add an imagefield to it to store the migrated image. Then add a content set to map the source content fields to their Drupal equivalents. When configuring the content set, you will see three fields for your image: “fid”, “list” and “data”. We’re going to ignore these and instead set the field value using the hook_migrate_destination_prepare_node().

function mymodule_migrate_destination_prepare_node(&$node, $tblinfo, $row) WHERE filepath = '%s'", $filepath));

// Create the file entry in the ‘files’ table if necessary.
if (!$file->fid)

// Set up the image field.
$node->field_news_image

[0] = array(
‘fid’ => $file->fid,
‘list’ => 1,
‘data’ => array(‘alt’ => $node->title, ‘title’ => $node->title, ‘description’ => $node->title),
‘uid’ => $node->uid,
‘filename’ => $file->filename,
‘filepath’ => $file->filepath,
‘filemime’ => $file->filemime,
‘status’ => 1,
‘timestamp’ => $node->created,
);

}
// Print an error message if the file doesn’t exist.
else
}
}
?>

In our example, we used a content type called “news” with a field_news_image imagefield, while the column containing the image filename in the source CMS is cms_news_table_image.

The hook_migrate_destination_prepare_node() hook is invoked just before each node record is migrated, allowing the module to manipulate the node data, one row at a time, immediately before it is stored to Drupal’s database.

For each row, we check that the file we wish to migrate exists in the “files” directory and create a record for it in Drupal’s files table if one doesn’t already exist. Finally we configure the field_news_image field with all the required information.

2017-03-31T06:20:18+00:00 Categories: Drupal|

About the Author: