Khanh Hoang - Kenn
Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.
In this blog post we're going to introduce you to Drupal entities and how they make site building easier.
In Drupal 6, there was a big difference between how Drupal handled different types of content. Nodes, comments, terms and users were all technically "content", but Drupal treated them all differently.
What happend with Drupal 7 is that we started to treat nodes, comments, terms and users in the same way. No longer was there a big difference between them.
Each individual node, user, term and comment is now called an entity.
Nodes, comments, terms and users are now called entity types. The image below from Heather James on Drupal.org shows some "entity types" in Drupal 7
Let's take the example of fields:
In Drupal 6 you could use the C.C.K. module to add fields to your nodes. If you used different modules, it was possible to add fields to other parts of your site, such as comments, terms, and users. However, having some fields in C.C.K. and some fields in other modules, made customization challenging at times.
In Drupal 7, not only have fields been added to the core, but those same fields can be added across your site. You can use the same fields for nodes, comments, terms, and users. This makes Drupal 7 more flexible and easier to customize. You can also use less modules to build your site.
In this example, imagine that you want to collect more than information when people leave comments. If you want to collect more than name, email and comment, you can add a field. For example, you might want to allow commenters to leave a link to their homepage. Let's assume that is the case for our Article content type.
It's also possible to add fields to terms. Why would you want to do this? Let's assume you want an icon to be associated with each term that you add. You can add an image field via the vocabulary's Manage Fields interface and for each term, you can upload an image icon.
The screenshot above shows the Manage Fields tab for terms.
Remember that with nodes, you add all the fields to the content type or to the comments within a content type. So with terms, the key part is the vocabulary. Add the fields to the vocabulary and all of the terms will automatically have those fields available.
Sometimes it's nice to know a little bit about the users on your site. One way to do this is to add some fields to their user account. For instance, you might want to know where they live, where they work, their hobbies, and more. You can add fields to collect all of that information.
Are you sensing a pattern yet? Yep, you guessed it. If you click on the Manage Fields tab, you are given the option to add fields to user accounts.
We have only scratched the surface of the world of entities in Drupal and if you are not a coder, you might be wondering why you need to even know that nodes, comments, terms, and user are types of entities.
The answer is, module developers are contributing modules that provide enhancements to nodes, comments, terms, and users and sometimes that use the word entity in the name of their module. If you didn't know what an entity was, you might not understand what the module is doing.
Of course, the entity APIs don’t stop at simply allowing nodes, comments, terms, and users to use the same fields. There is so much more and if you are a coder, we recommend you check out Wolfgang Zieglar’s presentation on the Entity API module at DrupalCon Denver. The presentation goes into much more detail about how entities have changed Drupal in a very good way.