Skip to main content

When to use custom developed drupal entity types?

Back-end Development
Drupal

Background

With Drupal 7 came entities.  Most everything is an entity which has normalized and streamlined many features within Drupal.  An entity is a piece of information with a given set of attributes. Read here if you are not familiar with the concept of Entities.  In short, entities are the building blocks of  Drupal.  A node (content type) is a type of entity, so is a user profile, and comment, and a taxonomy term.  At the core of each of these lies the same structure.  What differentiates them are the properties added to them in code.  Again read http://drupal.org/node/1261744 for a great explanation, and definition of the various aspects of entities (bundles, fields, etc).

The question I would like to address is when is it appropriate to define a custom entity vs just using the node entity already provided?   As I mentioned, a node is  an 'extended' entity.  It is field-able, usable in views (and of course in an entity field query) just like all entities are.  But is also has other properties, like versioning, a URL path, a publish status among many others. Often we are tasked with putting together a very data centric web application.  In these situations I find myself drawing Entity Relationship Diagrams followed by figuring out how to best define Drupal content types to match my ERD. Lately I've been exploring adding custom entities to the mix. I should note that just about everything you can do with entities you can also do with nodes, just with some extra baggage and possibly administrative clutter.

Considerations

Do you have the need for many "bundles" or "content types" that all share common properties?

While it isn't difficult to get up and running with a custom entity type it probably isn't worth the effort if your only going to use it for one type (which drupal refers to now as a 'bundle').  Custom Entity Types work best when you have a number of field-able elements that share properties. Do you need to have a page for each piece of content? Nodes are nice because they provide a page and other standard features right "out of the box".  If your needing dedicated page for each entity then sticking with nodes might be the way to go.  That being said, any entity can do anything and everything that a node can do, so this certainly isn't a rule.

How familiar are you with writing modules?

I would not suggest that anyone who isn't at least somewhat familiar with the inner workings of Drupal dive into creating a custom entity type.  While you can get up and running fairly quickly using the Model project, you will most likely need to tweak and debug your entity along the way, and since you are developing a CUSTOM entity type, most often there are other unique pieces of functionality you'll want to add.  Rarely will the plain entity model project base provide all that you need.

Have you considered one of the other entity types in core?

Because a user, term, comment and node are all variations of an entity, you should consider if one of these other types fulfill your requirements.  While this may seem like a long shot, it's worth mentioning.  Especially when it comes to Taxonomy Terms, which now act very similar to a basic node. Let me know if you have questions or are looking at digging into custom entities. Hope this helps.

Need a fresh perspective on a tough project?

Let’s talk about how RDG can help.

Contact Us