Import multiple images to Drupal Image Field us...
2012-02-25 00:00
281 查看
Drupal 7.12, Migrate 2.3
class GarmentPictureMigration extends Migration { public function __construct() { parent::__construct(MigrateGroup::getInstance('plm')); $this->description = t('Migrate Garment Pictures from PLM.'); $this->map = new MigrateSQLMap($this->machineName, array( 'ID' => array( 'type' => 'varchar', 'size' => 'normal', 'not null' => TRUE, 'description' => 'User ID', ) ), MigrateDestinationNode::getKeySchema() ); $fields = array( 'ID' => 'ID', 'IMAGES' => 'Images' ); $query = "select trim(style_id)||trim(color_id) ID from PLM.SC_IMAGE WHERE MIGRATED=0 GROUP BY STYLE_ID,COLOR_ID"; $count_query = "SELECT COUNT(1) FROM (SELECT 1 FROM PLM.SC_IMAGE GROUP BY STYLE_ID,COLOR_ID)"; global $conf; $this->source = new MigrateSourceOracle($conf['oracle_db'], $query, $count_query, $fields); $this->destination = new MigrateDestinationNode('garment_picture'); $file_args = MigrateFileFieldHandler::arguments(array('source_field'=>'FILE_NAME'), 'file_blob', FILE_EXISTS_RENAME); $this->addFieldMapping('field_images', 'IMAGES') ->arguments($file_args); } public function prepareRow($row) { $images = array(); $s = oci_parse($this->source->getConnection(), "SELECT rawtohex(id) id,file_name, file_blobfied_date FROM PLM.SC_IMAGE WHERE trim(STYLE_ID) || trim(COLOR_ID) ='". $row->ID ."'"); oci_execute($s, OCI_DEFAULT); while (oci_fetch($s)) { $blob = oci_result($s, "FILE_BLOB")->load(); $data = array( 'path' => oci_result($s, "ID").'.jpg', 'alt' => oci_result($s, "FILE_NAME"), 'title' => oci_result($s, "FILE_NAME"), 'contents' => $blob, ); $images[] = $data; $row->FILE_NAME = oci_result($s, "FILE_NAME"); } $row->IMAGES = $images; return TRUE; } //修改MigrateFileFieldHandler, modules/migrate/plugins/destinations/fields.inc 275行 public function prepare($entity, array $field_info, array $instance, array $values) { $destination_dir = $this->destinationDir($field_info, $instance); file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY); foreach($values as $value){ $destination_file = file_stream_wrapper_uri_normalize($destination_dir . "/" . str_replace('/', '-', $value['path'])); if (!file_exists($destination_file)) { try { $return = (bool) file_put_contents($destination_file, $value['contents']); } catch (Exception $exception) { throw new MigrateException("Can't write file to: $destination_file"); return FALSE; } } } } //修改MigrateFileFieldHandler, modules/migrate/plugins/destinations/fields.inc 357行 protected function buildFileArray($entity, array $field_info, array $instance, $migration, $arguments, $value) { if (is_array($value) || $value{0} == '{') { $properties = is_array($value) ? $value : drupal_json_decode($value); $path = $properties['path']; // Properties passed in with the image override any set via arguments if (!empty($properties['path'])) { $arguments['source_path'] = $properties['path']; } if (!empty($properties['alt'])) { $arguments['alt'] = $properties['alt']; } if (!empty($properties['title'])) { $arguments['title'] = $properties['title']; } if (!empty($properties['description'])) { $arguments['description'] = $properties['description']; } if (!empty($properties['display'])) { $arguments['display'] = $properties['display']; } } else { $path = $value; } }
相关文章推荐
- Using Imagecache and CCK Imagefield to Add an Image in Drupal
- How to merge multiple images into one image - Java ImageIO
- L6305W: Image does not have an entry point. (Not specified or not set due to multiple choices.)
- [笔记].怎样解决MDK中的警告:Warning: L6305W: Image does not have an entry point. (Not specified or not set due to multiple choices.)
- ImageMagick Errors: Convert PDF to Images
- MediaStore.Images.Media.insertImage时发生Failed to insert image原因分析
- 解决No “rule to make target `../Database/images/login.png', needed by `debug/qrc_image.cpp'. Stop.”
- ImportError: You must install PIL to fetch image objects
- ImportError: Could not import PIL.Image. The use of `array_to_img` requires PIL.
- 解决docker 删除 images时,出现image is referenced in multiple repositories错误,不能删除
- How to select Multiple images from UIImagePickerController [duplicate]
- How to add multiple filters to UIImage GPUImage?
- using pyodbc on ubuntu to insert a image field on SQL Server
- How to import class from other package when building android image
- How to read images from drawable folder, not by R.drawable.[imageid]
- [SharePoint]javascript client object model 获取lookup 类型的field的值,包括user类型(单人或者多人)的值。how to get the multiple user type/lookup type field value by Javascript client object model
- attempt to index field 'image' (a nil value) torch
- How to delete old image when update ImageField?
- How to Change Default Web ADI Upload Parameters for FlexField Import / Validation
- How to Change Default Web ADI Upload Parameters for FlexField Import / Validation