---
title: "Using entity metadata wrappers with Drupal · Appnovation"
url: "https://www.appnovation.com/insights/using-entity-metadata-wrappers-drupal"
description: "Simplify Drupal entity code with entity metadata wrappers. How to read and set node and taxonomy properties in a clean, generic way."
---

Article

# Using entity metadata wrappers with Drupal

[![](https://cdn.sanity.io/images/ey850001/production/68ea411feeb79ffb29cb1fe2d06f74995dc6351d-68x68.jpg?w=68&auto=format) Jerome Jerome Drupal Developer](/people/jerome-jerome.md) Sep 15, 2015  
 2 min read 

![Using entity metadata wrappers with Drupal](https://cdn.sanity.io/images/ey850001/production/eb991531f19adaf414d87b6ede3c34c05d602525-1400x400.jpg?w=1400&auto=format) 

**Introduction** Entity metadata wrappers are PHP wrapper classes used in order to simplify the code dealing with entities(nodes, taxonomy...). They abstract structure so that a developer can write code in a generic way when accessing entities and their properties.

The Entity API in Drupal 7 provides a set of common functions and classes to make it easier for developers to create their own custom entity types or to work with other already existing ones in a generic way.

Everybody already used this kind of code which is sometimes a pain.

//Code sample

*$my\_field = $node->field\_custom\_field\[LANGUAGE\_NONE\]\[0\]\['value'\];*

We have two problems with this kind of code, first, the langage is hard coded into the field value lookup and we'll see PHP warnings if the field has no data.

**How wrappers make life easier** 

The wrapping classes are provided by the Entity API module.

The magic of wrappers is in their use of the following three classes:

*   EntityStructureWrapper
*   EntityListWrapper
*   EntityValueWrapper

*Here is an example of using wrappers with nodes*

// Code sample

*$wrapper = entity\_metadata\_wrapper('node', $node);*

*dsm($wrapper->getPropertyInfo());*

This will print the list of all known fields on the node and their properties can be accessed easily. The wrapper object is defined so now let's check the synthax for querying values of fields.

The code sample on the "Introduction" was the old approach for querying fields, here is the Entity metadata wrapper approach

// Code sample

*$number\_of\_articles = $wrapper->field\_number\_of\_articles->raw();*

raw() retrieves the raw value from the field *field\_number\_of\_articles. It is useful for integer fields...*

*Using wrapper with a single select taxonomy*

*// Code sample (exract specific level)*

*$level = $show->field\_level;*

*$tid = $level\[LANGUAGE\_NONE\]\[0\]\['tid\];*

*$term = taxonomy\_term\_load($tid);*

*$variables\['level'\] = $term->name;*

This was the old approach. Here is the sample code using wrappers.

*$variables\['level'\] = $wrapper->field\_level->value()->name;*

As we said with the old approch we had to specify the language to retrieve the field value but Entity Mmetadata wrappers has native support for accessing the value of langage specific fields.

// Code sample with wrappers

global $language;

// get Localised custom\_field

$my\_custom\_field = $wrapper->language($language->language)->custom\_field->raw();

**Advantages of using entity metadata wrappers**

*   The code is more readable
*   Stop hardcoding the language key into the array lookups
*   Prevent PHP for giving warnings when trying to access properties that does not exist

This article was an overview of the wrappers on drupal 7.

[![](https://cdn.sanity.io/images/ey850001/production/68ea411feeb79ffb29cb1fe2d06f74995dc6351d-68x68.jpg?w=68&auto=format) Jerome Jerome Drupal Developer · Appnovation](/people/jerome-jerome.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.
