Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Using the Entity API module makes this fairly straightforward. Let's say there's an entity type called "Contact" [contact] and a bundle called 'contact_simple'.
Creating new entities is as easy as:
$entity_type = 'contact';
$entity = entity_create($entity_type, array('type' => 'contact_simple'));
$wrapper = entity_metadata_wrapper($entity_type, $entity);
Once the entity is created the properties can be changed:
$wrapper->uid = 9;
And any attached field:
$wrapper->field_full_name->set("John doe");
Alternatively, field values can be set directly as the EntityMetadataWrapper class (provided by EntityAPI) implements PHP's magic __set() method. For example:
$wrapper->field_full_name = "John doe";
Just remember to save it when all changes are made:
$wrapper->save();
That's it!