1 entity.api.php | hook_entity_info() |
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).
Return value
An array of information about one or more entity types: Keys are entity type names; values identify properties of those types that the system needs to know about:
- label: The human-readable name of the type.
- entity class: A class that the controller will use for instantiating entities. Must extend the Entity class or implement EntityInterface.
- controller class: The name of the class that is used to load the objects. The class has to implement the EntityControllerInterface interface. Leave blank to use the DefaultEntityController implementation.
- base table: (used by DefaultEntityController) The name of the entity type's base table.
- static cache: (used by DefaultEntityController) FALSE to disable static caching of entities during a page request. Defaults to TRUE.
- entity cache: (used by DefaultEntityController) Set to TRUE to enable persistent caching of fully loaded entities. This will be considered to be FALSE if there is not a cache table for the entity. Defaults to FALSE.
- field cache: (used by Field API loading and saving of field data) FALSE to disable Field API's persistent cache of field data. Setting this to FALSE is recommended if a higher level persistent cache is available for the entity type. Defaults to TRUE.
- load hook: The name of the hook which should be invoked by DefaultEntityController:attachLoad(), for example 'node_load'.
- fieldable: Set to TRUE if you want your entity type to be fieldable.
- translation: An associative array of modules registered as field translation handlers. Array keys are the module names, array values can be any data structure the module uses to provide field translation. Any empty value disallows the module to appear as a translation handler.
- entity keys: An array describing additional information about the entity.
This is used by both the EntityFieldQuery class and the Field API.
EntityFieldQuery assumes at the very least that the id is always present.
The Field API uses this array to extract the information it needs from
the objects of the type. Elements:
- id: The name of the property that contains the primary id of the entity. Every entity object passed to the Field API must have this property and its value must be numeric.
- revision: The name of the property that contains the revision id of the entity. The Field API assumes that all revision ids are unique across all entities of a type. This entry can be omitted if the entities of this type are not versionable. Defaults to an empty string.
- bundle: The name of the property that contains the bundle name for the entity. The bundle name defines which set of fields are attached to the entity (e.g. what nodes call "content type"). This entry can be omitted if this entity type exposes a single bundle (all entities have the same collection of fields). The name of this single bundle will be the same as the entity type.
- bundle keys: An array describing how the Field API can extract the
information it needs from the bundle objects for this type (e.g
$vocabulary objects for terms; not applicable for nodes). This entry can
be omitted if this type's bundles do not exist as standalone objects.
Elements:
- bundle: The name of the property that contains the name of the bundle object.
- bundles: An array describing all bundles for this object type. Keys are
bundles' machine names, as found in the objects' 'bundle' property
(defined in the 'entity keys' entry above). This entry can be omitted if
this entity type exposes a single bundle (all entities have the same
collection of fields). The name of this single bundle will be the same as
the entity type. Defaults to an empty string. Elements:
- label: The human-readable name of the bundle.
- admin: An array of information that allows Field UI pages to attach
themselves to the existing administration pages for the bundle.
Elements:
- path: the path of the bundle's main administration page, as defined in hook_menu(). If the path includes a placeholder for the bundle, the 'bundle argument', 'bundle helper' and 'real path' keys below are required.
- bundle argument: The position of the placeholder in 'path', if any.
- real path: The actual path (no placeholder) of the bundle's main administration page. This will be used to generate links.
- access callback: As in hook_menu(). 'user_access' will be assumed if no value is provided.
- access arguments: As in hook_menu().
- bundle cache: (used by DefaultEntityController) Set to FALSE to disable persistent caching of fully loaded entities for this bundle. Defaults to TRUE. Has no effect if 'entity cache' for the entity is FALSE.
- view modes: An array describing the display modes for the entity type.
Display modes let entities be displayed differently depending on the
context. For instance, a node can be displayed differently on its own
page ('full' mode), on the home page or taxonomy listings ('teaser'
mode), or in an RSS feed ('rss' mode). Modules taking part in the display
of the entity (notably the Field API) can adjust their behavior depending
on the requested display mode. An additional 'default' display mode is
available for all entity types. This display mode is not intended for
actual entity display, but holds default display settings. For each
available display mode, administrators can configure whether it should use
its own set of field display settings, or just replicate the settings of
the 'default' display mode, thus reducing the amount of display
configurations to keep track of. Keys of the array are display mode
names. Each display mode is described by an array with the following
key/value pairs:
- label: The human-readable name of the display mode
- custom settings: A boolean specifying whether the display mode should by default use its own custom field display settings. If FALSE, entities displayed in this display mode will reuse the 'default' display settings by default (e.g. right after the module exposing the display mode is enabled), but administrators can later use the Field UI to apply custom display settings specific to the display mode.
See also
Related topics
File
- core/
modules/ entity/ entity.api.php, line 121 - Hooks provided by the Entity module.
Code
function hook_entity_info() {
$return = array(
'node' => array(
'label' => t('Node'),
'controller class' => 'NodeController',
'entity class' => 'Node',
'base table' => 'node',
'revision table' => 'node_revision',
'fieldable' => TRUE,
'translation' => array(
'locale' => TRUE,
),
'entity keys' => array(
'id' => 'nid',
'revision' => 'vid',
'bundle' => 'type',
),
'bundle keys' => array(
'bundle' => 'type',
),
'bundles' => array(),
'view modes' => array(
'full' => array(
'label' => t('Full content'),
'custom settings' => FALSE,
),
'teaser' => array(
'label' => t('Teaser'),
'custom settings' => TRUE,
),
'rss' => array(
'label' => t('RSS'),
'custom settings' => FALSE,
),
),
),
);
// Search integration is provided by node.module, so search-related
// display modes for nodes are defined here and not in search.module.
if (module_exists('search')) {
$return['node']['view modes'] += array(
'search_index' => array(
'label' => t('Search index'),
'custom settings' => FALSE,
),
'search_result' => array(
'label' => t('Search result highlighting input'),
'custom settings' => FALSE,
),
);
}
// Bundles must provide a human readable name so we can create help and error
// messages, and the path to attach Field admin pages to.
foreach (node_type_get_names() as $type => $name) {
$return['node']['bundles'][$type] = array(
'label' => $name,
'admin' => array(
'path' => 'admin/structure/types/manage/%node_type',
'real path' => 'admin/structure/types/manage/' . str_replace('_', '-', $type),
'bundle argument' => 4,
'access arguments' => array('administer content types'),
),
);
}
return $return;
}