Created
March 13, 2014 20:39
-
-
Save jmk1ng/9536525 to your computer and use it in GitHub Desktop.
Drupal Data Import Skelleton
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 | |
error_reporting(E_ALL); | |
set_time_limit(3600); | |
require_once 'includes/bootstrap.inc'; // Include the Drupal bootstrap include file | |
define('DRUPAL_ROOT', getcwd()); // Set the web root directory | |
drupal_bootstrap(7); // Full Drupal Bootstrap level https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_bootstrap/7 | |
$source_data = array(); | |
/*... | |
Load your data in from your source. Either from another database, or from a flat file | |
... */ | |
$row = 0; | |
foreach($source_data as $row) { | |
$row++; | |
// Set some defaults | |
$node = (object)array( | |
'type' => NODE_TYPE_HERE, | |
'language' => LANGUAGE_NONE, // or a language code as applicable | |
'status' => 1, | |
'comment' => 0, | |
'promote' => 0, | |
'uid' => 1, | |
); | |
// Runs prepare hooks on node, and sets things like date and revision | |
node_object_prepare($node); | |
// Map data | |
$node->field_field_name_A[$node->language][0]['value'] = $source_data['field_name_A']; | |
$node->field_field_name_B[$node->language][0]['value'] = $source_data['field_name_B']; | |
$node->field_term_reference[$node->language]['0']['tid'] = $term_map[$source_data['term_name']]; | |
/*... | |
Continue mapping, doing any cleansing/reformatting as necessary | |
... */ | |
try { | |
// Try to save | |
node_save($node); | |
} catch (Exception $e) { | |
// Output helpful debugging information in event of failure | |
echo '<pre>EXCEPTION: '.$e->getMessage())."\n" | |
.'Error processing row #'.$row.'</pre>'; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment