Entities and Bundles in Drupal 7
Jun 22, 2012 · CommentsDevelopmentDrupal
I have to say that one of the things that I have spend the most time trying to get a good grasp of in Drupal 7 is the concept of Entities and Bundles in Drupal 7. The Field API seemed pretty intuitive, and the basic concept of Entities (a computer science-y word for “things” or “objects”) seemed pretty clear.
This great tutorial from Trellon helped a lot.
I even understood what Bundles did in terms of creating sub types for Entities that could hold different sets of fields. That made perfect sense.
What I did not understand was the implementation. Which parts were from the core and which parts were from the Entity API (also see the Entity Project Page). There seemed to be conflicting documentation between hook_entity_info() and the previously mentioned Trellon tutorial, about how to define Bundles and what they were used for and what exactly they were.
The crux of the matter as I understand it (which may not be exactly right), is that Entity API adds the functionality to Entities that really should have been included in the Core implementation but wasn’t. The Core on it’s own does not provide a “run-time” way to define Entity Bundles. You had to do it in the code.
This obviously presented a problem for non-developers who needed to add content (aka. a whole freaking lot of people who use Drupal). So, the Entity API module was developed to address this issue and it does a great job.
But…
The implementation is sort of Hackish (another very technical computer science-y term). Technically when bundles are defined by the Entity API they are also Entities and in fact logical peers of the Entity they are providing the Bundles for. It would have made a lot more sense if all these things were called objects because that seems more generic than Entities and doesn’t seem to imply a structural hierarchy the way Entities and Bundles do (in the core implementation).
Especially since the Core defines bundles as an Entity Property and Entity API changes that so some other Entities are used to define the Bundles property of an Entity.
In an effort to understand how this works for myself I made this picture. I am not a great artist and this may not be totally right. If it’s not please let me know in the comments. I just couldn’t find another (good) picture anywhere of how this worked…
Also the best documentation leaning how to work with Entities is in the Entity API README and the Entity API code itself. Please read these!