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: 

Introduction to Working with Entities in Drupal 7 Series

Video loading...

  • 0:01
  • 0:06
    [Series Introduction: Working with Entities in Drupal 7]
  • 0:13
    >> In this series, Blake and I are going to be introducing
  • 0:15
    the entity API in Drupal 7.
  • 0:18
    We're going to make use of the core entity API.
  • 0:21
    We're also going to make heavy use of the entity
  • 0:23
    module from contrib, because it provides a lot of really useful functionality.
  • 0:28
    In fact, we're probably going to make so much use of that module
  • 0:31
    that I'd say it's safe to just pretend that there's no reason
  • 0:35
    to work with entities without using the entity module from contrib.
  • 0:39
    Just download it, install it, and make use of it.
  • 0:43
    We're going to go through a whole bunch of terminology in this series.
  • 0:47
    There's a lot to learn—entity types, bundles, properties, fields, entities—
  • 0:52
    and it can get a little bit confusing, but don't worry.
  • 0:54
    We'll cover all of it.
  • 0:56
    We'll also take a look at how you can write code that works
  • 0:59
    with existing entities in Drupal 7—like nodes and comments
  • 1:03
    and users and files—and write it in a way that's it's reusable.
  • 1:07
    And then we're going to take a look at the entity field query class,
  • 1:11
    which makes it possible to build lists of entities in a bit more of an abstract way
  • 1:16
    so that when somebody adds a new module and a new entity type,
  • 1:19
    your code's still going to work no matter what that entity type is.
  • 1:24
    >> After we've got a good handle on the entities that come
  • 1:26
    with Core and things you can do with existing entities,
  • 1:28
    we'll create our own custom video entity and take a look at
  • 1:32
    what's involved in that process and what we need to do to make that happen.
  • 1:35
    We'll need to look at the entity class that comes with the contrib module,
  • 1:39
    the controllers, the controller for the admin UI,
  • 1:42
    and then some additional support provided by the contrib module
  • 1:45
    that'll do things like enable our entity to use views, be exportable,
  • 1:49
    use revisions, etc.
  • 1:52
    >> When we use the entity API, we got a lot of stuff for free.
  • 1:56
    But we're also going to have to do some things to describe
  • 1:58
    the different properties on our entities and all the metadata that's contained within that.
  • 2:03
    We'll be using hook entity property info to describe those things.
  • 2:07
    We're also going to look at using the metadata wrappers provided by
  • 2:11
    entity module from contrib, which make it super easy to access
  • 2:16
    the values of properties and fields.
  • 2:18
    And also to set the values of those 2, even if you don't know
  • 2:22
    what language the user prefers or even the cardinality of the field.
  • 2:26
    Metadata wrappers can make it really easy to find out just about
  • 2:29
    anything about your entity.
  • 2:33
    >> We'll also take a look at some other examples of entities
  • 2:35
    provided by other contrib modules and talk a little bit about why
  • 2:38
    you might want to create your own entity or when
  • 2:41
    you can just stick with plain old nodes.
  • 2:43
    >> Even though the nodes system is really powerful,
  • 2:45
    not all content in Drupal needs to be a node.
  • 2:48
    Using the entity API, we can describe our content in different ways,
  • 2:53
    without having the extra baggage of making nodes.
  • 2:55
    For example, if the comment module is turned on,
  • 2:58
    and your storing data is a node, those 2 systems work together.
  • 3:04
    But for our video entity that we're going to create,
  • 3:06
    we don't necessarily even need comments in the first place.
  • 3:09
    So why make it a node and have that extra overhead?
  • 3:13
    The entire concept of entities is something new for Drupal 7,
  • 3:17
    and the entity API makes it pretty easy to get started using them,
  • 3:21
    giving us ways to describe our content in ways other than just creating a new node.
  • 3:26
    It's already being used for things like users and comments,
  • 3:30
    even storing files, but it's pretty cool once you start using it
  • 3:34
    to store your own data, and you'll see all the power that it provides.
  • 3:37
    When you're done with this series, you'll have a very good
  • 3:41
    understanding of how all of the pieces fit together.
  • 3:44
    We even talk about some of the things that may not
  • 3:47
    get used that often, but are still useful.
  • 3:50

Introduction to Working with Entities in Drupal 7 Series


Entities were introduced in Drupal 7 as a way of taking the things that people loved about nodes + CCK in Drupal 6 and applying them to other types of data like users, comments, and taxonomy terms. 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 existing ones in a generic way. The API in Drupal core however is still missing some really useful tools and is supplemented by the Entity module in Drupal contributed which we'll make heavy use of throughout the series.

In this series we'll learn about the interplay between Entities, Entity Types, Bundles, and Fields and how to write custom code to deal with each of these things. The Entity API demo site files that we use in this series are all located in the Lullabot GitHub, as well as in zip files attached to the respective video pages, under the Downloads tab.

This series covers:

  • What entities are and how they fit into the Drupal ecosphere
  • EntityFieldQuery
  • Entity classes, what they do and how to override them
  • Providing an admin UI for adding/editing and deleting entities from Drupal
  • Making entities fieldable
  • View modes
  • Creating custom UI's for dealing with entities
  • Describing entity properties to Drupal
  • Views integration for entities
  • Entity Metadata wrappers
  • Making entities revisionable

And much much more. This series assumes that you're already familiar with the basic tenets of writing modules for Drupal and makes use of things like hook_menu() without spending time explaining them. If you're not familiar with Drupal module development, you might want to brush up by watching our Module Development for Drupal 7 first.