Learning Drupal 7 Entities

One of the biggest changes in Drupal 7 is the entity system. In Drupal terms “Entities are the new nodes”. They are the “base class” if you will for defining types of data. Still, nodes remain the base for types of content. The distinction between “data” and “content” is subtle, but an important one.

In previous versions of Drupal, the node system was bent and wrapped around just about every kind of data imaginable. It was really the only way to make dynamic data objects shareable and extendable by a broad set of modules. Nodes were used as Content Profiles, to store configuration information, as comments, and of course blog posts and stories. But in a lot of cases, when you took nodes out of their “content” role, the result was really just a hack; and elegant one, but a hack none the less.

In Drupal 7, that’s all changed. You can create a proper class of data for you to extend with types. Kind of like defining your own “node” system. In fact, that’s exactly what you’re doing. It’s really an interesting experience going in and defining new types. For example, you can define “artwork” and have sub types of “statue” and “painting” each inheriting common fields from the root “artwork” but allow each to have their own fields supplied by the Fields API. (The artwork example is from Drupal 7 Module Development)

But learning how to essentially write a new piece of Drupal core isn’t terribly easy. The online documentation helps, so do examples. But I remembered when I was learning Drupal 6, I could always turn to my old friend Pro Drupal Development. Essentially the “Drupal Bible”. It covered everything, and let you dive right into the “fiddly-bits” of Drupal’s framework.

There is a new edition of this book, appropriately called Pro Drupal 7 Development which is also very good, but is lacking in the Entity side of things. It’s a first edition run, and there are several typos and code errors in the first sets of pages, but all in all the book is good. But it really doesn’t cover the entity system that well, nor does it really cover how to create your own entities in any great depth. For such an important new framework inside Drupal, it would have been nice to have a more expansive chapter on creating entities. You look at the detail gone into the menu system and personally I would have liked to see as much gone into entities.

That’s not to say it doesn’t help at all, but it definitely benefits from a friend in the print world; Drupal 7 Module Development. There’s an entire chapter on defining your own entity type, adding fields and really explains it well. At times you get lost as to which file or function the author is referring to but you can usually figure it out by looking at the code examples. This is a much more practical or example driven book, which I prefer. But once again, at times some things just aren’t explained properly, so referring back to Pro Drupal helps. Between the two, you should have no problem defining entities.

And once you’ve done this, you’ll realize “man that’s a lot of work”. A lot of repetitive code, repetitive functions, just a lot. Thankfully, in proper Drupal fashion, there’s a module for that. Enter the Entity API module. The unfortunate naming of this module makes googling for information on Drupal’s entity code or API tricky but can be forgiven. If you use this module to help you build your entities, you can save a lot of CRUD work. A great example of using this module to define entities is in the Drupal Commerce suite of modules. The cousin or uncle to the Ubercart project defines several entity types and uses the Entity module to help do so.

I certainly don’t consider myself an expert on Entities just yet. They’re still very new and I haven’t had the chance to implement a lot of Drupal 7 applications which required custom entities just yet. But the tools listed above have helped me learn what I know thus far and I’m sure will be invaluable once entities become a day to day thing in the Drupalverse.