2

I've attached a new entityreference field to users that referenced my custom entity type, say it's called my_entity. When I dpm() the user or view the tokens, I can see my custom entity properties (id, name, etc) but when I view the user I do not see the field label or the entity label.

I threw a dpm($items) into hook_field_formatter_view() inside entityreference.module and discovered that there are 0 items available to be displayed, but they are available in many other places! (as described above)

In hook_entity_info():

<?php
return array('my_entity' => array(
  'label' => t('My Entity'),
  'controller class' => 'MyEntityController',
  'base table' => 'my_entity',
  'uri callback' => 'my_entity_uri',
  'load hook' => 'my_entity_load',
  'fieldable' => FALSE,
  'label callback' => 'my_entity_label',
  'entity keys' => array(
    'id' => 'id',
    'label' => 'name',
  ),
);
?>

I tried just setting the entity keys['label'] to the schema field, which is supposed to be adequate to set the label, but I also tried 'label callback' and even setting up the bundle info, which is not necessary for my entity.

I've also tried adding the property into hook_entity_property_info() in the hope that it would be "made aware" to other modules, but no luck:

<?php
return array('my_entity' => array(
  'properties' => array(
    'name' => array(
      'label' => 'Name',
      'type' => 'text',
      'schema field' => 'name',
    ),
  ),
);
?>

Any ideas?

2
  • I'm not strong on this stuff, but I did notice this comment: api.drupal.org/comment/48533#comment-48533 ... this would indicate that if you are returning a static value rather than storing the value in the db, the $items will be an empty array ?
    – Dom Day
    Commented Jun 29, 2013 at 2:01
  • Thanks for tracking that down! There's a reference to hook_field_prepare_view() which could help me out here. Commented Jun 30, 2013 at 17:32

1 Answer 1

1

I figured this one out. Entityreference runs entity_access() before returning $items:

$items[$id][$delta]['access'] = entity_access('view', $field['settings']['target_type'], $target_entities[$item['target_id']]);

To get this working, you need to set the 'access callback' property in hook_entity_info(), which is an extended property that is checked by entity.module, not by core's entity functionality. So, for example:

<?php
return array('my_entity' => array(
  'label' => t('My Entity'),
  'controller class' => 'MyEntityController',
  'base table' => 'my_entity',
  'uri callback' => 'my_entity_uri',
  'load hook' => 'my_entity_load',
  'fieldable' => FALSE,
  'label callback' => 'my_entity_label',
  'access callback' => 'my_access_check',
  'entity keys' => array(
    'id' => 'id',
    'label' => 'name',
  ),
);

function my_access_check($op, $entity, $account, $entity_type) {
  // ...do something
  return TRUE;
}
?>

Your Answer

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

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