logo
Submitted by Ivan Abramenko on Thu, 10/03/2019 - 17:49

We have already figured out the Form API, the Fields API and we know how the data in Drupal 8 gets into the database. Now let's look at the foundation of all Drupal sites, namely the Entity API.

You have probably noticed that the fields do not exist by themselves, but are “attached” to entities: nodes, blocks, taxonomy terms, views, etc. You can create bundle entities, for example, material types, block types, taxonomy dictionaries. But what if you need to create a new entity with your bundles? In this case, you will need the Entity API to create a new entity.

I added all the code on github to the drupalbook_product module, you can download the module and add it to my website:

https://github.com/levmyshkin/drupalbook8

Проще всего создавать новые типы сущностей через Drupal Console, потому что потребуется выполнить всего одну команду.

https://drupalconsole.com/

You do not have to often create new types of entities. Most small and medium-sized projects use contrib modules as the basis, for example, Commerce for online stores:

https://www.drupal.org/project/commerce

If you are going to create your own plug-in module, then perhaps you will need to create a new type of entities, as well as among the Drupal modules, you can find an already suitable module with the necessary functionality, which can be added or supplemented with its missing functionality.

In this article, we will analyze the Entity API and create our own type of Product entities, which will serve as the basis for a small custom catalog or store. If you still decide to create a custom store, then I would strongly recommend that you use Commerce, because it will save tons of time for you and your customers.

When you want to create a new type of entity, the question arises, why can't you create another type of Product material and use it for a catalog? Yes, this can be done for a small catalog of goods with output via Views and simple filters by price and balances. But when the number of products will be thousands, the manager will be uncomfortable to search for the desired product on the content page. The manager will want to fasten the filters by article, category, color, name and all additional kit on the page

/admin/content

It will look redundant and interfere with working with other types of materials News, Articles, Events and Pages. Therefore, in such situations, they make a separate type of Product entities with their own admin panel and settings.

Let's get started creating a separate type of Product entity. Before you execute the command to create an entity type, I highly recommend creating a backup of the site, at least the database. Because the process of creating an entity type consists in part of generating configs that are written to the database and if something goes wrong and part of the configs is written without entity type files in the module, this will break the site. Create backup first.

Now we are creating a new module, it can also be created through the Drupal Console:

drupal generate:module

https://hechoendrupal.gitbooks.io/drupal-console/en/commands/generate-module.html

 

$ vendor/bin/drupal generate:module
 
 // Welcome to the Drupal module generator
 
 Enter the new module name:
 > Drupalbook Product
 
 Enter the module machine name [drupalbook_product]:
 >
 
 Enter the module Path [modules/custom]:
 >
 
 Enter module description [My Awesome Module]:
 > Products catalog
!>
 Enter package name [Custom]:
 >
 
 Enter Drupal Core version [8.x]:
 >
 
 Do you want to generate a .module file? (yes/no) [yes]:
 > no
 
 Define module as feature (yes/no) [no]:
 > no
 
 Do you want to add a composer.json file to your module? (yes/no) [yes]:
 > no
 
 Would you like to add module dependencies? (yes/no) [no]:
 > no
 
 Do you want to generate a unit test class? (yes/no) [yes]:
 > no
 
 Do you want to generate a themeable template? (yes/no) [yes]:
 > no
 
 Do you want proceed with the operation? (yes/no) [yes]:
 > yes
 
Generated or updated files
 Generation path: /home/laptop/projects/drupalbook
 1 - /modules/custom/drupalbook_product/drupalbook_product.info.yml
 
                                                                                                                         
 Generated lines: "5"

Or just create a module with the drupalbook_product.info.yml file: 

name: 'drupalbook_product'
type: module
description: 'Products catalog'
core: 8.x
package: 'Drupalbook'

Now we can generate the Product entity type.

https://hechoendrupal.gitbooks.io/drupal-console/en/commands/generate-entity-content.html

$ drupal generate:entity:content

$ vendor/bin/drupal generate:entity:content
 
 // Welcome to the Drupal Content Entity generator
 Enter the module name [admin_toolbar]:
 > drupalbook_product
 
 Enter the class of your new content entity [DefaultEntity]:
 > DrupalbookProductEntity
 
 Enter the machine name of your new content entity [drupalbook_product_entity]:
 >
 
 Enter the label of your new content entity [Drupalbook product entity]:
 > Product    
 
 Enter the base-path for the content entity routes [/admin/structure]:
 >
 
 Do you want this (content) entity to have bundles? (yes/no) [no]:
 > yes
 
 Is your entity translatable? (yes/no) [yes]:
 > yes
 
 Is your entity revisionable? (yes/no) [yes]:
 > no
 
 
 // generate:entity:config
 
 Enter the base-path for the config entity routes [/admin/structure]:
 >
 
