-
-
Save dwaghmare/4affade502716626f7a6 to your computer and use it in GitHub Desktop.
a simple json file migration for the drupal 7 (maybe 6?) migrate module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"title": "this is a title", | |
"content": "this is a body", | |
"date": "2009-01-01", | |
"url":"http://oldsite.com/123/test", | |
"image": "test.jpg", | |
"id": 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class JSONMigration extends Migration { | |
/** | |
* A constructor | |
*/ | |
public function __construct() { | |
parent::__construct(MigrateGroup::getInstance('json_migration')); | |
// list of the file ids [1,2,3,4,5,6] | |
$list_url = 'http://localhost/json/list.json'; | |
//pattern where the files are at | |
$item_url = 'http://localhost/json/:id.json'; | |
//no options, or you could specify some | |
$http_options = array(); | |
//map for the migration | |
$this->map = new MigrateSQLMap($this->machineName, | |
array( | |
'muid' => array( | |
'type' => 'int', | |
'not null' => true, | |
), | |
), | |
MigrateDestinationNode::getKeySchema() | |
); | |
$this->source = new MigrateSourceList( | |
new MigrateListJSON($list_url), | |
new MigrateItemJSON($item_url, $http_options), | |
$this->fields() | |
); | |
//destination node type is json (because this is an example) | |
$this->destination = new MigrateDestinationNode('json'); | |
//map node's title field to the title in the json content | |
$this->addFieldMapping('title', 'title'); | |
//map node's body field to the content in the json content | |
$this->addFieldMapping('body', 'content'); | |
//map node's field_id field to the id in the json content | |
$this->addFieldMapping('field_id', 'id'); | |
//map node's field_name field to the title in the json content | |
$this->addFieldMapping('field_name', 'title'); | |
//map node's created field to the date in the json content | |
$this->addFieldMapping('created', 'date'); | |
//apparently you can do stuff like this: | |
$this->addFieldMapping('comment', null)->defaultValue(COMMENT_NODE_CLOSED); | |
} | |
/** | |
* Return the fields (this is cleaner than passing in the array in the MigrateSourceList class above) | |
* @return array | |
*/ | |
function fields() { | |
return array( | |
'title' => 'The title of the content', | |
'content' => 'The body of the content', | |
'date' => 'Date associated with the content', | |
'url' => 'Source url associated with the content', | |
'image' => 'Image Source url associated with the content', | |
'id' => 'Source ID associated with the content', | |
); | |
} | |
/** | |
* Remap fields as needed | |
* @param type $row | |
*/ | |
function prepareRow($row) { | |
//rewrite the date as a timestamp | |
list($year, $month, $day) = explode('-', $row->date); | |
$row->date = mktime(0, 0, 0, $month, $day, $year); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name = "JSON MIGRATION" | |
description = "Module to migrate content from old site to Drupal 7 (json format)" | |
package = "Development" | |
core = 7.x | |
dependencies[] = migrate | |
dependencies[] = field | |
dependencies[] = file | |
dependencies[] = image | |
dependencies[] = number | |
dependencies[] = text | |
files[] = json_migration.module | |
files[] = json.migration.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* You must implement hook_migrate_api(), setting the API level to 2, for | |
* your migration classes to be recognized by the Migrate module. | |
*/ | |
function json_migration_migrate_api() { | |
$api = array( | |
'api' => 2, | |
); | |
return $api; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1,2,3,4,5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment