Skip to content

Instantly share code, notes, and snippets.

@SeanJA
Created June 18, 2012 04:17
Show Gist options
  • Save SeanJA/2946818 to your computer and use it in GitHub Desktop.
Save SeanJA/2946818 to your computer and use it in GitHub Desktop.
a simple json file migration for the drupal 7 (maybe 6?) migrate module
{
"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
}
<?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);
}
}
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
<?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;
}
[1,2,3,4,5]
@danylevskyi
Copy link

This is great! Thanks!

@basilbaby6
Copy link

i enable this module and run in http://localhost/drupal7/admin/content/migrate i got the error like this let meknow the reason
(I am working in D7)

MigrateException: No migration found with machine name JSON in MigrationBase::getInstance() (line 444 of C:\xampp\htdocs\drupal7\sites\all\modules\contrib\migrate\includes\base.inc)

@basilbaby6
Copy link

any idea for a complex json url migration?

@SeanJA
Copy link
Author

SeanJA commented Apr 12, 2013

@danylevskyi Glad it worked for you!

@basilbaby6 You probably need the drupal migration plugin for json, I didn't actually end up doing anything more complex with this... sorry

@timodwhit
Copy link

Thank you for this! should link to it on d.o

@Siva-1992
Copy link

Hi,

For single node migration it's fine but how can I use this for multiple node migration, please advice me.

@Yusadolat
Copy link

Thanks A lot bro it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment