Как перенести поле изображения с помощью модуля migrate 2 на drupal 6
Я создал свой файл .module и .info и (один) .inc.
Я также успешно использовал drush mi для импорта данных за вычетом изображений.
Изображения находятся в связанной таблице в базе данных и не копируются при запуске класса migrate. Вот мой код:
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;
}
}
Итак, чтобы было ясно. Существуют две таблицы данных migrate_ror_pages и migrate_ror_pages_images. Первая таблица импортирует нормально, но изображения не импортируются. В таблице изображений есть URL-адрес изображения, подобный этому /sites/all/default/files/uploads/page_assets/images/filename.jpg и в нем есть столбец заголовка, идентификатор страницы и идентификатор изображения. На странице более одного изображения.
1 answers
Ладно, я понял, в чем дело. Мне нужно было поместить URL-адрес сайта перед путем в моей бд.
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);
}
Строка, которую необходимо было изменить, касалась пути к изображению. Я использовал относительный путь/sites/default/.../filname.jpg и мне нужно было использовать полный путь:
'path' => $row->$GLOBALS['base_url'] . filename_path,
Поэтому, если вы пытаетесь импортировать изображения/файлы, вам понадобится полный путь к изображению, а не повторный!
Надеется, что это кому-то поможет.
//Крейг