Last updated November 27, 2013. Created by capynet on June 3, 2013.
Edited by oadaeh, pandaPowder, nerdcore. Log in to edit this page.

Lets say you created an entity type called "Contact" [contact] and a bundle called "contact_simple".

Creating new entities is easy as:

<?php
  $entity_type
= "contact";
 
$entity = entity_create($entity_type, array('type' => "contact_simple"));
 
$wrapper = entity_metadata_wrapper($entity_type, $entity);
?>

Once the entity is created you can change its properties:

<?php
$wrapper
->uid = 9;
?>

And any attached field:

<?php
$wrapper
->field_full_name->set("John doe");
?>

That's it. Just remember save it:

<?php
$wrapper
->save();
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Thank you, thank you! Now my module doesn't need it's own schema and I can harness all the exciting stuff entities has to offer such as views integration and pagination and caching etc...