You are here

function entity_property_values_create_entity in Entity API 7

Creates the entity object for an array of given property values.

Parameters

$entity_type: The entity type to create an entity for.

$values: An array of values as described by the entity's property info. All entity properties of the given entity type that are marked as required, must be present. If the passed values have no matching property, their value will be assigned to the entity directly, without the use of the metadata-wrapper property.

Return value

EntityDrupalWrapper|false An EntityDrupalWrapper wrapping the newly created entity or FALSE, if there were no information how to create the entity.

File

includes/entity.property.inc, line 317
Provides API functions around hook_entity_property_info(). Also see entity.info.inc, which cares for providing entity property info for all core entity types.

Code

function entity_property_values_create_entity($entity_type, $values = array()) {
  if (entity_type_supports($entity_type, 'create')) {
    $info = entity_get_info($entity_type);

    // Create the initial entity by passing the values for all 'entity keys'
    // to entity_create().
    $entity_keys = array_filter($info['entity keys']);
    $creation_values = array_intersect_key($values, array_flip($entity_keys));

    // In case the bundle key does not match the property that sets it, ensure
    // the bundle key is initialized somehow, so entity_extract_ids()
    // does not bail out during wrapper creation.
    if (!empty($info['entity keys']['bundle'])) {
      $creation_values += array(
        $info['entity keys']['bundle'] => FALSE,
      );
    }
    $entity = entity_create($entity_type, $creation_values);

    // Now set the remaining values using the wrapper.
    $wrapper = entity_metadata_wrapper($entity_type, $entity);
    foreach ($values as $key => $value) {
      if (!in_array($key, $info['entity keys'])) {
        if (isset($wrapper->{$key})) {
          $wrapper->{$key}
            ->set($value);
        }
        else {
          $entity->{$key} = $value;
        }
      }
    }

    // @todo Once we require Drupal 7.7 or later, verify the entity has
    // now a valid bundle and throw the EntityMalformedException if not.
    return $wrapper;
  }
  return FALSE;
}