You are here

public function MigrateExampleMediaImageMigration::__construct in Migrate Extras 7.2

General initialization of a Migration object.

Overrides Migration::__construct

File

migrate_extras_examples/migrate_extras_media/migrate_extras_media.migrate.inc, line 11
Examples and test fodder for migration of Media entities and fields.

Class

MigrateExampleMediaImageMigration
Migration class for media images.

Code

public function __construct() {
  parent::__construct();
  $this->description = t('Example migration of media images');
  $this->map = new MigrateSQLMap($this->machineName, array(
    'filename' => array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'description' => 'Image filename',
    ),
  ), MigrateDestinationMedia::getKeySchema());

  // Source fields available in the XML file.
  $fields = array(
    'filename' => t('Image filename, relative to the source directory'),
    'description' => t('Description of the image'),
  );

  // Our test data is in an XML file
  $xml_folder = drupal_get_path('module', 'migrate_extras_media');
  $items_url = $xml_folder . '/migrate_extras_media.xml';
  $item_xpath = '/source_data/item/image';
  $item_ID_xpath = 'filename';
  $items_class = new MigrateItemsXML($items_url, $item_xpath, $item_ID_xpath);
  $this->source = new MigrateSourceMultiItems($items_class, $fields);

  // In the simplest case, just pass the media type.
  $this->destination = new MigrateDestinationMedia('image');

  // The source images are in a local directory - specify the parent.
  $this
    ->addFieldMapping('source_dir')
    ->defaultValue(drupal_get_path('module', 'migrate_extras_media') . '/source_files');

  // The 'value' of the media destination is mapped to the source field
  // representing the media itself - in this case, a filename relative to
  // source_dir.
  $this
    ->addFieldMapping('value', 'filename')
    ->xpath('filename');

  // Fields on the entity can be mapped in the usual way.
  $this
    ->addFieldMapping('field_image_description', 'description')
    ->xpath('description');
  $this
    ->addFieldMapping('uid')
    ->defaultValue(1);
  $this
    ->addUnmigratedDestinations(array(
    'field_image_description:format',
    'field_image_description:language',
    'destination_dir',
    'destination_file',
    'file_replace',
    'preserve_files',
    'timestamp',
  ));
  if (module_exists('path')) {
    $this
      ->addUnmigratedDestinations(array(
      'path',
    ));
  }
}