Create custom Entity Type in Drupal8 for better content management
Blog

Create custom Entity Type in Drupal8 for better content management

Entities were introduced from Drupal 7.  I would say in Drupal 8 , entities are essential part takers like node, users, files, images and comments, etc.. Still sometimes you need to create Drupal custom entities according to your requirements. From the experience of working with some of the top Media companies in the world, sometimes we need to create custom Drupal 8 entity types. Example like recently we got the requirement to create the entity for string the analytic data of the Articles. Why  we need to create  the custom drupal entity instead of using nodes  or exiting entities, because the client doesn’t want to show the data in content administration page (‘admin/content’). Still it should be able to manipulate the data and also easy to administrate with views, fields, etc. So finally we architect to create the custom Drupal 8 entity types.

Here in this tutorial, we will go through how to  create custom Entity type in Drupal 8. Entity is fully fieldable and uses most of the new entity concepts available in Drupal 8. We will be creating an entity type let’s say “inventory” and will also see how to create edit and delete

How to create a Drupal 8 Entity Type Programmatically?

So let’s create a module called “Manage Inventory” with the custom entity type ‘inventory’. And the details to create the module is explained in following steps,

Module Setup :

manage_inventory.info.yml

Routing :

In Drupal 8, menu system has been replaced by routing system.

manage_inventory.routing.yml

The route names for actions, defined in the 'link' section of the entity annotation must follow the right pattern.

manage_inventory.links.menu.yml

In addition of routing file, this replaces hook_menu for the module.

manage_inventory.links.action.yml

manage_inventory.links.task.yml

The "View/Edit/Delete" tabs will appear on the entity view page. The "Settings" tab will appear on the entity settings page.

Entity type classes

src/InventoryInterface.php

It’s better to provide an interface to define the public access to an entity. Here we invoked EntityOwnerInterface to get access to additional functionality.

src/Entity/Inventory.php

This file defines the Inventory entity class. The database schema is automatically determined from the definition of the base fields and here we can define the required fields for the content types. As mentioned in the routing section, the routes for the 'links' section must follow the right pattern. It is documented in the annotations section below.

Controllers

Let’s rewind. Until now we have seen routing, links and interface as well as Entity class. Once the entity type is created we need to provide an interface to the user for creating the entity.

Now let’s create form for creating entity.

src/Form/InventoryForm.php

Here we will define Entity form for adding and editing Inventory entity content. We can call this _entity_form defined in routing.

InventoryDeleteForm.php

Confirmation form while deleting the entity.

In above class we have submission handler for deleting any content and also for creating a log for the same.

src/Entity/Controller/InventoryListBuilder.php

Here we will be defining a listing page for the Entity type. In list we will be extending EntityListBuilder class to create list. We will be adding header with desired fields and later constructing row using rowBuilder method. Operation link will be added automatically from the parent class.

 

Field Settings

Till now we have seen,

  • How we can add routing
  • Creating Entity class for defining the entity with the default required field
  • Form for adding, editing content and deleting.

Now we will see how to add more fields from user interface. For that we need to define field settings class. So let’s create InventorySettingsForm.

src/Form/InventorySettingsForm.php

Access control handler

Entity type creation is done. Now we need to create access control handler for the same. With the implementation of EntityAccessControlHandler we will able to restrict the access of the content which we can manage from interface.

src/InventoryAccessControlHandler.php

After this, we need to register this entity type, which we will achieve by enabling the module. Incase you have enabled this module in the middle of this blog before creating Inventory class you will land up with error as  Drupal won’t have created the database table.

To overcome the error you can simply execute drush updatedb --entity-updates.When executing this command, Drupal will inform you that it found a new entity and that there's a pending update. After applying the pending update, you will be able to start using the new entity on your Drupal website!