I have an entity with a couple properties that are computed. How do I get those to show up in a View? Note that I'm adding the computed fields. The fields from the schema work just fine.

The code uses hook_views_data() to define its fields. Anyone know how to make that work?

Alternatively, how does a field handler know the name of the field it is supposed to be displaying? If I could figure that out, I could write my own handler.


DanZ’s picture

I tried to use views_handler_field_entity, but that doesn't work. It gives the error:

Warning: htmlspecialchars() expects parameter 1 to be string, object given in htmlspecialchars() (line 1545 of /var/www/html/drupal_dev/includes/bootstrap.inc)

What am I missing here? This is the code I put in to use it. What else does the handler need to work? Maybe there needs to be some information about the module that defines the entity? Maybe it's for handling a whole entity as a field, not just a property of an entity.

  $data['uc_order_products']['table']['entity type'] = 'uc_order_product';

  $data['uc_order_products']['total_price'] = array(
    'title' => t('Total price'),
    'help' => t('The price paid for all the products (price * quantity).'),
    'field' => array(
      'handler' => 'views_handler_field_entity',
      'click sortable' => TRUE,
      'float' => TRUE,
    'sort' => array(
      'handler' => 'views_handler_sort',
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',

www.ztwistbooks.com. Math books that are actually fun.

DanZ’s picture

Using this:

'handler' => 'entity_views_handler_field_numeric',

...almost works. It correctly gets the value (using the getter callback) in a regular Views. However, it doesn't work for aggregated fields. The query ends up with a "GROUP BY" on the entity ID, which means that the computed values can't be summed. Any idea how to fix that?

Does it have anything to do with entity_views_plugin_row_entity_view? I can find zero documentation on how to make a View use a plugin.

www.ztwistbooks.com. Math books that are actually fun.