Creating New Entities in Drupal 7
Submitted by admin on Sun, 10/30/2011 - 23:13
History of Entities
The very first thing I learned when started working Drupal was this: "everything is a node." This is what allows you to write a module that works generally on nodes and have it apply to sll sorts of content you have no idea about. Later, I learned that this wasn't quite true. It's true that all content types are descended from notes. Comments, users, taxonomy terms are not nodes. They don't share a common base type. In Drupal 7, they sought to resolve this with another level of abstraction called Entities. So the new truth is Drupal 7 is: everything is an Entity.Why add entities?
As in Drupal 6, you can happily using Node as your base type and add fields to it to create any custom content type. But in Drupal 7 and going forward, entities will lead the way in adding new functionality in a generic way. No longer will you need to have a Title and Body on all your content. The main reason I needed to use Entities was to take advantage of the Relations module and all the metadata goodness of being able to treat relationships as entities.How to create an Entity?
I spent nearly two days working to get a good functioning entity. Good and current info on creating entities is hard to find. Like most cases you'll need to roll up your sleeve, plug your nose, and dig into some existing code to see how this stuff works. I studied the Profile2, Model Entities, and Message modules to see how other devs created their entities. What I'm going to describe here is a hybrid of what Model Entities does but with out the Views integration.Let's get started!
For the sake of illustration, I'll be creating a module called: Dummy and entities called: Vehicle and Vehicle Type. One trouble I found reading the Model Entities code was that the module and entity were both called "model" so it wasn't easy to tell whether it was a hook implementation or something else.Files you will create
- dummy.info
- dummy.install
- dummy.module
- vehicle.admin.inc
- vehicle_type.admin.inc
- Log in to post comments