1

I have table of my entity with structure:

=====================================
eid | myfield1 | myfield2 | myfield3
=====================================

And also an additional table with primary key being foreign key to my entity id:

==========================
eid | myfield4 | myfield5
==========================

I would like to expose values in my second table to view of fields, of my entity, but I don't want to join second table with first, every time, but only if I will use one of its fields.

I believe it should be doable with views_join class modifying a query at handler level, but is it possible on hook_views_data_alter() level?

Is it possible to do this transparently - so I wouldn't need to add any relationship to view (just like the field values are pulled from fields tables - yes, you can think a bit about it as about creating fields alternative ;) ) ?

0

Technically it's possible, using hook_views_query_alter(), you can adapt the view query to add or change conditions. So you could add a join with something like this:

function modulename_views_query_alter(&$view, &$query) {
  $join = new views_join();
  $join->table = 'mytable';
  $join->field = 'eid';
  $join->left_table = 'entity_table';
  $join->left_field = 'eid';
  $join->type = 'left';
  $query->add_relationship('relationship_name', $join, 'entity_table');
}

But a better approach would be to use hook_views_data() to expose your table to views, and then add the relevant fields when needed (with a relationship though), but in practical terms is no different than altering the query.

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.