1

I managed to programmatically add tables/joins in hook_views_query_alter() like this:

$td_join = new views_join();
$td_join->table = 'taxonomy_term_data';
$td_join->field = 'tid';
$td_join->left_table = 'audience_node_og_mvh_ti';
$td_join->left_field = 'tid';
$td_join->type = 'inner';

$query->add_relationship('audience_node_og_mvh_ti_td', $td_join, 'node', 'audience_node_og_mvh_ti');

When I go to edit the view and check the "auto preview" I can see that the generated query is correct:

SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created, audience_node_og_mvh.title AS mvh_title, audience_node_og_mvh_ti_td.name AS field_data_field_cv_node_entity_type
FROM 
{node} node
inner JOIN {field_data_group_audience} audience_node ON node.nid = audience_node.entity_id
inner JOIN {og} audience_node_og ON audience_node.group_audience_gid = audience_node_og.gid
inner JOIN {node} audience_node_og_mvh ON audience_node_og.etid = audience_node_og_mvh.nid
inner JOIN {taxonomy_index} audience_node_og_mvh_ti ON audience_node_og_mvh.nid = audience_node_og_mvh_ti.nid
LEFT JOIN {taxonomy_index} audience_node_og_mvh_ti_audience_node_og_mvh_ti ON audience_node_og_mvh_ti.nid = audience_node_og_mvh_ti_audience_node_og_mvh_ti.nid
inner JOIN {taxonomy_term_data} audience_node_og_mvh_ti_td ON audience_node_og_mvh_ti_audience_node_og_mvh_ti.tid = audience_node_og_mvh_ti_td.tid
WHERE (( (node.status = '1') )AND( (node.type IN  ('johndoe')) ))
ORDER BY node_created DESC
LIMIT 10 OFFSET 0

But somehow I can't figure out how the new fields in the query (the ones from the joins) can be shown in the view itself.

Does anyone know how to do this?

2 Answers 2

2

If you want view to show your fields, you should not use a hook_views_query_alter. instead you should implement hook_views_data() to describe your audience_node_og_mvh_ti table to view and be able to use its columns as fields when building your views using the UI.

You can however access your view's query results in an hook_views_pre_render() implementations to add your additional field to the output.

2
  • can you give us an example?
    – Asy
    Commented Feb 21, 2017 at 9:12
  • we should use hook_views_data() instead of hook_views_query_alter() or we should use them both as a complementary way? And how can we use EntityViewsData instead of hook_views_data() since I'm using drupal 8.
    – Asy
    Commented Feb 21, 2017 at 10:39
0

If the data is in the views result row object then you can use the customfield module.

http://drupal.org/project/views_customfield

You have to create a php field and you have access to the $data object for each row. So what you can use it to do a return "string"

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.