Check your version

This video covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

Alternate resources: 

Using Entity Metadata Wrappers

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:02
    [Working with Entities in Drupal 7, Chapter 15,]
  • 0:04
    [with Joe Shindelar]
  • 0:05
    One of my favorite things about the Entity API is the supplied metadata wrappers,
  • 0:10
    which we're going to be taking a look at in this lesson.
  • 0:13
    The metadata wrappers allow us to wrap an entity object with another class
  • 0:19
    and provide easier access to the values of all the fields and the properties.
  • 0:24
    It makes it so that I don't have to worry about things like the current user's
  • 0:27
    preferred language or field cardinality.
  • 0:30
    I can just access the properties and their value using simple object notation.
  • 0:35
    Rather than what you've probably seen before when you've tried

Using Entity Metadata Wrappers


If you've done any module development, you're probably familiar with hook_node_view and Drupal's arrays of doom. In this lesson we'll show you how to get easier access to the same information using entity metadata wrappers. We will use the entity_metadata_wrapper function to retrieve a new EntityMetaDataWrapper object that provides an interface for easily accessing an entities property and field values. We'll use the getPropertyInfo method to expose information about individual properties, and the getIterator method to access fields that contain multiple values, such as tags. You'll see that by using meta data wrappers you can also access properties on referenced entities, such as the email address of the author of a node, without having to load that information independently.

Metadata wrappers also provide a consistent way to access properties common to all entities. For example, every entity in Drupal has a unique ID property or a human readable label, but these properties often have different names. User name vs. node title. Metadata wrappers allow you to access this information in a consistent way.

Log in or sign up to download companion files.
Additional resources: