blainelang of Nextide
1/22/2014 - 5:15 PM

HOOK_views_pre_execute example

HOOK_views_pre_execute example

/**
 * HOOK views_pre_execute
 * Need to alter the SQL query for the join on the process_variables table
 */
function maestro_views_pre_execute(&$view) {
  $query = $view->build_info['query'];
  if (!is_object($query)) {
    return;
  }
  $tables =& $query->getTables();
  $fields =& $query->getFields();
  $where =& $query->conditions();

  if(isset($view->exposed_input['variable_name']) OR isset($fields['maestro_template_variables_variable_name'])) {

    /* The query object in $view is a selectQuery object
       To eliminate the duplicate records, needed to change the
       order of the joins and use a more specific join condition.
       The join of the process_variables table was creating
       the duplicates even with DISTINCT query option.

       Reference: http://drupal.org/node/310077
     */

    $process_variables = $tables['maestro_process_variables'];
    unset($tables['maestro_process_variables']);
    $template_variables = $tables['maestro_template_variables'];
    unset($tables['maestro_template_variables']);
    $tables['maestro_process_variables'] = $process_variables;
    $tables['maestro_template_variables'] = $template_variables;

    $tables['maestro_template_variables']['condition'] = "maestro_template.id = maestro_template_variables.template_id AND " .
      "maestro_process_variables.template_variable_id = maestro_template_variables.id AND " .
      "maestro_process_variables.process_id = maestro_queue.process_id";

  }

}