---
title: "Part 2: Entity Metadata Wrappers and Entity API · Appnovation"
url: "https://www.appnovation.com/insights/part-2-entity-metadata-wrappers-and-entity-api"
description: "Part 2: use Drupal's Entity Metadata Wrapper and Entity API to create field collections and set taxonomy term values."
---

Article

# Part 2: Entity Metadata Wrappers and Entity API

[![](https://cdn.sanity.io/images/ey850001/production/36eb314260fb474cacc1b3a473d7e8402d997898-68x68.png?w=68&auto=format) Appno Blogger Appnovation Coop](/people/appno-blogger.md) Feb 25, 2013  
 2 min read 

![Appnovation Blog Default Header](https://cdn.sanity.io/images/ey850001/production/9efe1c26d9584ecfb3dfef5a63e698bf4567d305-1440x600.jpg?w=1400&auto=format) 

On this page

## On this page

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; $field\_collection\_item->save(); 

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); $field\_collection->phone\_number\_type->set($new\_value); $field\_collection->save(); 

## Taxonomy:

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); $field\_collection\_item->save(); 

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"; $wrapper->save(); 

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](/insights/part-2-entity-metadata-wrappers-and-entity-api.md).

[![](https://cdn.sanity.io/images/ey850001/production/36eb314260fb474cacc1b3a473d7e8402d997898-68x68.png?w=68&auto=format) Appno Blogger Appnovation Coop · Appnovation](/people/appno-blogger.md) 

## Keep reading

[![What is Sovereign AI](https://cdn.sanity.io/images/ey850001/production/a5889abdd5e80376912546622e046e3fbef234b4-2496x1664.png?w=640&auto=format)](/insights/what-is-open-source-sovereign-ai.md)

 Article · 7 min AI 

### [What Is Open Source, Sovereign AI?](/insights/what-is-open-source-sovereign-ai.md)

Every company is encouraging more AI, and almost every AI bill is climbing. Sovereign AI is about two kinds of control: over your data and over your costs. Here's why open source sits at its foundation.

[Iain MacNeil](/people/iain-macneil.md) Article 

[![AI-first experience design](https://cdn.sanity.io/images/ey850001/production/8bbc2418e87924ef10f36115478a2e10d5bed95e-1584x624.jpg?w=640&auto=format)](/insights/how-build-ai-first-experience-without-throwing-out-everything-youve-already-built.md)

 Article · 6 min AI Sep 10, 2026 

### [How to Build an AI-First Experience Without Throwing Out Everything You've Already Built](/insights/how-build-ai-first-experience-without-throwing-out-everything-youve-already-built.md)

Users now expect a direct answer, not a list of links to click through. Here's how organizations can layer an AI-First experience onto their existing website or app, in weeks rather than years.

[Andrew Douglas](/people/andrew-douglas.md) Article 

[![What Becomes Possible When AI Can Read All the Data](https://cdn.sanity.io/images/ey850001/production/c28ab0bd26753421ee42937e13adc7a7f19b47f0-1296x510.jpg?w=640&auto=format)](/insights/what-becomes-possible-when-ai-can-read-all-data.md)

 Article · 4 min AI Sep 09, 2026 

### [What Becomes Possible When AI Can Read All the Data](/insights/what-becomes-possible-when-ai-can-read-all-data.md)

Most of the world's data has never been analyzed, only sampled. AI changes that. How AI-ready data platforms unlock questions no one could ask before.

[Appnovation](/people/appnovation.md) Article 

## Get the thinking,  
*before everyone else.*

One considered email a month. New essays, field notes from live engagements, and the occasional strong opinion. No noise.
