Create a custom entity in Drupal 8/9 using drupal-console

lakshmi , Credit to  volkotech-solutions Nov 26

Entities in Drupal give an organized means of storing and managing data. The Entity API offers a uniform interface for dealing with entities and their properties. Using Entity API, you can simply manage existing entities and create new ones. Entities are grouped into Entity Types, which contain subtypes called Bundles to which fields can be attached.

In Drupal core, for example, the node entity type includes two bundles:  Articles and Basic page, and both bundles share several common properties as well as some fields.

Drupal Core contains many built-in entity types and also allows the creation of custom entity types and bundles. Custom entity types enable you to define custom functionality, boost performance and have complete control over the data structure, paths, menus, and storage schema.

Create a custom entity in Drupal 8/9 using drupal-console

To create custom entities, first, create a module and then add a custom entity to that module. Drupal Console is an easily generated module and entity because it is used to generate most of the boilerplate code. Creating custom entities requires some knowledge of object-oriented programming, PHP, and YML is required to be able to add custom functionality to our entities.

Consider an example of a PostActions module with the PostactionsEntity type, properties Title and status, and bundles Follows Readinglist and Likes.

  • Navigate to the root directory of your Drupal site

    $ cd /var/www/html/drupalpractice/
    //Where drupalpractice is a drupal folder.
  • Generate module boilerplate using Drupal Console by using the following command 

    $ drupal generate:module
  • Answer a series of questions generated by Drupal Console.generate module image
  • Generate custom entity content using Drupal Console by using the following command.

    $ drupal generate:entity:content
  • Answer a series of questions generated by the entity generator.
generate entity
  • The directory structure of the generated module
directory structure

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

$fields['status'] = BaseFieldDefinition::create('boolean')
     ->setLabel(t('Status'))
     ->setDescription(t('Status of post action entity'))
     ->setDefaultValue(TRUE)
     ->setSettings(['on_label' => 'Published', 'off_label' => 'Unpublished'])
     ->setDisplayOptions('view', [
       'label' => 'visible',
       'type' => 'boolean',
       'weight' => 2,
     ])
     ->setDisplayOptions('form', [
       'type' => 'boolean_checkbox',
       'weight' => 2,
     ])
     ->setDisplayConfigurable('view', TRUE)
     ->setDisplayConfigurable('form', TRUE);

Various entity features, including menus, paths, and forms may be changed, and constraints and properties can be applied to fields. For the time being, let’s use the drush or Drupal Console to install the postactions custom entity created module.

Using Drush:

$ drush en postactions -y 

Using Drupal Console:

$ drupal module:install postactions

Manually install the module:

Goto Extend -> check postactions module-> click install

After installation is completed then navigate to structure.

drupal structure

Steps to create Bundles and add fields to postactions entity type

  • Navigate to Manage -> Structure and Click on “Postactions entity type”.
  • Add a suitable label as follows 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 the remaining bundles of the reading list and likes.

List of entity type bundles:

bundle list

Step to Form display of bundles:

  • Navigate to Manage -> Structure and Click on “Postactions entity list”
  • Click on postactions entity -> select bundle as follows
  • Form should be
final output

You can add different fields as your requirements.

Conclusion

Entities are powerful building blocks in Drupal 8/9. Custom entity types provide custom functionality, boost performance and have total control over the structure of data, paths, menus, and storage schema. Easily create a custom entity by using Drupal Console.

Comments