April 19 2018

How to create Custom Entity in Drupal 8

Entities in Drupal provide a structured way of storing and managing data. With the inclusion of Entity API in Drupal 8, it becomes even easier to manage existing, and creating new entities. Similar entities are grouped into Entity Types, which further have subtypes known as Bundles, to which fields can be attached.

For example, the node entity type has two bundles — Articles, and Basic page. The bundles share some common properties and also have some fields that are unique to them.

Drupal core provides with many built-in entity types and also allows us to define custom entity types and bundles. There are two types of entity types — Content Entities and Configuration Entities.

When to use Custom Entities?

Before actually defining custom entity types, let us understand the situations where they should be used:

Boost in Performance

In most cases, nodes can be used to store any kind of content. However, nodes implement tons of hooks and add the functionalities that might not be required. Moreover, all the content data is stored in a single database table. This can cause performance issues when the number of content items increases. Here, custom entities can be used to boost performance.

Build a better admin UX

At times, it is necessary to provide better administration forms and menus for the site builders as well as the content editors. The menus, paths, and form displays can be configured as required.

Defining custom functionalities

We can implement custom methods to our entity types, add custom menu items, and paths. We may also define custom data storage and workflows as per requirements.

Custom entity types allow us to define custom functionality, boost performance, and have total control over the structure of data, admin menus, paths, and storage schema.

Creating a Custom Entity using Drupal Console

To define custom entities, we must create a module and then add a custom entity. Drupal Console can be used to generate most of the boilerplate code, however, some knowledge of object-oriented programming, PHP, and YML is required to be able to add custom functionality to our entity.

Let's consider an example of a Book entity with the following properties — Title, ISBN, Author(s), and Price, with two bundles or subtypes — EBook (File size, Download URL) and PaperBook (Weight, In Stock).

Drupal Console has generated the module under modules/custom/books. Now, to add custom properties to the Book entity type, open up the file modules/custom/books/src/Entity/BookEntity.php in any IDE or text editor and add the following lines of code under the baseFieldDefinitions method of the class BookEntity.

Various other aspects of the entity can be customized such as admin menus, paths, and forms, constraints and properties can be applied to the fields. Refer to the docs for a complete reference. For now, let’s install the newly created module.

$ drush en books -y

or

$ drupal module:install books

In case you make any changes to the entity after installing the module, make sure to run the drupal update:entities to apply the updates.

Creating Bundles and Adding Fields to Them

Bundles can be created and fields can be attached to them using the UI.

Navigate to Manage → Structure and click on “Add Book Entity type”

Add a suitable label and click on “Save”

Click on “Manage Fields” from the drop-down menu and add fields to the bundle as required.
​​​​​​​

Repeat this process for all the bundles

You can now create entities of your custom entity types. By default, the menu for creating a new entity will be present under admin/structure/{your-custom-entity}.This can be altered by changing the parent key under entity.book_entity.collection in the modules/custom/books/books.links.menu.yml file.

Form displays of the paper book and ebook bundles

Conclusion

Entities are building blocks and integral of a Drupal site. Custom entity types allow us to define custom functionality, boost performance, and have total control over the structure of data, admin menus, paths, and storage schema. They can be created with little or no effort using the Drupal Console.

Let us know in the comments below how your custom entity helped you out. Or hit us up at [email protected] for more such tutorials.