0

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).

File widget

Below is screenshot from drupal 5 node old site

And from new site in drupal 7 enter image description here

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.

enter image description here

<?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;
  }  
}
0

1 Answer 1

0

try

$this->addFieldMapping('field_document_attach:language', 'LANGUAGE_DATA' );

'LANGUAGE_DATA' needs to come from your source.

If this doesn't work I have used the prepare function also to access destination fields I have struggled with; however those where not showing up in the Admin UI whereas yours are. Let me know if you still have issues and I will post up that code .

http://drupalcontrib.org/api/drupal/contributions!migrate!plugins!destinations!entity.inc/function/MigrateDestinationEntity%3A%3Aprepare/7

6
  • Hi Deejay! Thanks for your answer but it doesn't solve my problem, I want to migrate "field_document_attach:description" and "field_document_attach:description". The problem is that it is multi-value field and I don't know how to do it.
    – user7929
    Commented Sep 12, 2014 at 8:07
  • "field_document_attach:description" and "field_document_attach:description" they are exactly the same; do you mean "field_document_attach:description" and "field_document_attach:display"
    – Deejay
    Commented Sep 12, 2014 at 8:12
  • exactly, my mistake I wanted say "field_document_attach:description" and "field_document_attach:dispaly"
    – user7929
    Commented Sep 12, 2014 at 8:51
  • And you can see these fields in the Admin UI. then $this->addFieldMapping('field_document_attach:description', 'DESCRIPTION_DATA' ); $this->addFieldMapping('field_document_attach:display', 'DISPLAY_DATA' ); should work; as I have this in some of my migrates and it works ! or am I not understanding the question fully ?
    – Deejay
    Commented Sep 12, 2014 at 8:56
  • I know how to do it, the problem is that I can't get those values easy in sql source because it is mutli-value, even with concat function
    – user7929
    Commented Sep 12, 2014 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.