1_676x476In Drupal 8 we are allowed to create our custom entity types and add multiple data fields of different data types. However, this process can be very time-consuming without using any modules or template tools.

Content Entity Example: https://www.drupal.org/node/2192175

1_676x476

Drupal’s website has provided the above example for developers. It’s possible to use the example as a template to create a new entity type but I think the easiest way to finish this job is to use the tool DRUPAL CONSOLE (http://drupalconsole.com). By using Drupal console, you can create an entity type module in one minute without coding.

INSTALLATION

// Run this in your terminal to get the latest Console version:$ curl -LSs http://drupalconsole.com/installer | php// Or if you don’t have curl:$ php -r “readfile(‘http://drupalconsole.com/installer’);” | php// To access the Console from anywhere your system, move console.phar and rename it to drupal:

$ mv console.phar /usr/local/bin/drupal

CREATE A MODULE

$ drupal generate:module

Screen Shot 2015-03-13 at 10.28.34 AM (1)

CREATE AN ENTITY

$ drupal generate:entity:content

Screen Shot 2015-03-13 at 10.28.34 AM

That’s it! Now you have the whole module with content entity.

Screen Shot 2015-03-13 at 10.30.56 AM

You can now install the module or edit the MyEntity.php and the MyEntityListController file if you want to add extra data fields to your entity. For example, if you want to add a new string field “address” you have to add the following code:

In MyEntity.php’s baseFieldDefinitions function

Screen Shot 2015-03-13 at 12.31.43 PM

In MyEntityListController’s  buildHeader() function:

$header[address] = $this->t(address);

In MyEntityListController’s buildRow function:

$row[‘address’] = $entity->address->value;

P.S. If you try to add a new data field after you install the module, a new table will be inserted into the database so it is advisable to add your data fields before you install the module.

Reference: (https://www.drupal.org/project/console)