Drupal7 entities, bundles, and fields

Some of the information in this zone combines aspects of the core Drupal7 entity and field handling and the contributed Entity API extensions, please be aware of this distinction when reading the references.

The core Drupal7 entity/field system as was available around 2010 was quite difficult to use. You can get a feel for them with this now historical article (and the 2 preceding ones that tell the history of Drupal7 entities), which shows how to make a minimal "fieldable" Drupal7 entity with a controller extending core  DrupalDefaultEntityController:

- Drupal Entities - Part 3 - Programming Hello Drupal Entity (Ronald Ashr, 2010)

These are the original entity functions that core's includes/common.inc made available:

function entity_get_info($entity_type = NULL) {
function entity_info_cache_clear() {
function entity_extract_ids($entity_type, $entity) {
function entity_create_stub_entity($entity_type, $ids) {
function entity_load($entity_type, $ids = FALSE, $conditions = array(), $reset = FALSE) {
function entity_load_unchanged($entity_type, $id) {
function entity_get_controller($entity_type) {
function entity_prepare_view($entity_type, $entities, $langcode = NULL) {
function entity_view_mode_prepare($entity_type, $entities, $view_mode, $langcode = NULL) {
function entity_uri($entity_type, $entity) {
function entity_label($entity_type, $entity) {
function entity_language($entity_type, $entity) {
function entity_form_field_validate($entity_type, $form, &$form_state) {
function entity_form_submit_build_entity($entity_type, $entity, $form, &$form_state) {

Note the lack of an entity_save() or entity_delete() function ?

Then along came the contribute Entity API module with some classic Create Read Update Delete (CRUD) functionality. A nice article showing you how to leverage the Entity API by creating and registering a controller extending EntityAPIController (which itself extends  DrupalDefaultEntityController) is Build Your Own Custom Entities in Drupal – Setup (2014). It demonstrates a simple project entity with some direct database table fields id, name, description, deadline. It shows how to create a project entity using entity_create(), set its fields, and save it it using <code>entity_save(), and how to change how project pages are rendered by overriding buildContent() in a ProjectEntityController. But although one can interact with the fields of this simple entity, this is not yet what in Drupal7 is called fieldable and there are no bundles yet.

The next article in that series shows nicely how to make an entity fieldable (and also how to expose the entity to Views).

Visit also