Skip to content

Instantly share code, notes, and snippets.

@level09
Last active December 18, 2015 09:49
Show Gist options
  • Save level09/5764423 to your computer and use it in GitHub Desktop.
Save level09/5764423 to your computer and use it in GitHub Desktop.
Drupal Create node Programmatically
<?php
/*
* $custom_fields is array of format:
*
* array(
* array(
* 'field'=>'location',
* 'value'=> 'dubai',
* ),
* array(
* 'field'=>'price',
* 'value'=> '239',
* ),
* );
*/
function core_save_node($type='article',$title, $body=null, $custom_fields=array()) {
$node = new stdClass();
$node->type = $type;
node_object_prepare($node);
$node->uid = 1;
$node->title = $title;
$node->language = LANGUAGE_NONE;
$node->body[$node->language][0]['value'] = $body;
#$node->body[$node->language][0]['summary'] = text_summary($body);
$node->body[$node->language][0]['format'] = 'filtered_html';
if($custom_fields && count($custom_fields>0)){
foreach ($custom_fields as $field){
$fname = 'field_'.$field->name;
$node->{$fname}['und'][0]['value'] = "$field->value";
}
}
node_save($node);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment