Submitted by admin on Thu, 09/05/2019 - 15:53

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 happened 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

Image removed.

How Do Entities Impact Your Site Building?

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.

Add Fields to Comments

Image removed.

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.

  1. Click on Structure in the black admin menu bar
  2. Click on Content types
  3. Click on Manage fields for the Article content type
  4. Observe the tabs shown in the screen shot above.
  5. You have the option to add fields to the content type via the Manage Fields tab and you have the option to add fields to comments via the Comment Fields tab. The fields you add to the Article content type will not automatically apply to comments on other content types.

Add Fields to Terms

Image removed.

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.

  1. Click on Structure in the black admin menu bar
  2. Click on Taxonomy
  3. At this point you can either edit an exiting vocabulary or add a new one. If you add a new vocabulary, save the vocabulary and then click edit vocabulary to see the tabs
  4. Once you are in vocabulary edit mode, click on Manage Fields
  5. Add the fields you need.

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.

Add Fields to Users

Image removed.

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.

  1. Click on Configuration in the black admin menu bar
  2. Click on Account settings
  3. Observe the Manage Fields tab (see screen shot above)

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.

Summary

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.

It's also worth reading the Drupal.org article called "An Introduction to Entities".

Solutions Img