Drupal - How to add relationship in view for custom entity
Entity API supports this out of the box, but you will have to let it know which entity your data represents. If Entity API encounters a field containing numeric data it will by default assign it the type 'integer'. Just change the type to the right entity type and you can define the relations in Views:
<?php
/**
* Implements hook_entity_property_info_alter().
*/
function my_entity_entity_property_info_alter(&$info) {
$info['my_entity']['properties']['nid']['type'] = 'node';
$info['my_entity']['properties']['uid']['type'] = 'user';
$info['my_entity']['properties']['tid']['type'] = 'taxonomy_term';
}
?>
two solutions:
1)using relation ,relation end field,relation UI
2)using hook_views_data_alter
example from commerce module:
function hook_views_data_alter(&$data) {
// Expose the uid as a relationship to users.
$data['users']['uc_orders'] = array(
'title' => t('Orders'),
'help' => t('Relate a user to the orders they have placed. This relationship will create one record for each order placed by the user.'),
'relationship' => array(
'base' => 'uc_orders',
'base field' => 'uid',
'relationship field' => 'uid',
'handler' => 'views_handler_relationship',
'label' => t('orders'),
),
);
}