Last updated October 24, 2012. Created by fmizzell on December 9, 2011.
Edited by mducharme, Kristen Pol. Log in to edit this page.
To create a new Entity Type simply create an object of the Entity Type class.
$entity_type = new EntityType();An entity type is composed of 3 things: name, label, and properties. At minimum, we need to set the name and the label of the entity type.
The name is the machine name for the entity type (letters, numbers, and underscores only, no spaces). This is the main identifier and it should be unique to the system. After the creation of the entity type, we will use this name to interact with our entity types.
The label is the human-readable name, and it is used mainly for GUI displays.
$entity_type->name = "eck_employee";
$entity_type->label = "Employee";After we get done editing the information in our entity type, we can save it by calling the method save().
$entity_type->save();I am guessing that you won't want to delete your entity type in the same request that you created it, so if you want to delete your entity type later on (like when your module is being uninstalled) you can do this easily, too.
First, we load the entity type.
$entity_type = EntityType::loadByName('eck_employee');If you wanted to get all of the entity types created through ECK you can use loadAll().
$entity_types = EntityType::loadAll();After an entity type is loaded, deleting it is a simple call to the delete method.
$entity_type->delete();
Comments
Bundles and fields programatically?
This module is very useful, thanks! I am a little confused about how to create bundles and fields programatically. Maybe useful to add a couple of words on that on this page. For example, whether the documentation will be added later or if it is best to create bundles and fields via drupal admin interface?