Thinking about Drupal 8 Base Entity Types

When developing applications for Drupal the need to interact with what we call Entities arises quickly in our development cycle. The Entity system in Drupal has evolved greatly over the last couple of versions. Drupal 8 now contains a full set of APIs to define, manipulate, and manage entities of various types.

One aspect of Drupal 8 that left me a bit lost was the two different base Entity types. That is:

  • Configuration Entity (ConfigEntityBase)

  • Content Entity (ContentEntityBase)

In this post, I’d like to talk a bit about the key differences between these two types of entities and discuss some of the potential use cases for each. I want to keep this as succinct as possible while still be able to clearly explain the two. Let me know how I did in the comments!

What is a Configuration Entity?

Generally speaking a configuration entity is something that a site administrator or site builder would be creating. There are many types of configuration entities that Drupal 8 implements in core. Some examples include Views, Image Styles, Vocabularies, Display settings, etc.

The idea of a configuration entity enables us to integrate the powerful new configuration system in D8 to the full featured Entity API. Combine this with the fact that we can export all of these configuration entities to YAML files and we have a very powerful solution for our development operations.

Configuration Entity information is stored as YAML. (as of writing active configuration is stored in the database but exportable as YAML)

What is a Content Entity?

Content Entities are pieces of content that are generally created by the content creators of our solutions. These entities serve as the underpinnings of the structured content within our application. Content entities come in various different types in core Drupal 8. This includes nodes, users, block types, comments, etc.

Content entities allow us to create entities that are tightly integrated with the Field API and the same Entity API configuration entities derive from. This allows content entities to be fieldable by site builders in order to define the structure of our content while still exposing a consistent set of APIs for developers to work with.

Content Entity information is stored in the database.

To Summarize

With the above being said we can summarize the core differences between these base Entities into two core areas:

  1. Where the information is stored.
    1. For Configuration Entities data is stored as YAML.
    2. For Content Entities data is stored in tables / fields in our database.
  2. Who is creating the information
    1. For Configuration Entities site builder / site administrators.
    2. For Content Entities content creators / end users.

My motivation to write this was to explore and better understand the differences between these two core types of Entities BUT I think it may be just as important (if not more) to understand the similarities. Both are now under the scope of same unified Entity API. That is important. They provide integration points from the Entity API to two other important APIs, the configuration API and field API respectfully.

As we continue to work with these systems I look forward to learning more about how to leverage these systems to develop solutions for our clients!

Nick's picture
Author: 
Nick
Tags: 
Drupal