I need to migrate nodes with attached files from Drupal 5 to Drupal 7. Basically I did it with media widget for file field what was easy but client wanted to have the functionality which allows him select which files should be displayed (this comes with file widget - so I had to change the widget).
Below is screenshot from drupal 5 node
And from new site in drupal 7
As you can see I've already did migration of those files using a callback function for specific field. Problem is I don't know how to migrate description and display options. Any idea how to migrate those two option to new site? If you want I can attached my code.
<?php
/**
* @file
* NodePageMigration class.
*/
class NodePageMigration extends TestBaseMigration {
/**
* Constructor.
*/
public function __construct($arguments) {
parent::__construct();
$this->dependencies = array('NodeMicrositeHomepage');
$fields = array(
'node' => array(
'nid' => 'nid',
'vid' => 'vid',
'type' => 'type',
'title' => 'title',
'uid' => 'uid',
'status' => 'status',
'created' => 'created',
'changed' => 'changed',
'promote' => 'promote',
'sticky' => 'sticky',
),
'node_revisions' => array(
'nid' => 'nid',
'body' => 'body',
'teaser' => 'teaser',
'timestamp' => 'timestamp',
),
);
$query = $this->get_db_conn()->select('node', 'n');
$query->fields('n', array_keys($fields['node']));
$query->join('users', 'u', 'u.uid = n.uid');
$query->condition('n.type', 'page', '=');
$query->join('node_revisions', 'nr', 'nr.vid = n.vid');
$query->fields('nr', array_keys($fields['node_revisions']));
$source_pk = array(
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'alias' => 'n',
)
);
$source_fields = array();
foreach ($fields as $key => $values) {
$source_fields += $values;
}
$this->source = new MigrateSourceSQL($query, $source_fields);
$this->destination = new MigrateDestinationNode('page');
$this->map = new MigrateSQLMap(
$this->machineName,
$source_pk,
MigrateDestinationNode::getKeySchema()
);
$this->addFieldMapping('type', 'type');
$this->addFieldMapping('title', 'title');
$this->addFieldMapping('uid', 'uid');
$this->addFieldMapping('status', 'status');
$this->addFieldMapping('created', 'created');
$this->addFieldMapping('changed', 'changed');
$this->addFieldMapping('promote', 'promote');
$this->addFieldMapping('sticky', 'sticky');
$this->addFieldMapping('is_new')->defaultValue(TRUE);
$this->addFieldMapping('body:summary', 'teaser');
$this->addFieldMapping('body:format')
->defaultValue('client_input_format');
$this->addFieldMapping('field_document_attach', 'vid')
->callbacks(array($this, 'getNodeUploadedDocuments'));
$this->addFieldMapping('field_document_attach:file_class')
->defaultValue('MigrateFileFid');
// $this->addFieldMapping('field_document_attach:display');
}
/**
* Getting fid and list (display option in drupal 7)
* for specific vid (node version id)
*/
protected function getNodeUploadedDocuments($vid) {
// Drupal 5 source
$query = $this->get_db_conn()->select('files', 'f');
$query->fields('f', array('fid'));
$query->join('file_revisions', 'fr', 'f.fid = fr.fid');
$query->fields('fr', array('list'));
$query->condition('fr.vid', $vid, '=');
$result = $query->execute();
$files = array();
foreach ($result as $row) {
$fid = $this->importManagedFileFromRemoteFileproxy($row->fid);
$files[] = $fid;
}
$fids = count($files) ? $files : NULL;
return $fids;
}
/**
* Helper function to get the image from the old site.
*/
public function importManagedFileFromRemoteFileproxy($fid) {
// If on file set return NULL.
if (!$fid) {
return $fid;
}
$old = $this->getFileFromFid($fid);
// If no record in the db for the file returns NULL.
if ($old) {
$old_url = $this->remoteBaseUrl . 'fileproxy/' . $fid;
$destination = 'public://' . str_replace('files/', '', $old->filepath);
$dirname = drupal_dirname($destination);
file_prepare_directory($dirname, FILE_CREATE_DIRECTORY);
$file = $this->test_migrate_system_retrieve_file($old_url, $destination);
$fid = !empty($file->fid) ? $file->fid : NULL;
}
return $fid;
}
/**
* Helper function.
*
* @see system_retrieve_file()
*/
function test_migrate_system_retrieve_file($url, $destination = NULL, $replace = FILE_EXISTS_REPLACE) {
$parsed_url = parse_url($url);
if (!isset($destination)) {
$path = file_build_uri(drupal_basename($parsed_url['path']));
}
else {
if (is_dir(drupal_realpath($destination))) {
// Prevent URIs with triple slashes when glueing parts together.
$path = str_replace('///', '//', "$destination/") . drupal_basename($parsed_url['path']);
}
else {
$path = $destination;
}
}
$files = &drupal_static(__FUNCTION__, array());
if ($fid = array_search($path, $files)) {
return file_load($fid);
}
// If exists in our installation, but not in the database, fake a download.
if (file_exists($path)) {
$result = new StdClass();
$result->code = 200;
$result->data = file_get_contents($path);
}
else {
// Try normal download process.
$result = drupal_http_request($url);
// If protected use the HttpClient.
if ($result->code == 403) {
$client = migrate_get_http_client();
$result = $client->download($url);
}
}
if ($result->code != 200) {
drupal_set_message(t('HTTP error @errorcode occurred when trying to fetch @remote.', array('@errorcode' => $result->code, '@remote' => $url)), 'error');
return FALSE;
}
$local = file_save_data($result->data, $path, $replace);
if ($local->fid) {
$files[$path] = $local->fid;
}
if (!$local) {/**
* Helper function.
*
* @see system_retrieve_file()
*/
function test_migrate_system_retrieve_file($url, $destination = NULL, $replace = FILE_EXISTS_REPLACE) {
$parsed_url = parse_url($url);
if (!isset($destination)) {
$path = file_build_uri(drupal_basename($parsed_url['path']));
}
else {
if (is_dir(drupal_realpath($destination))) {
// Prevent URIs with triple slashes when glueing parts together.
$path = str_replace('///', '//', "$destination/") . drupal_basename($parsed_url['path']);
}
else {
$path = $destination;
}
}
$files = &drupal_static(__FUNCTION__, array());
if ($fid = array_search($path, $files)) {
return file_load($fid);
}
// If exists in our installation, but not in the database, fake a download.
if (file_exists($path)) {
$result = new StdClass();
$result->code = 200;
$result->data = file_get_contents($path);
}
else {
// Try normal download process.
$result = drupal_http_request($url);
// If protected use the HttpClient.
if ($result->code == 403) {
$client = migrate_get_http_client();
$result = $client->download($url);
}
}
if ($result->code != 200) {
drupal_set_message(t('HTTP error @errorcode occurred when trying to fetch @remote.', array('@errorcode' => $result->code, '@remote' => $url)), 'error');
return FALSE;
}
$local = file_save_data($result->data, $path, $replace);
if ($local->fid) {
$files[$path] = $local->fid;
}
if (!$local) {
drupal_set_message(t('@remote could not be saved to @path.', array('@remote' => $url, '@path' => $path)), 'error');
}
if (!file_exists($local->uri)) {
drupal_set_message(t('@path downloaded from @remote was not found in the local system.', array('@remote' => $url, '@path' => $path)), 'error');
}
return $local;
}
drupal_set_message(t('@remote could not be saved to @path.', array('@remote' => $url, '@path' => $path)), 'error');
}
if (!file_exists($local->uri)) {
drupal_set_message(t('@path downloaded from @remote was not found in the local system.', array('@remote' => $url, '@path' => $path)), 'error');
}
return $local;
}
}