Created
August 19, 2014 14:27
-
-
Save batandwa/3ccfe0421561ffc6f436 to your computer and use it in GitHub Desktop.
Drupal 7 Entities - Create an entity using the entity_metadata_wrapper class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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