Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create nodes using node_save?

I'm trying to migrate my current html site into drupal. I have over 80,000 pages I have to migrate so I thought instead of sitting infront of a computer for 50 years I would create a module. I was able to create a script that extracts the html from each directory and now I got to a road block where I need to create a node. I'm trying to create a new node using node_save(), but when node_save is executed, I get a PDOException error with everything I try. I'm passing in $node, which is an array which is then casted into an object.

PDOException: in field_sql_storage_field_storage_write() (line 424 of /srv/www/htdocs/modules/field/modules/field_sql_storage/field_sql_storage.module).

This is how we are currently creating the node, but it produces an error:

$node= array(
    'uid' => $user->uid,
    'name' => $user->name,
    'type' => 'page',
    'language' => LANGUAGE_NONE,
    'title' => $html['title'],
    'status' => 1,
    'promote' => 0,
    'sticky' => 0,
    'created' => (int)REQUEST_TIME,
    'revision' => 0,
    'comment' => '1',
    'menu' => array(
        'enabled' => 0,
        'mlid' => 0,
        'module' => 'menu',
        'hidden' => 0,
        'has_children' => 0,
        'customized' => 0,
        'options' => array(),
        'expanded' => 0,
        'parent_depth_limit' => 8,
        'link_title' => '',
        'description' => '',
        'parent' => 'main-menu:0',
        'weight' => '0',
        'plid' => '0',
        'menu_name' => 'main-menu',
    ),
    'path' => array(
        'alias' => '',
        'pid' => null,
        'source' => null,
        'language' => LANGUAGE_NONE,
        'pathauto' => 1,
    ),
    'nid' => null,
    'vid' => null,
    'changed' => '',
    'additional_settings__active_tab' => 'edit-menu',
    'log' => '',
    'date' => '',
    'submit' => 'Save',
    'preview' => 'Preview',
    'private' => 0,
    'op' => 'Save',
    'body' => array(LANGUAGE_NONE => array(
        array(
            'value' => $html['html'],
            'summary' => $link,
            'format' => 'full_html',
        ),
    )),
        'validated' => true,
);

node_save((object)$node);

// Small hack to link revisions to our test user.
db_update('node_revision')
    ->fields(array('uid' => $node->uid))
    ->condition('vid', $node->vid)
    ->execute();
like image 289
samwell Avatar asked Jun 26 '26 03:06

samwell


1 Answers

Usually I create a bulkimport.php script in the document root to do this kind of thing.

Here is the code I use for Drupal 7:

<?php

define('DRUPAL_ROOT', getcwd());

include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);



function _create_node($title="", $body="", $language="und") {

  $node = (object)array();

  $node->uid      = '1';
  $node->name     = 'admin';

  $node->type     = 'page';

  $node->status   = 1;
  $node->promote  = 0;
  $node->sticky   = 0;
  $node->revision = 1;
  $node->language = $language;

  $node->title    = $title;
  $node->body[$language][0] = array(
    'value'  => $body,
    'format' => 'full_html',
    );

  $node->teaser   = '';
  $node->log      = 'Auto Imported Node';

  node_submit($node); 
  node_save($node);

  return $node;

  } ### function _create_node

$sith = array(
  'Darth Vader'    => 'Master: Darth Sidious',
  'Darth Sidious'  => 'Master: Darth Plagous',
  'Darth Maul'     => 'Master: Darth Sidious',
  'Darth Tyranous' => 'Master: Darth Sidious',
  );

foreach($sith as $title=>$body) {
  print "Creating Node. Title:[".$title."] \tBody:[".$body."] ";
  $node = _create_node($title, $body);
  print "\t... Created Node ID: [".$node->nid."]\n";
  #print_r($node);
  } ### foreach
like image 102
Devon Hubner Avatar answered Jun 27 '26 17:06

Devon Hubner