Skip to content

Instantly share code, notes, and snippets.

@adamzimmermann
Last active August 15, 2016 19:31
Show Gist options
  • Save adamzimmermann/251d0037840261160fe70b287cd0b101 to your computer and use it in GitHub Desktop.
Save adamzimmermann/251d0037840261160fe70b287cd0b101 to your computer and use it in GitHub Desktop.
Drupal Migrate class for creating image entities.
<?php
/**
* Migrate images into entities.
*/
class ExampleImageMigration extends Migration {
/**
* {@inheritdoc}
*/
public function __construct($arguments) {
parent::__construct($arguments);
$this->description = t('Creates image entities.');
// Set the source.
$this->source = new ExampleMigrateSourceImage();
// Set the destination.
$this->destination = new MigrateDestinationNode('image');
// Build a migration map.
$this->map = new MigrateSQLMap($this->machineName,
array(
'id' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => t('Image URL'),
),
),
MigrateDestinationNode::getKeySchema()
);
// Title.
$this->addFieldMapping('title', 'node_title');
// Credit.
$this->addFieldMapping('field_image_credit', 'credit');
$this->addFieldMapping('field_image_credit:format')
->defaultValue('filtered_html');
// Alt text.
$this->addFieldMapping('field_image_alt_text', 'alt');
// Title text.
$this->addFieldMapping('field_image_title_text', 'title');
// Image file.
$this->addFieldMapping('field_image_master', 'url');
$this->addFieldMapping('field_image_master:destination_file', 'filename');
$this->addFieldMapping('field_image_master:file_replace')
->defaultValue(FILE_EXISTS_RENAME);
$this->addFieldMapping('field_image_master:destination_dir')
->defaultValue('public://images');
// Status.
$this->addFieldMapping('status')
->defaultValue(NODE_PUBLISHED);
// Author.
$this->addFieldMapping('uid')
->defaultValue(1);
// Sticky.
$this->addFieldMapping('sticky')
->defaultValue(NODE_NOT_STICKY);
// Pathauto.
$this->addFieldMapping('pathauto')
->defaultValue(TRUE);
// Verify that all unmapped fields are set to DNM here.
}
/**
* {@inheritdoc}
*/
public function prepareRow($row) {
if (parent::prepareRow($row) === FALSE) {
return FALSE;
}
// Add optional fallback node title value logic here that would set the
// value for $row->node_title if it was empty.
// Add optional logic here to verify that we have a valid image URL.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment