I created a custom content type Car with cck, and would like to be able to post/create a Car node by using php script directly outside of Drupal, not from the web form created for this content type by the module. is it allowed? if so, can someone provide me some sample code? I have seen someone doing this within a new module, but not complete outside of the Drupal environment itself.

Comments

zbricoleur’s picture

My approach was to install Devel module and use it to see the queries that were run during node creation for the particular content type. Then I created a script that ran those INSERTs and UPDATEs, pulling the appropriate data from another file or database.

sapark’s picture

I have imported from a csv file with this.

/***********************************************************
 * Drupal 6
 * Creates nodes from a comma separated values (CSV) file of CCK, Link and Taxonomy fields.
 * Place both this file and the CSV file in the root of the Drupal site.
 * Run from browser at example.com/import.php; you will get a white screen,
 * click back button to see results.
 * Install Devel module to get names of your fields; Devel module instructions and
 * source of code: acquia.com/blog/migrating-drupal-way-part-i-creating-node
 * Also source of code: drupal.org/node/67887
 ***********************************************************/

// Bootstrap Drupal
require 'includes/bootstrap.inc';        // Basic include
require 'modules/node/node.pages.inc';   // For node_object_prepare
require 'includes/common.inc';           // For fgetcsv, fopen, fclose
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// CSV file containing the data to import.
// For simplicity I place this in the root of the Drupal
// site and remove it when done.
$filename = 'import.csv';

$fp = fopen($filename, "r");   // Open the CSV file read only.

while ($line = fgetcsv($fp)) { // Read a line of the file and create a node from it.
  $node = new stdClass();      // Construct the new node object.
  $node->type = 'document';    // Your specified content type.
  node_object_prepare($node);  // Fill in default values for uid, status, promote,
                               // sticky, created, changed, and revision properties.

  // CCK and Links module fields, mapped to CSV file data.
  $node->field_date[0]['value']         = ($line[0] . "T:00:00:00");   // CCK date field
//  $node->field_summary[0]['value']      = $line[10];  // CCK text field
  $node->field_issues[0]['value']       = $line[3];     // CCK text field
//  $node->field_volume[0]['value']       = $line[2];   // CCK text field
  $node->field_oclc[0]['value']         = $line[1];     // CCK text field
  $node->field_pubresource[0]['value']  = $line[9];     // CCK text field
  $node->field_agency1[0]['value']      = $line[5];     // CCK text field
  $node->field_agency2[0]['value']      = $line[6];     // CCK text field
  $node->field_related[0]['url']        = $line[12];    // Link module
  $node->field_related[0]['title']      = $line[13];    // Link module
  $node->field_related[0]['attributes'] = $line[14];    // Link module
  $node->field_related[1]['url']        = $line[15];    // Link module
  $node->field_related[1]['title']      = $line[16];    // Link module
  $node->field_related[1]['attributes'] = $line[17];    // Link module
  $node->field_related[2]['url']        = $line[18];    // Link module
  $node->field_related[2]['title']      = $line[19];    // Link module
  $node->field_related[2]['attributes'] = $line[20];    // Link module
  $node->field_related[3]['url']        = $line[21];    // Link module
  $node->field_related[3]['title']      = $line[22];    // Link module
  $node->field_related[3]['attributes'] = $line[23];    // Link module
  $node->field_related[4]['url']        = $line[24];    // Link module
  $node->field_related[4]['title']      = $line[25];    // Link module
  $node->field_related[4]['attributes'] = $line[26];    // Link module

  // If known, the taxonomy TID values can be added as an array.
  // You will need to import Taxonomy vocabularies and terms separately before this to know TID's.
  $node->taxonomy = array($line[8],$line[7],$line[4]);  // Term1, Term2, Term3

  // CCK Filefield
  $mime = 'application/pdf';
  $file_drupal_path = $line[11];

  $file = new stdClass();       // Construct the new file object.
  $file->filename = basename($file_drupal_path);
  $file->filepath = $file_drupal_path;
  $file->filemime = $mime;
  $file->filesize = filesize($file_drupal_path);

  $file->uid = 1;
  $file->status = FILE_STATUS_PERMANENT;
  $file->timestamp = time();
  drupal_write_record('files', $file);
  $file->fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", $file->filepath));

  $node->field_files = array(
    array(
      'fid' => $file->fid,
      'list' => 1,
      'filename' => $file->filename,
      'filepath' => $file->filepath,
      'filesize' => $file->filesize,
      'mimetype' => $mime,
      //'description' => basename($file->filename),
    ),
  );

  // Importing data by hand
  /*
  $node->field_date[0]['value'] = "2007-03-03T00:00:00";
  $node->field_summary[0]['value'] = "My summary.";
  $node->field_issues[0]['value'] = "20070701";
  $node->field_oclc[0]['value'] = "35056107";
  $node->field_related[0]['url'] = "http://www.example.com";
  $node->field_related[0]['title'] = "My title";
  $node->field_related[0]['attributes'] = "N;";
  $node->field_related[1]['url'] = "http://www.example.com";
  $node->field_related[1]['title'] = "My title";
  $node->field_related[1]['attributes'] = "N;";
  // If known, the taxonomy TID values can be added as an array.
  $node->taxonomy = array(24,82,137);                 // Term1, Term2, Term3
  */

  // Optional settings
  //$node->title = "My imported node";
  //$node->body = "The body of my imported node.\n\nAdditional Information";
  //$node->created = time();          // Current date and time
  //$node->changed = $node->created;  // Also current date and time
  //$node->status = 1;                // Published
  //$node->promote = 0;               // Not promoted to front page
  //$node->sticky = 0;                // Not stickied at top of lists
  //$node->format = 1;                // Filtered HTML
  //$node->uid = 1;                   // User ID of content owner, 1 is admin
  //$node->language = 'en';           // English language

  node_save($node);   // Save node, also bypass required fields.
}
fclose ($fp);         // Close CSV file.
josepvalls’s picture

Hi,

I'm trying to create nodes programatically. I tried your code and works for some simple nodes, but in a content type I have a mandatory cck nodereference field and whenever I try to create a node of that type I get the following error:
Fatal error: Call to undefined function userreference_content_is_empty() in C:\wamp\www\drupal\sites\all\modules\cck\content.module on line 906
I've been reading around and there is people that suggest to use drupal_execute instead of node_save but I can't manage to get anything created using drupal_execute .
Anyone could point me on how to get those nodereference fields filled? Maybe someone could address me to some code for creating nodes using drupal_execute?

//works
		$node = new StdClass();
		node_object_prepare($node);
		$node->type = 'blog';
		$node->status = 1;
		$node->title = 'guardado';
		node_save($node);
//error
		$node = new StdClass();
		node_object_prepare($node);
		$node->type = 'aplicacion';
		$node->status = 1;
		$node->title = 'guardado';
		#$node->field_aplicacion_cv[0]['nid'] = 123;
		node_save($node);
//doesn't error, but doesn't do anything
	$node = new StdClass();
	$node->type='blog';
	$node->title='blog';
	node_object_prepare($node);
	$values = array();
	$values['field_tipo'][0]['value'] = 2;
	$values['title'] = "test";
	$values['status'] = 1;
	drupal_execute('blog_node_form', $values, $node);
//this either, i found it somewhere
	$node = array('type' => 'blog');
	$values = array();
	$values['field_tipo'][0]['value'] = 2;
	$values['title'] = "test";
	$values['status'] = 1;
	drupal_execute('blog_node_form', $values, $node);
sapark’s picture

I think you could do

$node->field_aplicacion_cv[0][nid] = 123;

without the quotes.

josepvalls’s picture

The quotes didn't change anything.
Actually the root of the problem was some module dependencies. I could only figure it out when I tried the drupal_execute approach.
For it to work, I found that you need to add
$values['values']['op'] = t('Save');
I'll leave this comment here just for the sake of documentation. The difference with the drupal_execute approach is that it will carry all the extra validation and module hooks involved whereas node_save won't. For bulk importing routines one may not want the checks to be applied and therefore the node_save approach would be better.

Tanks for your prompt reply anyway.