Problem/Motivation

The views handler provided for entity properties / fields does not handle generic 'entity'-type properties. eg:

  $properties['referenced'] = array(
    'label' => t("Referenced item"),
    'type' => 'entity',
    'description' => t("The entity item referenced by this transaction."),
    'computed' => TRUE,
    'entity views field' => TRUE,
    'getter callback' => '_referenced_entity_callback', // Returns an EntityMetadataWrapper-wrapped entity
  );

This is because the entity_views_handler_field_entity::init() function sets 'entity', pulled from the property 'type' definition, as the entity type:

  /**
   * Initialize the entity type with the field's entity type.
   */
  public function init(&$view, &$options) {
    parent::init($view, $options);
    $this->field_entity_type = entity_property_extract_innermost_type($this->definition['type']);
  }

Down the line, 'entity' get passed as the entity type to entity_get_controller(), which dies as 'entity' is not a valid entity type.

Proposed resolution

While arguably, it's impossible to create a full featured entity handler that can handle arbitrary entity types, it *should* be possible to create a simplified handler that can use $entity->uri(), $entity->label() etc. to create links to these arbitrary entities.

Remaining tasks

User interface changes

API changes

Original report by @username

Hi,
I have a self-written entity type called 'A'. Entity type 'A' has 3 properties: entity_type, entity_id and revision_id. These 3 properties store the reference information to an exisiting entity.
Now I have used hook_entity_property_info() to define the property informations, including following snippet:

<?php
  $properties['entity']  = array(
    'label' => t('Entity'),
    'type' => 'entity',
    'computed' => TRUE,
    'getter callback' => 'my_getter_callback',
  );
?>

Providing the custom callback:

<?php
function my_getter_callback($data, array $options, $name, $type, $info) {
  return entity_metadata_wrapper($data->entity_type, $data->entity_id);
}
?>

With this, I'm able to directly access the referenced entity on my entities of type 'A' like:

<?php
entity_metadata_wrapper('A', $id)->entity->label();
?>

The API documentation about hook_entity_property_info() tells me:

entity views field: (optional) If enabled, the property is automatically exposed as views field available to all views query backends listing this entity-type. As the property value will always be generated from a loaded entity object, this is particularly useful for 'computed' properties. Defaults to FALSE.

I tried this with my 'entity' property to be able to use it in Views, that means i just added 'entity views field' => TRUE to my $properties['entity'] array. And the field even appears then in the Views configurations for entities of type 'A', so I can add the 'entity' field and I'm able to setup the display settings (Show label, show id, render full entity, link to the entity and so on).
Unfortunately, the view is not able to be rendered then. I just get a fatal error:

PHP Fatal error: Class name must be a valid object or a string in [...]\includes\common.inc on line 7809

I guess I did something wrong or have missed a point, but I can't find out what the problem is. If you could give me a hint or something, I'd be very thankful.
Thanks!

Comments

mxh’s picture

I think I got it. 'entity views field' can only be used when the entity type is known beforehand, so I can't use 'type' => 'entity' here. It's a pity though, because I'd like to be able to link to any entity regardless of its type in the view.

Maintainers may close this, if I am right with my assumption? Thank you :)

mxh’s picture

Status: Active » Closed (won't fix)

Marking it as wont fix since there doesnt seem to be any interest on it.

brianV’s picture

Version: 7.x-1.2 » 7.x-1.x-dev
Status: Closed (won't fix) » Active

Re-opening, as this is a legit bug.

To expand on e-xam's report above, entity_get_controller() gets invoked with 'entity' passed as the entity type. This doesn't work as the class and controller info can't be found (naturally), and when a the function tries to create a new entity controller instance, it dies.

The root of this problem is in 'entity_views_handler_field_entity.inc' where the init function is written as:

  /**
   * Initialize the entity type with the field's entity type.
   */
  public function init(&$view, &$options) {
    parent::init($view, $options);
    $this->field_entity_type = entity_property_extract_innermost_type($this->definition['type']);
  }

As you can see, it sets the entity type as the type declared in the 'type' key described in the entity property definition. I can confirm this solution by manually overriding $this->field_entity_type to a known type.

Ideally, an 'entity' type should have it's own simplified field handler that can call $entity->label(), $entity->uri etc. to be able to get the id, label, path, create links etc. for these columns which may have arbitrary entity types in them.

brianV’s picture

Title: hook_entity_property_info(): 'entity views field' example? » entity_views_field_handler_entity.inc does not handle entity properties of type 'entity'
Issue summary: View changes

Renamed issue and added a proper issue summary to try to improve clarity about this problem.

brianV’s picture

Category: Support request » Bug report

Also setting as a bug report.

artreaktor’s picture

Title: entity_views_field_handler_entity.inc does not handle entity properties of type 'entity' » entity_views_handler_field_entity.inc does not handle entity properties of type 'entity'

Wrong file name. The correct one is entity_views_handler_field_entity.inc