Last active
September 8, 2016 22:07
-
-
Save markisatacomputer/dd8e494ba9ac900e65c9ff451090d9b8 to your computer and use it in GitHub Desktop.
alter and debug a views query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Alters the past events view query to look up event type nodes with a featured program tagged with the contextual filter value | |
* @param [type] &$view [description] | |
* @param [type] &$query [description] | |
* @return [type] [description] | |
*/ | |
function debug_module_views_query_alter(&$view, &$query) { | |
if (in_array('after_dark_context_past', $query->options['query_tags'])) { | |
$value = isset($query->where[0]['conditions'][0]['value'][':field_data_field_event_series_field_event_series_tid1']) ? $query->where[0]['conditions'][0]['value'][':field_data_field_event_series_field_event_series_tid1'] : false; | |
if ($value) { | |
// Add Join so our condition works | |
$join = new views_join(); | |
$join->table = 'field_data_field_event_series'; | |
$join->left_table = 'field_collection_item_field_data_field_featured_programs'; | |
$join->left_field = 'item_id'; | |
$join->field = 'entity_id'; | |
$join->extra = array( | |
array( | |
'field' => 'entity_type', | |
'value' => 'field_collection_item' | |
), | |
array( | |
'field' => 'deleted', | |
'value' => 0, | |
'numeric' => true | |
) | |
); | |
$join->type = 'LEFT'; | |
$query->add_relationship('field_collection_item_field_data_field_featured_programs__field_data_field_event_series',$join,'field_collection_item_field_data_field_featured_programs'); | |
$query->add_field('field_data_field_event_series','field_event_series_tid','field_collection_item_field_data_field_featured_programs__field_data_field_event_series'); | |
// Add query condition | |
$query->where[0]['type'] = 'OR'; | |
$condition = array( | |
'field' => 'field_collection_item_field_data_field_featured_programs__field_data_field_event_series.field_event_series_tid = :field_data_field_event_series_field_event_series_tid', | |
'value' => array(':field_data_field_event_series_field_event_series_tid' => $value), | |
'operator' => 'formula' | |
); | |
array_push($query->where[0]['conditions'],$condition); | |
} | |
} | |
} | |
function debug_module_views_pre_render(&$view) { | |
$string = dpq($view->build_info['query'], true); // get the query string with devel's help | |
$string = Database::getConnection()->prefixTables($string); // replace curly braces with prefixed table names | |
dpm($string); // print! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Drupal 7
This function is the important part:
https://gist.github.com/markisatacomputer/dd8e494ba9ac900e65c9ff451090d9b8#file-debug_module-views-inc-L45-L49