5

I have a view displaying node titles formatted as a table. In a custom module I'm using hook_views_pre_execute to do do a table join and add a field to the views query. (I cannot use a regular relationship in Views to accomplish the join, because it has to be a RIGHT JOIN).

When I inspect the resulting view object with dpm($view) in hook_views_pre_render, I can see that the objects in $view->result contain the string values of the node_title, nid, and field_myfield_value (where field_myfield_value is added via the custom module).

But how do I actually display field_myfield_value in the view? And more specifically, how do I make it a column of the table?

0

3 Answers 3

7
+50

To take a different approach: you could add the field through the UI as normal, and use hook_views_query_alter() to change the join type:

function MYMODULE views_query_alter(&$view, &$query) {
  if ($view->name == 'my_view' && $view->current_display == 'foo') {
    $query->table_queue['field_data_field_myfield']['join']->type = 'RIGHT';
  }
}

That should get you the same effect with a bit less effort.

2
  • It would be great if I can use the UI for the output. But is this possible? The views_join class description says: type: either LEFT (default) or INNER, so I was assuming we cannot get a RIGHT JOIN via the Views API.
    – arjan
    Jun 7, 2014 at 23:48
  • The type is passed directly through to SelectQuery::addJoin(), I can't think of a reason it wouldn't work
    – Clive
    Jun 7, 2014 at 23:50
1

I guess the easiest would be to add i.e. the title field again in the views ui and then in hook_views_post_execute set $row->title2 (or whatever it would be called) to the value of your field.

Alternatively, you might try to describe the join to views, add the column in the ui "normally" and "only" alter the join. (Clive was faster with that one :)

1
  • Thanks, that trick of setting the value of an existing field to something else may come in handy some time. Changing the query was a cleaner solution in this case.
    – arjan
    Jun 11, 2014 at 13:42
0

I think Clive is right about using hook_views_query_alter being a little easier.

The views_join class also lets you define what is the left_table, left_field, (right) table and (right) field. Perhaps you would need to do more re-building in your hook_views_query_alter to get all the tables in the right place.

Considering the last part of your question "But how do I actually display field_myfield_value in the view?" hasn't been addressed yet, I think you might need to write a module to add the field to views. This article seems pretty straight-forward: http://www.grasmash.com/article/demystifying-views-api-developers-guide-integrating-views In it, they discuss hook_views_data() (adding data to a view via a custom table and extending the views_handler_field class to display the data.

4
  • Thanks, that looks like a good post. My problem with displaying fields had to do with fields added via hook_views_pre_execute. I went that route because I was misled by the descriptions of views_join and hook_views_data, which make it look like only LEFT and INNER joins are supported.
    – arjan
    Jun 11, 2014 at 13:52
  • P.S. your link is missing an final s and I can't edit the post without changing more than that :S
    – arjan
    Jun 11, 2014 at 13:54
  • Thanks, fixed the link. In your MYMODULE_views_pre_execute, have you tried adding using the addField() method of the query? (arrived at by looking at drupal.org/node/748844#comment-7070234 ). Perhaps you could post your not-yet-working function?
    – Nels
    Jun 11, 2014 at 19:41
  • Yes, I was using $query->rightJoin() plus $query->addField(). But addField only adds the table column to the select statement of the SQL query, it doesn't influence the rendering.
    – arjan
    Jun 11, 2014 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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