My last blog post demonstrated the ability to retrieve and set values using the Entity Metadata Wrapper. Today, I’m going to follow that up by showing how to create and set values for field collections as well as taxonomy terms.

Field Collections:

Creating a field collection on the fly is done by using the “entity_create” function available from the entity module. Once the entity field collection object is created, associating that object to the node is done by using the "setHostEntity" function and passing the node you wish to associate to.
$new_node = node_load($new_node_id, NULL, TRUE);
$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_flights_simple_trips'));
$field_collection_item->setHostEntity('node', $new_node);
$field_collection_item->field_cc_transpo_main_simple[LANGUAGE_NONE][]['nid'] = $new_node_id;
Saving field collection data is done by grabbing the ID that is stored within the field value. However, calling "entity_metadata_wrapper" with the first parameter of "field_collection_item", will retrieve the entire field collection allowing you to manipulate it. In the example below, I set the phone number type (cell, office etc) and save the field collection.
$raw_collection = $node_data->field_phone_number->value();
$field_collection = entity_metadata_wrapper('field_collection_item', $raw_collection);


The way to create taxonomy terms using Entity API is very similar in creating field collections. The only code that changes is the parameter you pass into the "entity_create" function.
$field_collection_item = entity_create('taxonomy_term', array('field_name' => 'field_tags'));
$field_collection_item->setHostEntity('node', $node);
The old approach to retrieving a taxonomy term would involve loading up the taxonomy term by tid. However, with entity metadata wrapper, you can use the wrapper to set / get taxonomy term values much easier.
$wrapper = entity_metadata_wrapper('taxonomy_term', $tid);
$term = $wrapper->value();
$wrapper->value = "New Value";
These examples show how using the Entity API module and the methods it provides can make CRUD for entities much, much easier and less prone to errors and bugs. In case you missed it, read Part 1 here.
Read Next
Appnovation Blog Default Header

Fixed vs Estimated: Understanding the Methodology Triangles

08 March, 2013|12 min
Get in touch and let’s get started.

We are a full service digital consultancy with experience and capacity to meet the needs of even the largest most complex of organizations in the world. Our services portfolio enables us to offer clients the best of experiences when working with our teams so as to make sure we keep the focus on their needs, customers and delivering tangible value to the business. End to end services; endless ideas. 

Three easy ways to get in touch. Complete the form to request a consultation, email us your inquiry, specs and/or RFP to OR call one of our many, global office locations and tell us how we can help.