2

Using Drupal 7 and Views 3 - I have created a custom entity and can view it fine through Views using EntityDefaultViewsController. The Table is a view shared to the drupal database and is constructed like:

Table A
------------
id_key
data

I have a drupal user field - field_tableA_data that contains the same data type/items in the Table A data field (they are not linked in any way - two separate systems load this data - in TableA the data is generated by another system and the User field is loaded by the LDAP module)

I want to make it so that I can make a "View" such that when viewing Table A Items I can also make a relationship to the User entities such that: Table A > field_tableA_data > User

Is this possible in code and if so how?

Here is some code I have tried and to no avail:

class TableAEntityViewsController extends EntityDefaultViewsController {

  /**
   * Edit or add extra fields to views_data().
   */
  public function views_data() {
    $data = parent::views_data();
    $data['tableA']['data']['relationship'] = array(
      'handler' => 'views_handler_relationship',
      'base' => 'field_data_field_tablea_data',
      'base field' => 'field_tablea_data_value',
      'title' => t('TableA Data'),
      'label' => t('TableA Data'),
    );
    return $data;
  }
}

and in the module (table A entity I have a hook):

function (module)_views_data_alter(&$data) {
    $data['tableA']['table']['join'] = array(
    'field_data_field_tablea_data' => array(
      'left_field' => 'field_tableA_data_value',
      'field' => 'data',
    ),
    'user' => array(
      'left_field' => 'uid',
      'field' => 'field_data_tablea_data.entity_id',
    ),
  );
}

The best this gives me though is User:field_tableA data and no other fields from User - I'm trying to get uid and username.

1
1

I know this is not timely, but if someone else is looking for an answer:

Assumptions: - Using Entity API - Joining to another entity where the entity id is a field in your entity's base table - It's a one-to-one relationship

  1. Add the views controller class to your entity info file function my_entity_entity_info() {

    $info['my_entity'] = array( 'views controller class' => 'MyEntityViewsController', ... );

2 Extend EntityDefaultMetadataController

class MyEntityMetadataController extends EntityDefaultMetadataController {

  public function entityPropertyInfo() {

    $info = parent::entityPropertyInfo();

    // This exposes the fields as references to views. 
    $info['my_entity_base_table']['properties']['uid']['type'] = 'user'; 

   ....
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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