Generated or updated files
 Generation path: /home/laptop/projects/drupalbook
 1 - modules/custom/drupalbook_product/drupalbook_product.permissions.yml
 2 - modules/custom/drupalbook_product/drupalbook_product.links.menu.yml
 3 - modules/custom/drupalbook_product/drupalbook_product.links.task.yml
 4 - modules/custom/drupalbook_product/drupalbook_product.links.action.yml
 5 - modules/custom/drupalbook_product/src/DrupalbookProductEntityAccessControlHandler.php
 6 - modules/custom/drupalbook_product/src/DrupalbookProductEntityTranslationHandler.php
 7 - modules/custom/drupalbook_product/src/Entity/DrupalbookProductEntityInterface.php
 8 - modules/custom/drupalbook_product/src/Entity/DrupalbookProductEntity.php
 9 - modules/custom/drupalbook_product/src/DrupalbookProductEntityHtmlRouteProvider.php
 10 - modules/custom/drupalbook_product/src/Entity/DrupalbookProductEntityViewsData.php
 11 - modules/custom/drupalbook_product/src/DrupalbookProductEntityListBuilder.php
 12 - modules/custom/drupalbook_product/src/Form/DrupalbookProductEntitySettingsForm.php
 13 - modules/custom/drupalbook_product/src/Form/DrupalbookProductEntityForm.php
 14 - modules/custom/drupalbook_product/src/Form/DrupalbookProductEntityDeleteForm.php
 15 - modules/custom/drupalbook_product/drupalbook_product_entity.page.inc
 16 - modules/custom/drupalbook_product/templates/drupalbook_product_entity.html.twig
 17 - modules/custom/drupalbook_product/templates//drupalbook-product-entity-content-add-list.html.twig
 18 - modules/custom/drupalbook_product/drupalbook_product.module
 19 - modules/custom/drupalbook_product/drupalbook_product.module
 20 - modules/custom/drupalbook_product/drupalbook_product.module
 21 - modules/custom/drupalbook_product/config/schema/drupalbook_product_entity_type.schema.yml
 22 - modules/custom/drupalbook_product/drupalbook_product.links.menu.yml
 23 - modules/custom/drupalbook_product/drupalbook_product.links.action.yml
 24 - modules/custom/drupalbook_product/src/Entity/DrupalbookProductEntityTypeInterface.php
 25 - modules/custom/drupalbook_product/src/Entity/DrupalbookProductEntityType.php
 26 - modules/custom/drupalbook_product/src/DrupalbookProductEntityTypeHtmlRouteProvider.php
 27 - modules/custom/drupalbook_product/src/Form/DrupalbookProductEntityTypeForm.php
 28 - modules/custom/drupalbook_product/src/Form/DrupalbookProductEntityTypeDeleteForm.php
 29 - modules/custom/drupalbook_product/src/DrupalbookProductEntityTypeListBuilder.php
 
                                                                                                                         
 Generated lines: "1060"

Now that the module is generated, you can enable it so that a new entity type appears on the site:

drupal console

When generating an entity, the Drupal Console prompts what the entity type should be.

Enter the class of your new content entity [DefaultEntity]:

The name of the PHP class for our entity, it is best to add Entity at the end of the name.

Enter the machine name of your new content entity [drupalbook_product_entity]:

The machine name of the class will be proposed based on the class name, if you press Enter, the proposed name will be used.

Enter the base-path for the content entity routes [/admin/structure]:

Your type entities will have their own page for CRUD operations:

structure drupal 8

The display of the product page is rather primitive, but it can be improved in the future or created through Views:

Product display

To create a new entity, you first need to create a bundle of the Product type entity type. We chose the bundable entity:

Do you want this (content) entity to have bundles? (yes/no):

Therefore, we are creating a new type of product. We will need this to create different fields Weight, Size, Color and other additional characteristics. For example, monitors will have a diagonal of screens, and shoes will have shoe size.

Let's create a new Product type - Clothes:

product type

Now with our new type of products you can customize the fields:

product type fields

We definitely need a price field at least.

Let's create a couple of products and take a look at the database. We have the drupalbook_product_entity table in it the UUIDs of our products are stored:

product data

There is also another drupalbook_product_entity_field_data table, in which Properties is stored. These are special fields of entities that are stored directly in tables, because these fields such as Name (Entity label, for example, this is the Title of a node) will not change depending on the revision.

product entity

If you add fields, then two tables will be created for each field, because we chose to use revisions:

Is your entity revisionable? (yes/no) [yes]

I think now you understand how to create new types of entities. In the following articles we will expand the capabilities of our custom catalog / store.

I added all the code on github to the drupalbook_product module, you can download the module and add it to your website:

https://github.com/levmyshkin/drupalbook8