\hook_entity_info
function
Inform the base system and the Field API about one or more entity types.

Inform the base system and the Field API about one or more entity types.

Inform the system about one or more entity types (i.e., object types that can be loaded via entity_load() and, optionally, to which fields can be attached).

Comments

Mile23’s picture

If you find yourself scratching your head having read this page, head on over to the Field Attach API page for a little bit more clarity.

rszrama’s picture

Note that even though the documentation for the label callback key tells you to look at entity_label() as an example, the parameters of that function are actually the reverse of the parameters passed to your entity's label callback.

steveoliver’s picture

For view modes, re:

Keys of the array are view mode names. ...

I found in Drupal 7.7 and 7.8-dev that the view mode key must only contain alphanumeric characters with no punctuation such as underscores (i.e. productselection works, while product_selection does not). An underscore in the key of my custom view mode caused the display mode to be displayed but not configurable in Structure > Content types (Manage display). Maybe this is a bug I should open an issue for?

Dave Reid’s picture

Did you enable that view mode to have custom settings on the 'Default' page under Manage display?

alanom’s picture

A little more on label callback: the second parameter seems to consistently be a string of the entity type, but for some reason I'm finding that the first parameter is sometimes an integer of the entity id, sometimes the object of the entity. I've not yet figured out what's going on, my best guess based on what I'm seeing is that maybe it's the ID if the entity hasn't yet been loaded with entity_load(); and the object if it has. This fits the behaviour I'm seeing but seems odd.

DamienMcKenna’s picture

Might it be assumed that the 'label' field is required, even if the 'label callback' is defined? That isn't clear.

mikey_p’s picture

Yes, without 'label' you'll probably get something like:

 Undefined index: label in entity_get_info() (line 7670 of includes/common.inc).
bluesky_still’s picture

load hook (optional) ....

jpons’s picture

I've spent hours strugging with drupal APIs such as form API, field API.. I had to use xdebug to know how things work because the documentation isn't really one in most of the cases, and you find your self struggling with line of codes (note that drupal overly uses keyed array with no appropriate doc).

Now i'd like to build an entity that is fieldable and that does not support bundle except tne default one which is itself. That means I'd like this entity to be fieldable via the field administration UI (as for node bundles) => http://your-site/admin/structure/types/manage/your-bundle/fields

Does someone know how to make this without having to write hundred lines of codes in which case I'd better develop it from scratch. I keep thinking drupal entity API solely allow us to use abstract ourself from querying the database to load our entity and does not bring any other features ...

What's are the pros of using this instead of writing a module from scratch if we just want a CRUD form for an entity ?

skriptble’s picture

I had the same problem. Here's what I did:
Declare the default bundle admin:

$entity_info[ENTITY_NAME]['bundles'][ENTITY_NAME] = array(
  'label' => t('Entity Name'),
  'admin' => array(
    'path' => 'path/to/admin',
    'access arguments' => array('administer ENTITY TYPE'),
  ),
);

Then I declared a default settings form in hook_menu:

  $items['path/to/admin'] = array(
    'title' => 'ENTITY settings',
    'description' => 'Configure the settings for the ENTITY TYPE.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('MODULE_NAME_ENTITY_NAME_settings'),
    'access arguments' => array('administer ENTITY TYPE'),
    'weight' => -10,
  );
  $items['path/to/admin/settings'] = array(
    'title' => 'Settings',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );

The settings page is necessary because the "Manage Fields" and "Manage Display" are local tasks. You don't have to make the Settings a form, but you can put some entity configuration options there.

Entity API is useful for integrating with other modules (like Rules and Views) and gives you a whole bunch of other useful stuff.

jpons’s picture

Thank you for your answer. I also found the solution, indeed the admin key in the array makes the whole thing work.

deepdive’s picture

You shouldn’t be leaving home without getting into the EntityAPI module if you want to do anything with entities. This module adds a whole lot of keys to hook_entity_info. You will find all the gory details here:

http://drupalcode.org/project/entity.git/blob/refs/heads/7.x-1.x:/entity.api.php

Neograph734’s picture

nally’s picture

Thanks for both links. The second didn't include "entity class". Both links help.

joachim’s picture

If you're omitting 'bundles' to get the system to define a single bundle automatically for you, you must not also have ['entity keys']['bundle'] defined.

nevergone’s picture