Skip to content

Instantly share code, notes, and snippets.

@selwynpolit
Created May 6, 2016 17:28
Show Gist options
  • Save selwynpolit/027052cba274375db3db9fbe94c58650 to your computer and use it in GitHub Desktop.
Save selwynpolit/027052cba274375db3db9fbe94c58650 to your computer and use it in GitHub Desktop.
/**
* Constructor.
*
* A Migration constructor takes an array of arguments as its
* first parameter. The arguments must be passed through to the parent
* constructor.
*
* @param array $arguments
* Arguments
*/
public function __construct($arguments) {
parent::__construct($arguments);
$this->team = array(
new MigrateTeamMember('Selwxxx', 'selxxx@zzz.com',
t('Developer')),
new MigrateTeamMember('Tomxxx', 'toxxx@zzz.com',
t('Developer')),
);
// Individual mappings in a migration can be linked to a ticket or issue
// in an external tracking system. Define the URL pattern here in the shared
// class with ':id:' representing the position of the issue number, then add
// ->issueNumber(1234) to a mapping.
$this->issuePattern = 'https://digitalandweb.atlassian.net/browse/:id:';
$this->description = t('Migrate recipes from zzz SAGE api v2.');
// Manually define source fields.
$this->fieldsArray = array(
'id' => 'ID number for each recipe',
'_id' => 'Mongo ID for each recipe',
'title' => 'Title of recipe',
'modified_at' => 'Datetime recipe was last modified',
'modified_at2' => 'Datetime recipe was last modified',
'description' => 'Description',
'comment_count' => 'Number of comments',
'number_of_ratings' => 'Number of ratings',
'created_at' => 'Created',
'rating' => 'Rating',
'weighted_rating' => 'Weighted Rating',
'prep_time' => 'Preparation time',
'cook_time' => 'Cooking time',
'serves' => 'Serves',
'ingredients' => 'Ingredients',
'directions' => 'Directions',
'photos' => 'Photographs',
'special_diet' => 'Special Diet Flag',
'type_dish' => 'Type of dish',
'cuisine' => 'Cuisine',
'occasion' => 'Occasion',
'category' => 'Category',
'categories' => 'Categories',
'main_ingredient' => 'Main Ingredient in dish',
'cloudinary_images' => 'Cloudinary Images',
'image_filename' => 'Image Filename',
'field_hero_image' => 'Main image for the recipe',
'image_alt_text' => 'Image title text',
'image_title_text' => 'Image title text',
);
// Hash all source rows and compare to hash in migrate_map_recipev2
// table to see if anything has changed.
$source_options = array(
'track_changes' => 1,
);
$migrate_list = new RecipeList();
$this->recipeAPI = $migrate_list->getrecipeAPI();
$this->migrateItem = new RecipeItem($this->recipeAPI);
$migrate_source_list = new MigrateSourceList($migrate_list, $this->migrateItem, $this->fieldsArray, $source_options);
$this->source = $migrate_source_list;
// Set up destination - nodes of type recipe.
$this->destination = new MigrateDestinationNode('recipe');
/* Create a map object for tracking the relationships
between source rows and their resulting Drupal objects.
mongo id <-> recipe nid
*/
$map_options = array(
// Create source object.
'track_last_imported' => TRUE,
);
$this->map = new MigrateSQLMap(
$this->machineName,
array(
'_id' => array(
'type' => 'varchar',
'length' => 255,
'description' => 'Sage API mongo DB id',
'not null' => TRUE,
),
),
MigrateDestinationNode::getKeySchema(),
'default',
$map_options
);
// Field Mappings.
$this->addFieldMapping('field_rid', 'id');
$this->addFieldMapping('field_mongo_id', '_id');
$this->addFieldMapping('title', 'title');
$this->addFieldMapping('body', 'description');
$this->addFieldMapping('body:format')
->defaultValue('full_html');
//$this->addFieldMapping('field_recipe_created_time', 'created_at');
$this->addFieldMapping('field_recipe_rating', 'weighted_rating');
$this->addFieldMapping('field_prep_time', 'prep_time');
$this->addFieldMapping('field_cook_time', 'cook_time');
$this->addFieldMapping('field_serves', 'serves');
$this->addFieldMapping('field_recipe_directions', 'directions');
$this->addFieldMapping('field_recipe_directions:format')
->defaultValue('full_html');
$this->addFieldMapping('field_recipe_course', 'type_dish');
$this->addFieldMapping('field_recipe_cuisine', 'cuisine');
$this->addFieldMapping('field_recipe_occasions', 'occasion');
$this->addFieldMapping('field_recipe_category', 'category');
$this->addFieldMapping('field_recipe_main_ingredient', 'main_ingredient');
//$this->addFieldMapping('field_recipe_updated_time', 'modified_at');
//$this->addFieldMapping('field_recipe_source_modified', 'modified_at2');
$this->addFieldMapping('field_special_diet', 'special_diet');
// Images.
$this->addFieldMapping('field_hero_image', 'field_hero_image');
$this->addFieldMapping('field_hero_image:file_class')
->defaultValue('MigrateFileUri');
$this->addFieldMapping('field_hero_image:destination_dir')
->defaultValue('public://media');
$this->addFieldMapping('field_hero_image:destination_file', 'image_filename');
$this->addFieldMapping('field_hero_image:file_replace')
->defaultValue(MigrateFile::FILE_EXISTS_REUSE);
$this->addFieldMapping('field_hero_image:preserve_files')
->defaultValue(TRUE);
$this->addFieldMapping('field_hero_image:alt', 'image_alt_text');
$this->addFieldMapping('field_hero_image:title', 'image_title_text');
$this->addFieldMapping('field_ingredients', 'ingredients');
$this->addFieldMapping('field_ingredients:format')
->defaultValue('full_html');
$this->addFieldMapping('field_nutritional_info', 'basic_nutrition');
$this->addFieldMapping('field_nutritional_info:format')
->defaultValue('full_html');
$this->addFieldMapping('locations_acl')
->defaultValue("WFM")
->description(t('Do we like global access?'));
$this->addFieldMapping('uid')
->defaultValue(0)
->description(t('Do we still want to leave this as anon?'));
$this->addUnmigratedSources(array(
'rating',
'categories',
'cloudinary_images',
'comment_count',
'number_of_ratings',
'photos',
));
$this->addUnmigratedDestinations(array(
'created',
'changed',
'status',
'promote',
'sticky',
'revision',
'log',
'language',
'tnid',
'translate',
'revision_uid',
'body:summary',
'field_hero_image:source_dir',
'field_hero_image:urlencode',
'field_ingredients:summary',
'field_is_featured',
'field_marquee_image',
'field_marquee_image:file_class',
'field_marquee_image:preserve_files',
'is_new',
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment