2

I have created my .module and .info and (one) .inc file.

I also have successfully used drush mi to import the data minus the images.

The images are in a related table in the database and are not being copied over when I run the migrate class. Here is my code:

    abstract class RoRMigration extends Migration {
     public function __construct() {
     parent::__construct();


     }
   }


    class RoRPagesMigration extends RoRMigration {
      public function __construct() {
      parent::__construct(MigrateGroup::getInstance('pages'));
      $this->description = t('Ruby on Rails Pages');


$this->map = new MigrateSQLMap($this->machineName,
  array(
    'page_id' => array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'description' => 'Page ID',
      'alias' => 'pid',
    )
  ),
  MigrateDestinationNode::getKeySchema()
);

$query = db_select('migrate_ror_pages', 'pid')
         ->fields('pid', array('page_id', 'title', 'content_subtitle', 'body_html', 'page_type',
          'redirect_url', 'published', 'meta_data', 'pretty_url'));



$source_fields = array(
 // TODO: Will add these other files later 
 // 'banners' => t('Banners attached to this page; populated in prepareRow()'),
 // 'files' => t('Files attached to this page; populated in prepareRow()'),
  'images' => t('Images attached to this page; populated in prepareRow()'),
);


$this->source = new MigrateSourceSQL($query, $source_fields);
$this->destination = new MigrateDestinationNode('page_data');

// Mapped fields
$this->addFieldMapping('title', 'title');
$this->addFieldMapping('field_legacy_guid', 'page_id');
$this->addFieldMapping('field_legacy_content_subtitle', 'content_subtitle');
$this->addFieldMapping('field_legacy_html_body', 'body_html');
$this->addFieldMapping('field_legacy_page_type', 'page_type');
$this->addFieldMapping('field_legacy_redirect_url', 'redirect_url');
$this->addFieldMapping('field_legacy_published', 'published');
$this->addFieldMapping('body', 'meta_data');
$this->addFieldMapping('field_pathauto', 'pretty_url');


// We will get the image data from a related table in prepareRow()
$arguments = MigrateFileFieldHandler::arguments('file_copy', FILE_EXISTS_REPLACE);
$this->addFieldMapping('field_gallery_images', 'images')
     ->arguments($arguments);


// No unmapped source fields

// Unmapped destination fields
$this->addUnmigratedDestinations(array('status', 'promote',
  'revision', 'language', 'uid', 'revision_uid', 'created', 'changed', 'teaser', 'sticky', 'field_file_attached_file', 'field_legacy_link', 'field_legacy_byline', 'field_banner_image', 'path', 'pathauto_perform_alias'));
      }

      public function prepareRow($current_row) {

        $source_id = $current_row->page_id;


// This adds the images to the image_gallery field
$result = db_select('migrate_ror_pages_images', 'pic')
          ->fields('pic', array('filename_path', 'title'))
          ->condition('page_id', $source_id)
          ->execute();
$current_row->images = array();
foreach ($result as $row) {
  $image_data = array(
    'path' => $row->filename_path,
    'alt' => $row->title,
    'title' => $row->title,
    'description' => $row->title,
  );
  $current_row->images[] = drupal_to_js($image_data);
}

return TRUE;
    }
   }

So to be clear. There are two data tables migrate_ror_pages and migrate_ror_pages_images. The first table imports fine but the images do not import. The image table has a url to the image like this /sites/all/default/files/uploads/page_assets/images/filename.jpg and it has a title column, page_id, and image_id. There are more than one images per page.

2

Okay so I figured out what was wrong. I needed to put the site url in front of the path in my db.

  public function prepareRow($current_row) {

    $source_id = $current_row->page_id;


    // This adds the images to the image_gallery field
    $result = db_select('migrate_ror_pages_images', 'pic')
      ->fields('pic', array('filename_path', 'title'))
      ->condition('page_id', $source_id)
      ->execute();
    $current_row->images = array();
    foreach ($result as $row) {
      $image_data = array(
       'path' => $row->$GLOBALS['base_url'] . filename_path,
       'alt' => $row->title,
       'title' => $row->title,
       'description' => $row->title,
      );
      $current_row->images[] = drupal_to_js($image_data);
     }

The line that needed to be changed was the one regarding the path to the image. I was using a relative path /sites/default/.../filname.jpg and I needed to use a full path:

           'path' => $row->$GLOBALS['base_url'] . filename_path,

So if you are trying to import images/files you will need a full path to the image not a relitave one!

Hopes this helps someone.

//Craig

  • I am using same way, but I am getting error when I am using "MigrateFileFieldHandler". ERROR is ( ! ) Fatal error: Call to undefined method MigrateFileFieldHandler::arguments() . Can you please help me with this? – Bhanu Prakash Ryaga Aug 14 '14 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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