Skip to content

Instantly share code, notes, and snippets.

@vincenzodibiaggio
Created August 7, 2013 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vincenzodibiaggio/6170508 to your computer and use it in GitHub Desktop.
Save vincenzodibiaggio/6170508 to your computer and use it in GitHub Desktop.
Drupal - Migration - Add term reference, included in a field collection, to an entity from CSV data
We can make this file beautiful and searchable if this error is corrected: It looks like row 3 should actually have 3 columns, instead of 1. in line 2.
"id","user","language_name"
1,"username","English"
....
....
class MyMigration extends MyBaseMigration {
public function __construct(array $arguments) {
parent::__construct($arguments);
// Description.
$this->description = t('Description');
// Dependencies.
$this->dependencies = array('MyUser');
// Mapping Table.
$this->map = new MigrateSQLMap(
$this->machineName,
array(
'id' => array(
'type' => 'int',
'not null' => TRUE,
'description' => 'Source id'
),
),
MigrateDestinationFieldCollection::getKeySchema()
);
$source_file = drupal_get_path('module', 'my_module') . '/data/my_data.csv';
$columns = array(
// [..]
array('language_name', 'language_name'),
);
$this->source = new MigrateSourceCSV(
$source_file,
$columns,
array(
'header_rows' => 1,
'delimiter' => ',',
'enclosure' => '"'
)
);
$this->destination = new MigrateDestinationFieldCollection(
'my_field_collection_name',
array('host_entity_type' => 'my_entity')
);
$this->addFieldMapping('host_entity_id', 'my_entity')->sourceMigration('MyClassThatImportTheEntity');
$this->addFieldMapping('term_reference_field', 'language_name');
$this->addFieldMapping('term_reference_field:source_type')->defaultValue('term_name');
$this->addFieldMapping('term_reference_field:create_term')->defaultValue(TRUE);
$this->addFieldMapping('term_reference_field:ignore_case')->defaultValue(TRUE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment