I am building a views plugin and I need it to do this:

LEFT JOIN my_table mt ON (mt.nid = node.nid AND mt.value = 'some argument');

I have found documentation for creating a views plugin, but all of the documentation that I have found doesn't say anything about creating view joins like this.

Is it possible? If so, how do I go about doing something like this?

  • 1
    Please provide some code that you have tried, or at least links to the resources that you have consulted. This helps a lot to get an understanding of your situation. – berliner Feb 21 '14 at 0:49
  • I think you're trying to do the impossible here. You're wanting to join tables based on the result of the query but before the query has run. How can the query know what to be if it hasn't run yet? – leexonline Feb 21 '14 at 5:29
  • 1
    The dynamic part of a relationship (JOIN) in views is usually provided by a contextual filter. It could be static or dynamic but as I mentioned above, it cannot be dynamic based on the results of the query. You might want to use a subquery and I have no idea how views works with that. Maybe time for db_query. – leexonline Feb 21 '14 at 5:34
  • There is an extra parameter that you can use for extra join conditions (the AND mt.value = 'some argument' part). Couldn't figure out how to inject the args into it from a views_data definition though – SeanJA Feb 21 '14 at 23:33

Drupal has an extra field in the join in which you can inject the extra where conditions, you can get to it by using a views_query_alter:

function my_module_views_query_alter(&$view, &$query) {
    if ($view->name === 'my_view' && $view->current_display === 'the_display') {
        $args = $view->args[0];
        $query->table_queue['my_joined_table']['join']->extra = array(
                'value' => $args, // pass the args through
                'numeric' => FALSE, // or true if it is
                'field' => 'the_field',
                'operator' => '='

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.