Community Documentation

Creating, Loading, and Deleting Entity Types

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?

About this page

Drupal version
Drupal 7.x
Audience
Programmers
Level
Intermediate
Keywords
entity

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here