Skip to content

Instantly share code, notes, and snippets.

@batandwa
Created August 19, 2014 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save batandwa/3ccfe0421561ffc6f436 to your computer and use it in GitHub Desktop.
Save batandwa/3ccfe0421561ffc6f436 to your computer and use it in GitHub Desktop.
Drupal 7 Entities - Create an entity using the entity_metadata_wrapper class.
<?php
// The entity and bundle we are working with
$entity_type = "node";
$bundle = "node";
// Instantiate the wrapper for the bundle
$entity = entity_create($entity_type, array('type' => $bundle));
$wrapper = entity_metadata_wrapper($entity_type, $entity);
// Check what fields are available to us.
dsm($wrapper->getPropertyInfo(), 'Available properties for ' . $bundle);
// Update the field values.
$wrapper->field_a_custom_field_in_our_bundle->set($code);
// Setting a multi-field for entity reference
$wrapper->field_entity_reference_field->set($code);
$wrapper->save();
return $entity->nid;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment