Views Relationship with Custom Entity and Node

1.5k views Asked by At

I have custom entity with just a base table and it is referenced with the node with its field field_a. Now I want to create the relationship between these two entities (node and my custom entity). I can get the result working but I am not getting the node fields listed in the views field section.

// Base table for custom entity is "clinical_trial_research_sites"

function clinical_trial_views_data_alter(&$data) {
 $data['clinical_trial_research_sites']['node'] = array(
    'title' => t('Clinical Trial'),
    'help' => t('Field Reference Appear in Location Content type.'),
    // Information of the referenced table!!
    'relationship' => array(
      'handler' => 'views_handler_relationship',
      'label' => t('Location'),
      'base' => 'field_data_field_site_comp_code', // node field table name
      'base field' => 'field_site_comp_code_value', // field column name of node field
      'relationship field' => 'site_comp_code', // This is referencing field in my custom entity
    ),
  );
}

I can get the sql working , inner join working but views is not listing all the fields of the node as I want to display the some of the fields from the nodes too!!. I think I am missing some information to tell the views that relationship is not only field table but with the node as a whole!!

1

There are 1 answers

1
prabeen giri On BEST ANSWER

I fixed it by copying some of the code from the entityreference. I have to use a different views handler for this relationship and add other attributes to that relationship

function clinical_trial_views_data_alter(&$data) {

  $data['clinical_trial_research']['clinical_trial_research_sites'] = array(
    'title' => t('Clinical Trial Research Sites'),
    'help' => t('Field Reference Appear in Clinical Trial Research Sites.'),
    'relationship' => array(
      'handler' => 'views_handler_relationship',
      'label' => t('Clinical Trial Research Sites'),
      'base' => 'clinical_trial_research_sites',
      'base field' => 'study_num',
      'relationship field' => 'study_num',
    ),
  );
  // The reference field in the node entity is not entity reference , its just a basic text field
  $data['clinical_trial_research_sites']['site_comp_code']['relationship'] = array(
    'handler' => 'views_handler_relationship_entity_reverse',
    'field_name' => 'field_site_comp_code',
    'field table' => 'field_data_field_site_comp_code',
    'field field' => "field_site_comp_code_value",
    'base' => 'node',
    'base field' => 'nid',
    'title' => t('Referencing entity'),
    'help' => t('Field Reference Appear in Location Content type.'),
    'title' => t('Location'),
  );
}