I have two custom entities in a D7 site. One is 'additional' data on an entity, and I want to attach that data to the main entity every time it is loaded.

Within Entity/Entity API, how can I do that correctly? attachLoad sounded correct but seems to be Field related, all my data are not fields (yet).

3 Answers 3


I've seen modules use attach load as well, which is what you need to get it on the actual entity.

It also depends on your data structure if the base entity has a reference to the additional data entity, you could simply use the entity wrapper along with entity properties. You wouldn't actually have the data on the entity, but have it accessible via the wrapper. The advantage is that you only run extra queries (loading the entity) as required.

In code you would then do

 $entity = load_entity_function($foo);
 $enity_wrapper = entity_metadata_wrapper('entity_type', $entity);
 $additional = $enity_wrapper->addition->value();
 $additional_id = $enity_wrapper->addition->id->value();

In order to have the wrapper do this, you tell it about the additional entity in a hook:

 * Implements hook_entity_property_info().
function module_entity_property_info() {
  $info = array();

  $properties = array();
  $properties['additional'] = array(
    'label' => t('Label'),
    'type' => 'entity_type',
    'description' => t('description.'),
    'getter callback' => 'function_get_entity_id',
    'setter callback' => 'function_set_entity_id',
    'clear' => array('id_of_additional'),
    'schema field' => 'schema_of_additional',

  $info['base_entity']['properties'] = $properties;

  return $info;

You need a function that will get or set the id from the entity. Depending on your naming convention entity modules come with some that you can use.


I'm not familiar with the inner workings of this, but if you don't get other good answer, here is a patch that does something similar to what you ask. It loads the media edit form on the node edit for to which the media element is attached.

This is "edit", and your usecase sounds like "view", but hopefully it can help you get started.


Solved: I began using the Field Collection module to facilitate this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.