Articles

This cheat sheet provides an overview of commonly used methods, classes, and interfaces for content entities.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

Audience
This documentation is primarily intended for developers experienced in object-oriented PHP, Drupal 6 or Drupal 7, and for those looking to explore the principles of Drupal 8.
The guide to creating a content entity type in Drupal 8 contains a full list of available options.
Building a Bundle-less Content Type in Drupal 8
In this case, we are creating a Drupal 8 content entity that does not have any bundles.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

This page is a copy of Enable-by-default configuration in a Drupal 8 module. This should be considered deprecated.
Creating a custom content type has become quite straightforward thanks to the new Configuration API provided by Drupal 8.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

Sometimes, when extracting a content type from a custom module, you may want to include fields associated with that content type. Automatically creating fields allows you to remove and reinstall on multiple sites without leaving behind unnecessary fields and ensures you don’t forget to add them. There are two ways to add these fields to your codebase, which we’ll cover here.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

Sometimes, when extracting a content type from a custom module, you may want to include fields associated with that content type. Automatically creating fields allows you to remove and reinstall on multiple sites without leaving behind unnecessary fields and ensures you don’t forget to add them. There are two ways to add these fields to your codebase, which we’ll cover here.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

Significant Improvement
- The Entity API now implements the Typed Data API
In this new implementation, the Entity API treats everything as a field based on the same API, making entities predictable and consistent.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

Placeholder landing page, just to outline the various child pages we’ll need.
For now, see: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/group/entity_api/8.

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

Defining and Using Content Entity Field Definitions
Content entities must explicitly define all their fields by providing field definitions in the entity class. Field definitions are based on the Typed Data API (see also How Entities Implement It).

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

In Drupal 8, field language is no longer exposed via the public API. Instead, fields are attached to language-aware objects from which they "inherit" their language.
The key benefits here are:

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.

Display Modes
There are display modes (available at admin/Structure/Display-Mode) that provide different views of content entities for viewing or editing. The two types of display modes are "view modes" and "form modes." Both of these — view modes and form modes — are examples of configuration entities. Here’s an example of an exported view mode.
