Last active
December 18, 2015 09:49
-
-
Save level09/5764423 to your computer and use it in GitHub Desktop.
Drupal Create node Programmatically
This file contains hidden or 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 | |
/* | |
* $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