hook_entity_info

Versions
mediamosa-21
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).

See also

entity_load()

@see hook_entity_info_alter()

Return value

An array whose keys are entity type names and whose values identify properties of those types that the system needs to know about:

  • label: The human-readable name of the type.
  • controller class: The name of the class that is used to load the objects. The class has to implement the DrupalEntityControllerInterface interface. Leave blank to use the DrupalDefaultEntityController implementation.
  • base table: (used by DrupalDefaultEntityController) The name of the entity type's base table.
  • static cache: (used by DrupalDefaultEntityController) FALSE to disable static caching of entities during a page request. Defaults to TRUE.
  • load hook: The name of the hook which should be invoked by DrupalDefaultEntityController:attachLoad(), for example 'node_load'.
  • uri callback: A function taking an entity as argument and returning the uri elements of the entity, e.g. 'path' and 'options'. The actual entity uri can be constructed by passing these elements to url().
  • fieldable: Set to TRUE if you want your entity type to be fieldable.
  • object keys: An array describing how the Field API can 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 object. Every 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 object. The Field API assumes that all revision ids are unique across all objects of a type. This element can be omitted if the objects of this type are not versionable.
    • bundle: The name of the property that contains the bundle name for the object. The bundle name defines which set of fields are attached to the object (e.g. what nodes call "content type"). This element can be omitted if this type has no bundles (all objects have the same fields).
  • 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 element 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.
  • cacheable: A boolean indicating whether Field API should cache loaded fields for each object, reducing the cost of field_attach_load().
  • 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 'object keys' entry above). 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().
  • view modes: An array describing the view modes for the entity type. View 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 view mode. Keys of the array are view mode names. Each view mode is described by an array with the following key/value pairs:

    • label: The human-readable name of the view mode

Related topics

Code

modules/system/system.api.php, line 124

<?php
function hook_entity_info() {
  $return = array(
    'node' => array(
      'label' => t('Node'),
      'controller class' => 'NodeController',
      'base table' => 'node',
      'revision table' => 'node_revision',
      'path callback' => 'node_path',
      'fieldable' => TRUE,
      'object keys' => array(
        'id' => 'nid',
        'revision' => 'vid',
        'bundle' => 'type',
      ),
      'bundle keys' => array(
        'bundle' => 'type',
      ),
      // Node.module handles its own caching.
      // 'cacheable' => FALSE,
      'bundles' => array(),
      'view modes' => array(
        'full' => array(
          'label' => t('Full node'),
        ),
        'teaser' => array(
          'label' => t('Teaser'),
        ),
        'rss' => array(
          'label' => t('RSS'),
        ),
      ),
    ),
  );

  // Search integration is provided by node.module, so search-related
  // view 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'),
      ),
      'search_result' => array(
        'label' => t('Search result'),
      ),
    );
  }

  // 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;
}
?>