8

I have an entity that references node and user via nid and uid fields.

I have created an entity around my schema and would like to use it in views. I can create a view based on a new entity that shows fields from the entity.

Now I want to add relationships to node and user so I could pull fields from them. So I want to show fields from related user and node, not my entity bundles.

What's the right way to do this?

1
  • I cannot post answer to that question, as it was marked as dupe by mistake. I will post solution here - drupal.org/node/1208874. It states that hook_view_data is not the right place to define entity fields for views, EntityDefaultViewsController should be used instead.
    – Bulat
    Mar 8, 2013 at 17:27

2 Answers 2

8

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'),
    ),
  );
}
3
  • First option is not an option really as I already have a uid field in my schema. Second option sounds better, but I read somewhere that you are not supposed to use data hooks with entity api.
    – Bulat
    Mar 8, 2013 at 13:20
  • Could not find anything better than the second option and it worked. Tnx!
    – Bulat
    Mar 18, 2013 at 10:19
  • One note on this. If you already have a custom views implementation, this will not work as an hook_alter, you need to add this to your view implementation code. May 28, 2015 at 1:29
10

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';
  }
?>

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.