Skip to content

Instantly share code, notes, and snippets.

@vincenzodibiaggio
Created January 25, 2016 10:18
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/a9c427c43e8f0f07e85f to your computer and use it in GitHub Desktop.
Save vincenzodibiaggio/a9c427c43e8f0f07e85f to your computer and use it in GitHub Desktop.
Drupal migrate content with field translation
id titolo_en titolo_it titolo_fr txt_breve txt_completo
1 Title 1 Titolo 1 Title 1 Short 1 Complete 1
2 Title 2 Titolo 2 Title 2 Short 2 Complete 2
<?php
/**
* @file
* Implements MyFixtures
*/
class CtTestResultsMigration extends MyFixtures {
/**
* Implement __construct.
*/
public function __construct(array $arguments) {
parent::__construct($arguments);
// Description.
$this->description = t('Migrate Ct Test Results from CSV to Ct Test Results');
// Mapping Table.
$this->map = new MigrateSQLMap(
$this->machineName,
array(
'id' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Ct Test Results id',
),
),
MigrateDestinationTerm::getKeySchema()
);
$source_file = drupal_get_path('module', 'my_migration') . '/data/ct_test_results.csv';
$columns = array(
array('id', 'id'),
array('titolo_en', 'titolo_en'),
array('titolo_it', 'titolo_it'),
array('titolo_fr', 'titolo_fr'),
array('txt_breve', 'txt_breve'),
array('txt_completo', 'txt_completo'),
);
$this->source = new MigrateSourceCSV(
$source_file,
$columns,
array(
'header_rows' => 1,
'delimiter' => ',',
'enclosure' => '"',
)
);
// Set destination.
$this->destination = new MigrateDestinationNode('quiz_textual_results');
$this->addFieldMapping('title_field', 'titolo_en');
$this->addFieldMapping('field_quiz_text_res_testo_breve', 'txt_breve');
$this->addFieldMapping('field_quiz_text_res_testo_breve:language')->defaultValue(array('en', 'it', 'fr', 'en-en'));
$this->addFieldMapping('field_quiz_text_res_testo_compl', 'txt_completo');
$this->addFieldMapping('field_quiz_text_res_testo_compl:language')->defaultValue(array('en', 'it', 'fr', 'en-en'));
$this->addFieldMapping('field_quiz_text_res_testo_compl:format')->defaultValue('filtered_html');
$this->addFieldMapping('uid')->defaultValue(1);
$this->addFieldMapping('language')->defaultValue('en');
$this->addFieldMapping('is_new')->defaultValue(TRUE);
$this->addUnmigratedDestinations(array());
}
public function prepareRow($row) {
$row->language = array('en', 'it', 'fr', 'en-en');
$row->txt_breve = array($row->txt_breve, $row->txt_breve, $row->txt_breve, $row->txt_breve);
$row->txt_completo = array($row->txt_completo, $row->txt_completo, $row->txt_completo, $row->txt_completo);
return TRUE;
}
/**
* Prepare fields.
*/
public function prepare($entity, stdClass $row) {
$entity->language = 'en';
$entity->title_field['en'][0]['value'] = $row->titolo_en;
$entity->title_field['en-en'][0]['value'] = $row->titolo_en;
$entity->title_field['it'][0]['value'] = $row->titolo_it;
$entity->title_field['fr'][0]['value'] = $row->titolo_fr;
$entity->translations = (object) array(
'original' => 'en',
'data' => array(
'en-en' => array(
'entity_type' => 'node',
'entity_id' => NULL,
'language' => 'en-en',
'source' => 'en',
'uid' => '1',
'status' => '1',
'translate' => '0',
),
'en' => array(
'entity_type' => 'node',
'entity_id' => NULL,
'language' => 'en',
'source' => '',
'uid' => '1',
'status' => '1',
'translate' => '0',
),
'it' => array(
'entity_type' => 'node',
'entity_id' => NULL,
'language' => 'it',
'source' => 'en',
'uid' => '1',
'status' => '1',
'translate' => '0',
),
'fr' => array(
'entity_type' => 'node',
'entity_id' => NULL,
'language' => 'fr',
'source' => 'en',
'uid' => '1',
'status' => '1',
'translate' => '0',
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment