Skip to content

Instantly share code, notes, and snippets.

@chadmandoo
Last active December 20, 2015 02:09
Show Gist options
  • Save chadmandoo/6054477 to your computer and use it in GitHub Desktop.
Save chadmandoo/6054477 to your computer and use it in GitHub Desktop.
Programmatically create an instance of a node using the Entity API/Entity Metadata Wrapper
<?php
//Load instance of entity
$node = entity_create('node', array('type' => 'content_type'));
$entity = entity_metadata_wrapper('node',$node);
//Set properties
$entity->author = $user->uid; // Author
$entity->title->set("String"); //Title
$entity->field_name->set("String"); //Textfield
$entity->body->set(array('value' => "String or HTML")); //Body
//File field
$entity->field_file[0] = array('fid' => $file->fid, 'display' => FALSE);
$entity->field_file[0]->description = 'foo';
$entity->field_file[0]->display = TRUE;
$entity->field_file[1]->file = $file;
//Images - assuming you have access to a $file object
$entity->field_image = array('fid' => $file->fid);
$entity->field_image->alt = 'foo';
$entity->field_image->file = $file;
$entity->field_image = array();
//Save
$entity->save(); //Save new instance
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment